繁体   English   中英

如果突破内部if,为什么我的消息打印两次?

[英]Why is my message printing twice when I break out of the inner if?

我的代码有一点问题。 编译和运行效果很好,但是,当我尝试突破内循环时,

System.out.println("Type which category you want to add to.");
System.out.println("Homework, Classwork, Labs, Test, Quizzes, Midterm, Final");

当我只希望它打印一次时,上面的代码将两次打印到终端。

我感觉这是我的括号对齐方式的一个简单错误,但是我很难弄清楚该如何做。 任何帮助将不胜感激!

import java.util.Scanner;

public class GetGrade {
    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        final int MAX = 15;

        int[] homework = new int[MAX];
        int[] classwork = new int[MAX];
        int[] lab = new int[MAX];
        int[] test = new int[MAX];
        int[] quizzes = new int[MAX];
        int[] midterm = new int[MAX];
        int[] fin = new int[MAX];
        int hwCount, clCount, labCount, testCount, quizCount, midCount, finCount;
        double hwTotal, clTotal, labTotal, testTotal, quizTotal, midTotal, finTotal;
        double grade = 0;

        String selection = "";

        System.out.println();
        System.out.println("Welcome to GetGrade!");
        System.out.println();

        while (true) {

            System.out.println("Type which category you want to add to.");
            System.out.println("Homework, Classwork, Labs, Test, Quizzes, Midterm, Final");
            selection = input.nextLine();

            if (selection.equals("homework")) {

                System.out.print("What percentange of your grade is homework? > ");
                double hwPercent = input.nextDouble();
                System.out.println("Now begin typing your grades. When you are finished, type -1.");

                for (int i = 0; i < homework.length; i++) {
                    homework[i] = input.nextInt();
                    hwTotal = homework[i] * hwPercent;
                    grade += hwTotal;
                    if (homework[i] == -1) break;
                }
            }
        }
    }
}

它看起来很简单:
调用input.nextInt(); 在您的内部循环中不包括换行符。 因此,您正在破坏内部循环,接收仅包含换行符的下一行-input.nextLine();中的字符; 这是您的“ -1 \\ n”行的其余输入,并由于与“作业”不匹配而再次进入主循环。

尝试将while循环中的条件变量设置为实际的布尔值,而不是true。

同样,当您调用“ break”时,您只会脱离for循环。 如果此时将布尔变量重新分配为false,则将完全退出while循环。

在while循环结束之前,添加“是否要继续?(是/否)”功能。

如果用户输入“ N”或其他,则执行另一个中断。 这种中断将使您摆脱while循环。

使代码正常工作的简单方法是更改

selection = input.nextLine();

selection = input.next();

next()仅读取字符串值(这是您在代码中实际执行的操作),而不是彼得建议的newline

因此,当您读取newline时,while的额外迭代不会发生。

当您使用扫描仪从键盘上读取一行时,它会读取用户键入并提交其输入之前的所有内容,包括换行符。 因此,例如:

Type which category you want to add to.
Homework, Classwork, Labs, Test, Quizzes, Midterm, Final
>

如果键入“ homework”,然后按Enter,则实际输入将变为“ homework \\ n”。 input.nextLine()将扫描输入,直到遇到第一个换行符'\\ n',它将消耗该字符,然后返回到该点为止的所有内容(即“作业”)。

您的问题是input.nextInt()不会占用换行符,因此, while循环开始另一回合时,输入缓冲区中仍然存在换行符。

Now begin typing your grades. When you are finished, type -1.
> ...
> -1
      => User input is "-1\n"
-------------------------------

// Meanwhile, back in the code...
for (int i=0;i<homework.length;i++) {
   homework[i] = input.nextInt();      // <--- This call consumes "-1" but leaves '\n'
   hwTotal = homework[i] * hwPercent;
   grade += hwTotal;
   if (homework[i] == -1) break; 
}

下一个对input.nextLine()调用将占用该换行符,而输入缓冲区为空。

while (true) {
    System.out.println("Type which category you want to add to.");
    System.out.println("Homework, Classwork, Labs, Test, Quizzes, Midterm, Final");
    selection = input.nextLine();    // <--- This call consumes the leftover '\n' and returns the empty string
    ...

而且,由于“”不等于“作业”,因此while循环又进行了大约input.nextLine() ,但是这次输入缓冲区为空,因此对input.nextLine()的调用的行为与您期望的一样。

// selection is empty, so this condition fails and the program loops back around
if (selection.equals("homework")) {
    ...

有两个简单的解决方案。 您可以

  • 使用Integer.parseInt(input.nextLine())代替input.nextInt()
  • while循环的结尾添加对input.nextLine()的额外调用以消耗最后的换行符

第一个选项可能是最健壮的,如果它们没有给您提供有效的整数作为输入,则会获得运行时错误的额外好处。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM