各位高手,小弟今天写了一段代码:import java.util.*;public class CalculateTheResult{ public static void main(String[] args){
Scanner keyboard=new Scanner(System.in); int max=0,min=0,next=0,total=0; int count=1; System.out.print("Start the calculation?(yes\\no):"); String firstRequest=keyboard.nextLine(); String request=firstRequest; while(request.equalsIgnoreCase("yes")){
System.out.println("Enter a exam of a student's:"); next=keyboard.nextInt(); if(count==1){
max=next; min=next; total=next;
}else{
if(next>max){max=next;} if(next<min){min=next;} total=total+next;
} count++; System.out.println("Do you want to input another ?(yes\\no):"); request=keyboard.nextLine();
}//while loop ends here. double average=total/count; System.out.println("The max result is: "+max); System.out.println("The min result is: "+min); System.out.println("The average result is: "+average); }//main method ends here.}//class ends here.编译通过,但是在运行的时候发现逻辑流程不是我想象中的那样(命令提示符的显示如下):Start the calculation?(yes\no):yes(用户输入)
Enter a exam of a student's:
85(用户输入)
Do you want to input another ?(yes\no):
The max result is:85
The min result is:85
The average result is:42.0我本意是询问用户是否要继续输入,如果在"Do you want to input another (yes\no):"那里输入yes则继续循环,谁知道代码好像直接结束了循环,然后就输出结果。请问各位高手,问题出在哪里?
Scanner keyboard=new Scanner(System.in); int max=0,min=0,next=0,total=0; int count=1; System.out.print("Start the calculation?(yes\\no):"); String firstRequest=keyboard.nextLine(); String request=firstRequest; while(request.equalsIgnoreCase("yes")){
System.out.println("Enter a exam of a student's:"); next=keyboard.nextInt(); if(count==1){
max=next; min=next; total=next;
}else{
if(next>max){max=next;} if(next<min){min=next;} total=total+next;
} count++; System.out.println("Do you want to input another ?(yes\\no):"); request=keyboard.nextLine();
}//while loop ends here. double average=total/count; System.out.println("The max result is: "+max); System.out.println("The min result is: "+min); System.out.println("The average result is: "+average); }//main method ends here.}//class ends here.编译通过,但是在运行的时候发现逻辑流程不是我想象中的那样(命令提示符的显示如下):Start the calculation?(yes\no):yes(用户输入)
Enter a exam of a student's:
85(用户输入)
Do you want to input another ?(yes\no):
The max result is:85
The min result is:85
The average result is:42.0我本意是询问用户是否要继续输入,如果在"Do you want to input another (yes\no):"那里输入yes则继续循环,谁知道代码好像直接结束了循环,然后就输出结果。请问各位高手,问题出在哪里?
解决方案 »
- 关于JTable单元格渲染器的问题,请高手帮忙解决,谢谢
- 这段关于多线程的代码,是本组其它人贴子上的,不是很理解.
- C++和JAVA中一个算术运算的问题(返回值居然不一样!!)
- 如何在JFrame里做一个相册?
- 如何 利用java实现 Mail的发信? 如果有例子,讲解以下。
- 怎么使用jdk来创建一个在Linux下有执行权限的文件呢?
- 散分咯!!!!答对一个给20分!!!!(多选题)
- <JAVA编程思想》里的一段代码,按照书的指示完整的打上去,可竟然发现了20个ERROR!
- 为何会有这种从未见过的错误发生!!
- 一个简单的小问题和一个困难的大问题请大家帮忙!!!急急急
- 如何动态加载jar包?
- JAVA中混频器
第二次输入数字的时候,因为不是yes所以就直接跳出循环了。
而且逻辑有些乱。
request=keyboard.nextLine(); //之前你用了keyboard.nextInt(),它只分离出了int,好像把回车符留了下来
request=keyboard.nextLine(); //再多读一次,就OK了
}//while loop ends here. 加多一个.nextLine()后的运行情况如下:
Start the calculation?(yes\no):yes
Enter a exam of a student's:
60
Do you want to input another ?(yes\no):
yes
Enter a exam of a student's:
70
Do you want to input another ?(yes\no):
yes
Enter a exam of a student's:
80
Do you want to input another ?(yes\no):
no
The max result is: 80
The min result is: 60
The average result is: 52.0Process completed.
Scanner keyboard = new Scanner(System.in);
int max = 0, min = 0, next = 0, total = 0;
int count = 1;
String request = null;
do {
System.out.println("Enter a exam of a student's:");
next = Integer.parseInt(keyboard.nextLine());
if (count == 1) {
max = next;
min = next;
total = next;
} else {
if (next > max) {
max = next;
}
if (next < min) {
min = next;
}
total = total + next;
}
count++;
System.out.println("Do you want to input another ?(yes\\no):");
request = keyboard.nextLine();
} while (request.equalsIgnoreCase("yes"));// while loop ends here.
double average = total / count;
System.out.println("The max result is: " + max);
System.out.println("The min result is: " + min);
System.out.println("The average result is: " + average);
}// main method ends here.
import java.util.*;public class CalculateTheResult { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); int max = 0, min = 0, next = 0, total = 0; int count = 0; System.out.print("Start the calculation?(yes\\no):"); String firstRequest = keyboard.nextLine(); String request = firstRequest; while (request.equalsIgnoreCase("yes")) { System.out.println("Enter a exam of a student's:"); next = keyboard.nextInt(); if (count == 0) { max = next; min = next; total = next; } else { if (next > max) {
max = next;
} if (next < min) {
min = next;
} total = total + next; } count++; System.out.println("Do you want to input another ?(yes\\no):");
request = keyboard.next();
} double average = total / count; System.out.println("The max result is: " + max); System.out.println("The min result is: " + min); System.out.println("The average result is: " + average); }}
package org.leelin.demo;import java.util.*;public class CalculateTheResult { public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
int max = 0, min = 0, next = 0, total = 0;
int count = 1;
System.out.println("Enter the first exam of a student's:exit with '-1'");
next = keyboard.nextInt();
max = next;
min = next;
total = next;
while (next != -1) {
{
if (next > max) {
max = next;
} else if (next < min) {
min = next;
}
total = total + next;
}
count++;
System.out.println("Enter next exam of a student's:");
next = keyboard.nextInt();
}// while loop ends here.
double average = total / count;
System.out.println("The max result is: " + max);
System.out.println("The min result is: " + min);
System.out.println("The average result is: "+average); }// main method ends here.
}