如何使用CTRL D中断while循环 [英] How do I break out of a while loop with CTRL D
问题描述
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package lettergrades;
import java.util.Scanner;
/**
*
* @author user1
*/
public class LetterGrades {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
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/LINUX/MACOS type <CMD> d then press Enter",
"On WINDOWS type <CTRL> z then press Enter");
do
{
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;
}
} while(input.hasNext());
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 who recieved each grade:",
"A: ",aCount,
"B: ",bCount,
"C: ",cCount,
"D: ",dCount,
"F: ",fCount);
}
else
System.out.println("No grades were entered...");
}
}
我尝试过:
我已经用JAVA程序编写了这个例子。我一直试图弄清楚为什么CTRL D不会结束循环并继续使用System.out.printf(%nGrade Report:%n);.我尝试了不同的循环变体,它只是不起作用。如果我在循环中包含输出它可以工作,但理想情况下它只在循环外放置一次。我知道还有其他方法可以做到这一点,但我希望按照作者的意图使用它。预先感谢您的帮助。
What I have tried:
Hi, I have written this example out of JAVA HOW TO PROGRAM. I have been trying to figure out why CTRL D does not end the loop and go on to System.out.printf("%nGrade Report:%n");. I have tried different variations of loops and it just does not work. If I include the output in the loop it works, but ideally it would only run once when placed outside the loop. I know there are other ways to do this, but I would like to get this working the way the Author intended. Thanks in advance for any help.
推荐答案
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package lettergrades;
import java.util.Scanner;
/**
*
* @author user1
*/
public class LetterGrades {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
String keepGoing = "yes";
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 ",
"Enter the integer grades in the range 0-100",
"Enter 0 to end the program");
do
{
int grade = input.nextInt();
total += grade;
++ gradeCounter;
if(grade != 0)
{
switch(grade/10)
{
case 9:
case 10:
++aCount;
break;
case 8:
++bCount;
break;
case 7:
++cCount;
break;
case 6:
++dCount;
break;
default:
++fCount;
break;
}
}
else
{
keepGoing = "no";
break;
}
} while(keepGoing.equals("yes"));
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 who recieved each grade:",
"A: ",aCount,
"B: ",bCount,
"C: ",cCount,
"D: ",dCount,
"F: ",fCount);
}
else
System.out.println("No grades were entered...");
}
}
这篇关于如何使用CTRL D中断while循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!