Java 如何使无效数字不添加到最终计算和计数器合计中?
当我输入一个有非法分数提示的数字时,我的问题出现了,它仍然会将一个数字添加到我的计数器中,并将该数字添加到总数中,这样平均值就会被忽略。我一直在绞尽脑汁,想尽一切办法想弄明白。代码有点草率,因为我还没有清理它,我只是想先把运动部件整理好Java 如何使无效数字不添加到最终计算和计数器合计中?,java,Java,当我输入一个有非法分数提示的数字时,我的问题出现了,它仍然会将一个数字添加到我的计数器中,并将该数字添加到总数中,这样平均值就会被忽略。我一直在绞尽脑汁,想尽一切办法想弄明白。代码有点草率,因为我还没有清理它,我只是想先把运动部件整理好 public class StudentSentinel { public static void main(String[] args) { double score; double total = 0.0; double ave
public class StudentSentinel {
public static void main(String[] args) {
double score;
double total = 0.0;
double average;
int scoreCount = 0;
// create the Scanner object. Name it stdin
Scanner stdin = new Scanner(System.in);
// title at the top of the output
System.out.println(" student score report");;
// read the first score
System.out.printf("Enter a score (1-100, -1 to quit)" +
": ", scoreCount);
score = stdin.nextDouble();
scoreCount++;
while (score != -1.0) {
total += score;
System.out.printf("Enter a score (1-100, -1 to quit)" +
": ", scoreCount);
scoreCount++;
score = stdin.nextDouble();
if (score < -1)
System.out.println("Illegal score. Try again");
else if (score > 100) {
System.out.println("Illegal score. Try again");
}
// increment the loop counter
}
// end of for loop
average = total / scoreCount;
System.out.printf("\nThe average score for %d students is %8.2f\n",
scoreCount, average);
} // end of main
} // end of class definition
公共班级学生哨兵{
公共静态void main(字符串[]args){
双倍得分;
双倍合计=0.0;
双倍平均;
积分计数=0;
//创建扫描仪对象。将其命名为stdin
扫描仪标准输入=新扫描仪(System.in);
//输出顶部的标题
System.out.println(“学生成绩报告”);;
//读第一乐谱
System.out.printf(“输入分数(1-100,-1表示退出)”+
“:”,计分);
分数=stdin.nextDouble();
scoreCount++;
而(分数!=-1.0){
总分+=总分;
System.out.printf(“输入分数(1-100,-1表示退出)”+
“:”,计分);
scoreCount++;
分数=stdin.nextDouble();
如果(分数<-1)
System.out.println(“非法分数。重试”);
否则,如果(分数>100){
System.out.println(“非法分数。重试”);
}
//递增循环计数器
}
//循环结束
平均值=总数/分数;
System.out.printf(“\n%d名学生的平均分数为%8.2f\n”,
分数(平均);
}//主管道末端
}//类定义结束
首先检查分数是否合法,然后增加计数器并将其添加到您的总数中。您还可以分配分数
,并通过单个操作检查该分数是否为-1
。并始终使用大括号(即使它们是可选的)。像
//读第一个分数
System.out.printf(“输入分数(1-100,-1表示退出)”+“:”,scoreCount);
而((分数=stdin.nextDouble())!=-1.0){
如果(分数<-1 | |分数>100){
System.out.println(“非法分数。重试”);
}否则{
scoreCount++;
总分+=总分;
}
System.out.printf(“输入分数(1-100,-1表示退出)”+“:”,scoreCount);
}
首先检查分数是否合法,然后增加计数器并将其添加到您的总数中。您还可以分配分数
,并通过单个操作检查该分数是否为-1
。并始终使用大括号(即使它们是可选的)。像
//读第一个分数
System.out.printf(“输入分数(1-100,-1表示退出)”+“:”,scoreCount);
而((分数=stdin.nextDouble())!=-1.0){
如果(分数<-1 | |分数>100){
System.out.println(“非法分数。重试”);
}否则{
scoreCount++;
总分+=总分;
}
System.out.printf(“输入分数(1-100,-1表示退出)”+“:”,scoreCount);
}
请尝试此代码。这对我很有用:
public class StudentSentinel {
public static void main(String[] args) {
double score;
double total = 0.0;
double average;
int scoreCount = 0;
// create the Scanner object. Name it stdin
Scanner stdin = new Scanner(System.in);
// title at the top of the output
System.out.println(" student score report");;
// read the first score
System.out.printf("Enter a score (1-100, -1 to quit)" +
": ", scoreCount);
score = stdin.nextDouble();
scoreCount++;
total += score;
while (score != -1.0) {
System.out.printf("Enter a score (1-100, -1 to quit)" +
": ", scoreCount);
scoreCount++;
score = stdin.nextDouble();
if (score < -1) {
System.out.println("Illegal score. Try again");
continue;
}else if (score > 100) {
System.out.println("Illegal score. Try again");
continue;
}
total += score;
// increment the loop counter
}
// end of for loop
average = total / scoreCount;
System.out.printf("\nThe average score for %d students is %8.2f\n",
scoreCount, average);
} // end of main
公共班级学生哨兵{
公共静态void main(字符串[]args){
双倍得分;
双倍合计=0.0;
双倍平均;
积分计数=0;
//创建扫描仪对象。将其命名为stdin
扫描仪标准输入=新扫描仪(System.in);
//输出顶部的标题
System.out.println(“学生成绩报告”);;
//读第一乐谱
System.out.printf(“输入分数(1-100,-1表示退出)”+
“:”,计分);
分数=stdin.nextDouble();
scoreCount++;
总分+=总分;
而(分数!=-1.0){
System.out.printf(“输入分数(1-100,-1表示退出)”+
“:”,计分);
scoreCount++;
分数=stdin.nextDouble();
如果(分数<-1){
System.out.println(“非法分数。重试”);
继续;
}否则,如果(分数>100){
System.out.println(“非法分数。重试”);
继续;
}
总分+=总分;
//递增循环计数器
}
//循环结束
平均值=总数/分数;
System.out.printf(“\n%d名学生的平均分数为%8.2f\n”,
分数(平均);
}//主管道末端
}请尝试此代码。这对我很有用:
public class StudentSentinel {
public static void main(String[] args) {
double score;
double total = 0.0;
double average;
int scoreCount = 0;
// create the Scanner object. Name it stdin
Scanner stdin = new Scanner(System.in);
// title at the top of the output
System.out.println(" student score report");;
// read the first score
System.out.printf("Enter a score (1-100, -1 to quit)" +
": ", scoreCount);
score = stdin.nextDouble();
scoreCount++;
total += score;
while (score != -1.0) {
System.out.printf("Enter a score (1-100, -1 to quit)" +
": ", scoreCount);
scoreCount++;
score = stdin.nextDouble();
if (score < -1) {
System.out.println("Illegal score. Try again");
continue;
}else if (score > 100) {
System.out.println("Illegal score. Try again");
continue;
}
total += score;
// increment the loop counter
}
// end of for loop
average = total / scoreCount;
System.out.printf("\nThe average score for %d students is %8.2f\n",
scoreCount, average);
} // end of main
公共班级学生哨兵{
公共静态void main(字符串[]args){
双倍得分;
双倍合计=0.0;
双倍平均;
积分计数=0;
//创建扫描仪对象。将其命名为stdin
扫描仪标准输入=新扫描仪(System.in);
//输出顶部的标题
System.out.println(“学生成绩报告”);;
//读第一乐谱
System.out.printf(“输入分数(1-100,-1表示退出)”+
“:”,计分);
分数=stdin.nextDouble();
scoreCount++;
总分+=总分;
而(分数!=-1.0){
System.out.printf(“输入分数(1-100,-1表示退出)”+
“:”,计分);
scoreCount++;
分数=stdin.nextDouble();
如果(分数<-1){
System.out.println(“非法分数。重试”);
继续;
}否则,如果(分数>100){
System.out.println(“非法分数。重试”);
继续;
}
总分+=总分;
//递增循环计数器
}
//循环结束
平均值=总数/分数;
System.out.printf(“\n%d名学生的平均分数为%8.2f\n”,
分数(平均);
}//主管道末端
}非常感谢,这很有意义,也很有帮助。非常感谢,这很有意义,也很有帮助。我保存了这封信以供参考。谢谢你抽出时间好的@Moca,试试看,让我知道这是否对你有用。谢谢。如果我想添加一个部件,用户可以在循环开始之前输入一个数字,然后在进行任何计算之前结束循环。你可以在这之前加上吗?你能详细说明一下吗,你到底想做什么?如果你发现以前的答案有用