为什么在文件结束语句后我的代码被忽略了?

huangapple go评论54阅读模式
英文:

Why gets my code after end of file statement ignored?

问题

我目前正在尝试我的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("请输入在0-100范围内的整数成绩%n输入文件结束指示符以终止输入%n在Unix上输入<ctrl>d然后按Enter键%n在Windows上输入<Ctrl>z然后按Enter键%n");
    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("%n成绩报告:%n");
    if (gradeCounter != 0){
        double average = (double) total/gradeCounter;
        System.out.printf("输入的%d个成绩的总和是%d%n班级平均分是%.2f%n", gradeCounter, total, average);
        System.out.printf("%n%s%n%s%d%n%s%d%n%s%d%n%s%d%n%s%d%n", "每个等级的学生人数","A: ", aCount, "B: ", bCount , "C: ", cCount, "D: ", dCount, "F: ", fCount);

    }
    else 
        System.out.println("没有输入成绩");

}

}
这是我得到的输出

     请输入在0-100范围内的整数成绩
    
     输入文件结束指示符以终止输入
    
     在Unix上输入<ctrl>d然后按Enter键
    
     在Windows上输入<Ctrl>z然后按Enter键

但是当我输入ctrl D并按下Enter键时什么都不会发生为什么printf和if语句不起作用呢
英文:

I'm currently trying an example from my textbook on learning java, but my code after an EoF statement just gets ignored by the compiler.

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(&quot;%s%n%s%n %s%n %s%n&quot;, &quot;Enter the integer grades in the range 0-100&quot;, &quot;Type the end-of-file indicator to terminate input&quot;, &quot;on unix type &lt;ctrl&gt; d then press Enter&quot;,&quot;On windows type &lt;Ctrl&gt; z then press enter&quot;);
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(&quot;%nGrade Report:%n&quot;);
if (gradeCounter !=0){
double average = (double) total/gradeCounter;
System.out.printf(&quot;total of the %d grades entered is %d%n&quot;, gradeCounter, total);
System.out.printf(&quot;Class average is %.2f%n&quot;, average);
System.out.printf(&quot;%n%s%n%s%d%n%s%d%n%s%d%n%s%d%n%s%d%n&quot;, &quot;Number of students that received each grade&quot;,&quot;A: &quot;, aCount, &quot;B: &quot;, bCount , &quot;C: &quot;, cCount, &quot;D: &quot;, dCount, &quot;F: &quot;, fCount);
}
else 
System.out.println(&quot;No grades were entered&quot;);
}
}

This is the output i get :

 Enter the integer grades in the range 0-100
Type the end-of-file indicator to terminate input
on unix type &lt;ctrl&gt; d then press Enter
On windows type &lt;Ctrl&gt; z then press enter

But when i input ctrl D and press enter, nothing happens. Why don't the printf and if statements work?

答案1

得分: 1

正如@FredLarson所报告的,对大多数人来说,它运行良好。您依赖EOF字符导致sysin被关闭,这将导致扫描器的hasNext()返回false。

显然,在您的系统上,这不起作用。如果您在IDE的“控制台”内运行此代码,通常它们不会让您关闭sysin,或者与命令行不同。

您可以尝试找出哪个神秘的按键组合可以结束输入,但也有另一种选择。

除了EOF之外,或者除了EOF之外,制定另一个符号来结束输入。也许是0,或者'END'。在后一种情况下,您不能再依赖nextInt,您将不得不调用next,检查它是否是END,如果是,停止接受输入,如果不是,则通过Integer.parseInt将其转换为数值。

现在,您不再需要在控制台消息中提及各种平台,也可以避免这些问题。

英文:

As e.g. @FredLarson is reporting, it's working fine for most. You're relying on EOF character resulting in sysin being closed, which will in turn cause scanner's hasNext() to return false.

Evidently, on your system, that isn't working. If you're running this inside an IDE's 'console', they often don't let you close sysin or work differently from the command line.

You can try to figure out which voodoo key combo ends things, but there is an alternative.

Instead of EOF, or in addition to EOF, make another symbol that ends inputs. Perhaps 0, or 'END'. In the later case you can no longer rely on nextInt, you'd have to call next, check if it is END, if yes, stop accepting input, and if not, toss it through Integer.parseInt to end up with a numeric value.

Now you no longer have to mention various platforms in your console messages, and you avoid these issues.

huangapple
  • 本文由 发表于 2020年10月1日 00:22:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/64141764.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定