【发布时间】:2020-09-30 16:22:52
【问题描述】:
我目前正在尝试学习 java 教科书中的一个示例,但是我在 EoF 语句之后的代码会被编译器忽略。
package lettergrades;
import java.util.Scanner;
public class LetterGrades {
public static void main(String[] args) {
int total = 0;
int gradeCounter = 0;
int aCount = 0;
int bCount = 0;
int cCount = 0;
int dCount = 0;
int fCount = 0;
Scanner input = new Scanner(System.in);
System.out.printf("%s%n%s%n %s%n %s%n", "Enter the integer grades in the range 0-100", "Type the end-of-file indicator to terminate input", "on unix type <ctrl> d then press Enter","On windows type <Ctrl> z then press enter");
while (input.hasNext()){
int grade = input.nextInt();
total += grade;
++gradeCounter;
switch (grade/10){
case 9:
case 10:
++aCount;
break;
case 8:
++bCount;
break;
case 7:
++cCount;
break;
case 6:
++dCount;
break;
default:
++fCount;
break;
}
}
System.out.printf("%nGrade Report:%n");
if (gradeCounter !=0){
double average = (double) total/gradeCounter;
System.out.printf("total of the %d grades entered is %d%n", gradeCounter, total);
System.out.printf("Class average is %.2f%n", average);
System.out.printf("%n%s%n%s%d%n%s%d%n%s%d%n%s%d%n%s%d%n", "Number of students that received each grade","A: ", aCount, "B: ", bCount , "C: ", cCount, "D: ", dCount, "F: ", fCount);
}
else
System.out.println("No grades were entered");
}
}
这是我得到的输出:
Enter the integer grades in the range 0-100
Type the end-of-file indicator to terminate input
on unix type <ctrl> d then press Enter
On windows type <Ctrl> z then press enter
但是当我输入 ctrl D 并按回车时,什么也没有发生。为什么 printf 和 if 语句不起作用?
【问题讨论】:
-
对我来说似乎没问题。
-
Windows 也使用 control-d 表示 eof。
-
这对我来说很好。我使用的是 Windows 和 BlueJ。我使用 control-d 退出。
-
我相信linux上的control-z是用来把当前进程放到后台的。