6

My project is finally complete, but my only problem is that my teacher does not accept "breaks" in our code. Can someone help me resolve this issue, I have been working on it for days and I just can't seem to get the program to work without using them. The breaks are located in my DropYellowDisk and DropRedDisk methods. Other then that issue, my connect four program is flawless.

    private static void DropYellowDisk(String[][] grid) {

        int number = 0;
         Scanner keyboard = new Scanner (System.in);
         System.out.println("Drop a yellow disk at column (1–7): ");
         int c = 2*keyboard.nextInt()+1;


         for (int i=6;i>=0;i--)
            {
              if (grid[i][c] == " ")
              {
                grid[i][c] = "Y";
                break;
              }}
    }

    private static void DropRedDisk(String[][] grid) {

         Scanner keyboard = new Scanner (System.in);
         System.out.print("Drop a red disk at column (1–7): ");
         int c = 2*keyboard.nextInt()+1;
         for (int i =6;i>=0;i--)
            {
              if (grid[i][c] == " ")
              {
                grid[i][c] = "R";
                break;
              }

    }}
1
  • 1
    Wouldn't the simplest way be to assign a value that would fail the for condition to i? It would work even if you put code after the loop Commented Nov 24, 2014 at 22:49

3 Answers 3

8

my teacher does not accept "breaks"

From a programming standpoint, that's just plain silly (although I'm sure it has merit from an instructional one).

But there's an easy workaround in this particular case because the loops your breaking from are all at the end of their respective methods. As such, you can replace them with return statements. i.e:

private static void DropYellowDisk(String[][] grid) {

  for (int i=6;i>=0;i--)
    {
      if (grid[i][c] == " ")
      {
        grid[i][c] = "Y";
        return; //break
      }}
}
Sign up to request clarification or add additional context in comments.

5 Comments

If you teacher doesn't accept breaks, then use goto and surprise him to hell.
It might not be a good style to use 'goto'-s. breaks and continues are really like them, especially with labels. return statements might also increase cyclomatic complexity, make the result harder to understand, reason about. (My take: no prohibition, but not encouraging it either.)
I've been there too Alex no worries. About gotos, if you are checking some nested constraints involving nested loops and when goto makes really readable code for that instance, it's great to use IMO. Otherwise it's a tool to surprise your teacher.
It is good practice when teaching/ giving assignments to students . The goal is make sure students can do it with the topics covered so far.
Thank you everyone especially @drewmoore. The simplest stuff is what really kills me in this class, but I do love it!
4
boolean flag = false;
for (int i=6;i>=0 && !flag;i--) {
    if (grid[i][c] == " ") {
        grid[i][c] = "Y";
        flag = true;
    }
}

Comments

3

You can use boolean flag instead of break with while loop. Also you should compare strings by the equals method.

private static void DropYellowDisk(String[][] grid) {

    int number = 0; boolean flag=true;
     Scanner keyboard = new Scanner (System.in);
     System.out.println("Drop a yellow disk at column (1–7): ");
     int c = 2*keyboard.nextInt()+1;

      int i=6;
      while(i>=0&& flag)
      {
          if(grid[i][c].equals(" "))
          {
              grid[i][c]="Y";
              flag=false;
          }    
          i--;
      }
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.