我的代码有点问题。编译和运行效果很好,但是,当我试图打破内部循环,为什么我的信息打印两次,当我脱离内心如果?
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;
}
}
}
}
}
如果您的while循环运行正确,则条件可能为真两次。向你的代码添加断点,它会告诉你为什么它运行两次。 –
你的代码应该做它正在做的事情,而不是你认为它应该做的事情。使用调试器的功能 –
何时打印出两行打印两行?如果选择等于家庭作业,或者在时间之外,你是否需要他们? –