各位高手,小弟今天写了一段代码: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则继续循环,谁知道代码好像直接结束了循环,然后就输出结果。请问各位高手,问题出在哪里?

解决方案 »

  1.   

    你循环放的位置不对。
    第二次输入数字的时候,因为不是yes所以就直接跳出循环了。
    而且逻辑有些乱。
      

  2.   

    System.out.println("Do you want to input another ?(yes\\no):"); 
    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.
      

  3.   

    我修改了一下,你看看。public static void main(String[] args) {
    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.
      

  4.   


    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); }}
      

  5.   


    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.
    }