2015-07-01 39 views
0

我的代码有点问题。编译和运行效果很好,但是,当我试图打破内部循环,为什么我的信息打印两次,当我脱离内心如果?

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; 
       } 
      } 
     } 
    } 
} 
+0

如果您的while循环运行正确,则条件可能为真两次。向你的代码添加断点,它会告诉你为什么它运行两次。 –

+0

你的代码应该做它正在做的事情,而不是你认为它应该做的事情。使用调试器的功能 –

+0

何时打印出两行打印两行?如果选择等于家庭作业,或者在时间之外,你是否需要他们? –

回答

1

它看起来很平凡:
对input.nextInt()的调用;在你的内部循环中不包含换行符。 所以你打破了innerloop,接收只包含换行符的下一行 - input.nextLine()中的字符;这是你的“-1 \ n”行的剩余输入,并再次进入主循环,因为它与“作业”不匹配。

+0

完美!从来没有发现过我自己的!非常感谢! –

0

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

另外,当你调用“break”时,你只能跳出for循环。如果您在此时重新分配布尔变量为false,则会完全退出while循环。

+0

我有,我添加while(!selection.equals(“退出”)),而不是'真正',它仍然不起作用 –

0

就在循环结束之前,添加一个“你想继续?(是/否)”功能。

如果用户输入“N”或其他任何内容,请执行另一个中断。那个休息会让你离开while循环。

0

简单的方法让你的代码工作是改变

selection = input.nextLine(); 

selection = input.next(); 

next()在一个字符串值只读取(这是你实际上是在做你的代码是什么),而不是newline字符彼得建议。

因此,当阅读newline字符时,不会发生额外的迭代。

0

当您使用扫描仪从键盘读取一条线时,它会读取所有最多以及包含用户键入的换行符以提交其输入。例如:

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

如果您输入“家庭作业”,然后回车,实际输入将变为“家庭作业\ 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()的行为如你所愿。

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

有两个简单的解决方案来解决这个问题。您可以

  • 使用Integer.parseInt(input.nextLine())而不是input.nextInt()
  • 添加一个额外的呼叫input.nextLine()while循环结束消耗最后的换行符

第一个选项可能是最强大的,如果它们没有给你一个有效的整数作为输入,你会得到运行时错误的额外好处。

相关问题