【问题标题】:Infinite Loop in Java 2d array programJava 2d 数组程序中的无限循环
【发布时间】:2014-02-16 18:30:22
【问题描述】:

程序应该在最后一个问题被回答后结束并给出正确答案的数量。相反,程序回到循环中的初始问题。 “阿拉巴马州的首府是什么”

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);
    }
}

【问题讨论】:

  • 这不是无限的。由于循环的嵌套,它只是在重复自己。
  • 打印计数的语句在循环内。

标签: java loops multidimensional-array infinite


【解决方案1】:

它不是无限的。它只运行两次:

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

这里不需要这个 i 变量,因此也不需要整个外循环,因为您在第一次遍历时使用了两个子数组 grid[0]grid[1]

【讨论】:

    【解决方案2】:

    尝试删除无关的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);
        }
    }
    

    【讨论】:

    • 你的代码 sn-p 不会编译,因为没有 i 变量,for 循环应该有 k &lt; grid[0].length
    【解决方案3】:

    改变

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

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

    由于您使用的是grid[0][k],因此在经历了所有 k 次迭代后,它将在同一 i=0 直到 i&lt;grid.length 上再次返回 k 次迭代

    【讨论】:

      【解决方案4】:

      您没有无限循环,但您将重复该过程的次数与网格长度一样多。基本上,您不需要外部 for 循环。删除循环

      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++ ){
      

      【讨论】:

        猜你喜欢
        • 2014-06-22
        • 1970-01-01
        • 1970-01-01
        • 2023-03-21
        • 2023-03-29
        • 1970-01-01
        • 2015-11-10
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多