【问题标题】:The while loop is changing intended values for unknown reasonwhile 循环出于未知原因更改预期值
【发布时间】:2014-04-07 11:43:16
【问题描述】:

我是编码新手,正在努力为数组创建条件,但是我设置的 while 循环条件由于某种原因会更改不应该的数组。如果这很明显,我很抱歉,但我似乎无法在这里找到适用于我的问题的问题。

代码:

import java.util.Scanner;
//================================================================
public class ArrayIrreg {
//----------------------------------------------------------------
    private static Scanner Keyboard = new Scanner(System.in);

    public static void main(String[] args) {
    //----------------------------------------------------------------
        char group, rLetter;

        int num     = 10; // for test
        int rows    = 10;
        int columns =  8;

        // creating 2d array


        System.out.print("Please enter number of rows               : ");
        rows = Keyboard.nextInt();

        while (rows < 0 || rows >= 10) {
            System.out.print("ERROR:Out of range, try again              : ");
            rows = Keyboard.nextInt();
        }

        double[][] figures = new double[rows + 1][num];

        for(int t = 0; t < rows; t++) {
            rLetter = (char)((t)+(int)'A');
            System.out.print("Please enter number of positions in row " + rLetter + " : ");
            columns = Keyboard.nextInt();

            while(columns < 0 || columns >= 8) {
                System.out.print("ERROR:Out of range, try again              : ");
                columns = Keyboard.nextInt();
            }

            for(int j = 0; j <= columns; j++) {
                figures[j] = new double[j] ;
            }

        }

        // filling the array
        for(int row = 0; row < figures.length; ++row) {
            for(int col = 0; col < figures[row].length; ++col) {
                figures[row][col] = 0.0;
            }
        }

        // printing the array
        for(int row = 0; row < figures.length; ++row) {    
            // printing data row
            group = (char)((row)+(int)'A');
            System.out.print(group + " : ");

            for(int col = 0; col < figures[row].length; ++col) {    
                System.out.print(figures[row][col] + " ");
                System.out.print(" ");
            }

            System.out.println();
        }

        // printing final border
        for(int col = 0; col < figures[0].length; ++col) {
            System.out.print("-+");
        }

        System.out.println("  ");
    }
}

当输入的值在while循环成立的条件下第一次显示正确,但是一旦进入while循环并更正了值,代码就会返回不同的错误或显示错误的数量。

我不确定为什么它会导致结果发生变化,因为 while 循环的目的是在初始输入超出预期范围时强制输入新的值。

【问题讨论】:

  • 是时候学习调试了。打印一些东西(变量、流控制),并阅读你得到的错误/堆栈跟踪。它们带有行号。使用实际的调试器可能有点矫枉过正,但学习起来也非常有用。
  • 这似乎对我有用。你能比返回不同的错误或显示错误的数量更具体吗?

标签: java arrays while-loop


【解决方案1】:

您需要在每个Keyboard.nextInt() 之后调用Keyboard.nextLine()

解释:扫描器调用 nextInt() 后,它获取第一个值并将字符串的其余部分留给\n。然后我们使用nextLine() 来消费字符串的其余部分。

【讨论】:

    猜你喜欢
    • 2018-03-12
    • 1970-01-01
    • 1970-01-01
    • 2019-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-09
    • 2023-03-07
    相关资源
    最近更新 更多