我正在努力学习java。我正在尝试制作一个程序,计算一组学生通过测试的分数,输出输入的总分数,分数高于69的通过测试的次数,并显示通过测试的百分比。
我遇到的问题是,我似乎无法正确获得输出的百分比。它一直显示0.0。以下是我目前为止想出的最好的代码。
嵌套while循环是一种好的编码方式吗?缩短我的程序有更简单的方法吗?谢谢
import java.util.Scanner;
import java.text.DecimalFormat;
public class CountPassingScores {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
// Formats the percentage output to one decimal place.
DecimalFormat df = new DecimalFormat("###,##0.0");
// counts how many times a score is entered. Passing score is not
// considered here.
int count = 0;
// The score the user enters.
int score = 0;
// percent of the class that passed the test. Passing score is 70 and
// above.
double percentOfClassPassed = 0.0;
// total number of tests passed. Passing score is 70 and above.
int numberOfTestsPassed = 0;
System.out.println("This program counts the number of passing "
+ "test scores. (-1 to quit)\n");
while (score != -1) {
System.out.print("Enter the first test score: ");
score = scan.nextInt();
while (count != -1 && score > 0) {
System.out.print("Enter the next test score: ");
score = scan.nextInt();
count++;
if (count == -1)
break;
else if (score > 69)
numberOfTestsPassed++;
percentOfClassPassed = (numberOfTestsPassed / count);
}
}
System.out.println("\nYou entered " + count + " scores.");
System.out.println("The number of passing test scores is "
+ numberOfTestsPassed + ".");
System.out.println(df.format(percentOfClassPassed)
+ "% of the class passed the test.");
}
}
这是因为您正在将int
与int
分开。这将只导致int
。
要获得正确的结果,请将任意一个强制转换为double
。
percentOfClassPassed = ((double) numberOfTestsPassed / count);
您的代码不考虑92作为测试通过的分数,因为您没有在第一个while循环中增加TestBuffStaseDebug的值。以下是我在您的代码片段中所做的一些更改:
while (score != -1) {
System.out.print("Enter the first test score: ");
score = scan.nextInt();
if(score > 69)
numberOfTestsPassed++;
while (count != -1 && score > 0) {
System.out.print("Enter the next test score: ");
score = scan.nextInt();
count++;
if (score == -1)
break;
else if (score > 69)
numberOfTestsPassed++;
}
percentOfClassPassed = ((double)numberOfTestsPassed * 100 / count);
}
它为所有输入提供正确的输出。
我用For创建了一个嵌套循环,这是程序代码和输出,然后我尝试了同时循环,得到了不同的结果 对于 虽然 请引导我。。谢谢
大家好,初学者, 被困在这里一段时间了。故障排除我要么得到一个无限循环,要么它只循环11次(我希望外部循环21次)。基本上,我读这是因为第一个循环将被执行,因为它将是真的,第二个循环将被执行十次,直到它不是真的。然后外部循环将绕过内部循环继续运行(因为条件不再有效),直到外部条件不再为真。 提前道谢!
我正在编写一个程序,用户输入一个班级中的学生人数、该班级参加的考试次数,然后输入每个学生的姓名和考试分数。然后,程序计算学生的成绩,并分配给他们一个相应的字母等级。然后最后,它将他们的分数添加到一个classSum,计算平均类分数并显示出来。 这就是我目前所拥有的:public class GradeCalculator{ 这是我得到的输出: 正如您所看到的,在第一个学生之后,输入姓名和考试分数并
本文向大家介绍在Python的while循环中使用else以及循环嵌套的用法,包括了在Python的while循环中使用else以及循环嵌套的用法的使用技巧和注意事项,需要的朋友参考一下 循环使用 else 语句 在 python 中,for … else 表示这样的意思,for 中的语句和普通的没有区别,else 中的语句会在循环正常执行完(即 for 不是通过 break 跳出而中断的)的情况
只要给定条件为真,Perl编程语言中的while循环语句就会重复执行目标语句。 语法 (Syntax) Perl编程语言中while循环的语法是 - while(condition) { statement(s); } 这里的statement(s)可以是单个陈述或一个陈述块。 condition可以是任何表达。 当条件为真时,循环迭代。 当条件变为假时,程序控制将立即传递到循环之后的行。
编写程序时,您可能会遇到需要反复执行操作的情况。 在这种情况下,您需要编写循环语句以减少行数。 JavaScript支持所有必要的循环,以减轻编程压力。 while循环 JavaScript中最基本的循环是while循环,将在本章中讨论。 while循环的目的是只要expression为真,就重复执行语句或代码块。 表达式变为false,循环终止。 流程图 while loop流程图如下 - 语法