2014-02-16 29 views
0

程序應該結束並在最後一個問題得到回答後提供正確答案的數量。相反,程序可以回到循環中的最初問題。 「什麼是阿拉巴馬州的首府」Java 2d數組程序中的無限循環

package exercise09_17; 
import java.util.Scanner; 

public class exercise09_17 { 

    static Scanner input = new Scanner(System.in).useDelimiter("\r\n"); 
    public static void main(String[] args) { 
     int correctAnswer = 0; 
     String [][] grid = { 
       {"Alabama", "California", "Delaware", "Florida", "Georgia", 
       "Hawaii", "Idaho", "Kansas", "Lousiana", "Maryland", "New Mexico", "Oregon", 
       "Pennsylvania", "Rhode Island", "South Carolina", "Texas", "Utah", "Virgina", 
       "West Virginia"}, 
       {"Montgomery", "Sacramento", "Dover", "Tallahassee", "Atlanta", 
       "Honolulu", "Boise", "Topeka", "Baton Rouge", "Annapolis", "San Jose", "Salem", 
       "Harrisburg", "Providence", "Columbia", "Austin", "Salt Lake City", "Richmond", 
       "Charleston"}}; 

     for(int i = 0; i< grid.length; i++){ 

      for(int k = 0; k < grid[i].length; k++){ 
       System.out.println("What is the capital of " + grid[0][k] + "?"); 
       String capital = input.next(); 
       String answer = grid[1][k]; 


       if(capital.equalsIgnoreCase(answer)){ 
       correctAnswer ++; 
       System.out.println("Your answer is correct"); 
       } 
       else 
        System.out.println("The correct answer should be " + answer); 
      } 

     } 
     System.out.println("The correct count is " + correctAnswer); 
    } 
} 
+0

這是不是無限的。這只是因爲嵌套的循環而重複。 – Makoto

+0

打印計數的語句位於循環內部。 –

回答

1

它不是無限的。它只是兩次:

for(int i = 0; i< grid.length; i++){ 

沒有必要爲這個i變量這裏,因此整個外環爲好,因爲你在第一次就通

使用兩個子陣列, grid[0]grid[1]
1

嘗試去除外部for loop

public class exercise09_17 { 

    static Scanner input = new Scanner(System.in).useDelimiter("\r\n"); 
    public static void main(String[] args) { 
     int correctAnswer = 0; 
     String [][] grid = { 
       {"Alabama", "California", "Delaware", "Florida", "Georgia", 
       "Hawaii", "Idaho", "Kansas", "Lousiana", "Maryland", "New Mexico", "Oregon", 
       "Pennsylvania", "Rhode Island", "South Carolina", "Texas", "Utah", "Virgina", 
       "West Virginia"}, 
       {"Montgomery", "Sacramento", "Dover", "Tallahassee", "Atlanta", 
       "Honolulu", "Boise", "Topeka", "Baton Rouge", "Annapolis", "San Jose", "Salem", 
       "Harrisburg", "Providence", "Columbia", "Austin", "Salt Lake City", "Richmond", 
       "Charleston"}}; 



     for(int k = 0; k < grid[0].length; k++){ 
      System.out.println("What is the capital of " + grid[0][k] + "?"); 
      String capital = input.next(); 
      String answer = grid[1][k]; 


      if(capital.equalsIgnoreCase(answer)){ 
       correctAnswer ++; 
       System.out.println("Your answer is correct"); 
      } else 
       System.out.println("The correct answer should be " + answer); 
     } 

     System.out.println("The correct count is " + correctAnswer); 
    } 
} 
+0

你的代碼片段不會編譯,因爲沒有'i'變量,for循環應該有'k torquestomp

+0

這很好。謝謝 –

0

變化

System.out.println("What is the capital of " + grid[0][k] + "?"); 

System.out.println("What is the capital of " + grid[i][k] + "?"); 

你通過所有K迭代去後使用grid[0][k]因爲它會回來對同一i=0另一個ķ迭代直到i<grid.length

0

你沒有一個無限循環,但是,你會重複該過程的次數與網格長度一樣多。基本上,你不需要外循環。取下環

for(int i = 0; i < grid.length; i++) 

和替代

for(int k = 0; k < grid[i].length; k++){ 

變化

for(int k = 0; k < grid[0].length; k++){