在打印全部之前完成数组的输入

发布于 2024-10-31 13:17:21 字数 858 浏览 0 评论 0原文

我正在尝试对数组进行输入,直到它已满,然后打印整个数组。但我无法让循​​环运行,直到数组已满,然后打印所有内容。

这是我的代码:

public static void main(String[] args) {

    Scanner input = new Scanner(System.in);

    String[] course = new String [2]; //creating array
    int [] grade = new int [2];

    System.out.println("Input coursename and grade: ");

    for (int i = 0; i < course.length; i++){
            course[i] = input.next();
            grade [i] = input.nextInt();

    if (i == course.length)
        break;

    //System.out.println("\nHow do you want to order course and grade?");
    //System.out.print("   1 - Ascending?\n"
    //               + "   2 - Decending?\n");

    //System.out.println("Name and grade is " + course[i] + " " + grade[i]);
       System.out.println(Arrays.toString(course)+(grade));

    }

  }
}

如何让循环运行然后跳转到 print 语句?

I am trying to do input to an array until it is full and after that print the entire array. But I cannot get the loop to run until the array is full and after that print all.

Here is my code:

public static void main(String[] args) {

    Scanner input = new Scanner(System.in);

    String[] course = new String [2]; //creating array
    int [] grade = new int [2];

    System.out.println("Input coursename and grade: ");

    for (int i = 0; i < course.length; i++){
            course[i] = input.next();
            grade [i] = input.nextInt();

    if (i == course.length)
        break;

    //System.out.println("\nHow do you want to order course and grade?");
    //System.out.print("   1 - Ascending?\n"
    //               + "   2 - Decending?\n");

    //System.out.println("Name and grade is " + course[i] + " " + grade[i]);
       System.out.println(Arrays.toString(course)+(grade));

    }

  }
}

How can get the loop to run and then jump to the print statement?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

甜点 2024-11-07 13:17:21

循环中的变量 i 永远不可能等于 course.length,因为循环仅运行到 i i i i i i i i i i课程.长度。所以 if 块无论如何都是多余的。

打印语句应该位于 for 块之后,否则您将在每次迭代中打印数组。

public static void main(String[] args) {

    Scanner input = new Scanner(System.in);

    String[] course = new String [2]; //creating array
    int [] grade = new int [2];

    System.out.println("Input coursename and grade: ");

    for (int i = 0; i < course.length; i++) {
        course[i] = input.next();
        grade [i] = input.nextInt();
    }

   System.out.println(Arrays.toString(course)+(grade));
}

the variable i in the loop can never be equal to course.length because the loop runs only until i < course.length. So the if block is redundant anyway.

The printing statement should be AFTER the for block, otherwise you'd be printing the array in each iteration.

public static void main(String[] args) {

    Scanner input = new Scanner(System.in);

    String[] course = new String [2]; //creating array
    int [] grade = new int [2];

    System.out.println("Input coursename and grade: ");

    for (int i = 0; i < course.length; i++) {
        course[i] = input.next();
        grade [i] = input.nextInt();
    }

   System.out.println(Arrays.toString(course)+(grade));
}
无声情话 2024-11-07 13:17:21

grade [i] = input.nextInt(); 之后应该有一个 }
而下面的if根本就没有必要。

There should be a } after grade [i] = input.nextInt();.
And the following if is not necessary at all.

不知在何时 2024-11-07 13:17:21

如果在两个赋值语句之后关闭循环,则循环似乎可以完成这项工作。之后的检查没有用,可以删除。

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        String[] course = new String[2]; // creating array
        int[] grade = new int[2];

        System.out.println("Input coursename and grade: ");

        for (int i = 0; i < course.length; i++) {
            course[i] = input.next();
            grade[i] = input.nextInt();
        }
        System.out.println(Arrays.toString(course) + (grade));

    }

It seems the loop would do the job if it was closed after the two assignment statements. The check afterwards isn't useful and could be removed.

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        String[] course = new String[2]; // creating array
        int[] grade = new int[2];

        System.out.println("Input coursename and grade: ");

        for (int i = 0; i < course.length; i++) {
            course[i] = input.next();
            grade[i] = input.nextInt();
        }
        System.out.println(Arrays.toString(course) + (grade));

    }
面如桃花 2024-11-07 13:17:21

if (i == course.length) 是不必要的,因为当 i == length 时,for 循环完成工作,并且 for-loop-body 不会调用,因此删除此行并把它放在闭环 "}"

接下来是打印你的数组。将最后一行更改为:

System.out.println(Arrays.toString(course) + Arrays.toString((grade)));

if (i == course.length) is unnecesery because when i == length then for loop finish working and for-loop-body doesn't call so remove this line and put instead of it close loop "}"

Next is printing your array. Change the last line to:

System.out.println(Arrays.toString(course) + Arrays.toString((grade)));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文