Java程序不会识别sentinel值

时间:2016-10-06 01:03:55

标签: java arrays for-loop do-while sentinel

我的程序接受来自用户的输入数据(最多20个值)并计算平均值/找到与平均值的距离。如果用户输入" 9999"当没有添加数字时,它将显示错误消息并告诉用户重新输入值。否则输入" 9999"将收集用户输入的内容并进行计算。我的程序必须收集用户的所有20个输入,并且当值" 9999"完全输入,但它会正确地进行其他计算。我不确定为什么它不承认我的哨兵价值。

package labpack;

import java.util.Scanner;

public class Lab4 {

    public static void main(String[] args) {
        int i = 0;
        double [] numbers = new double[20];
        double sum = 0;
        int sentValue = 9999;

        java.util.Scanner input = new java.util.Scanner(System.in);

        System.out.print("Enter the numbers you want up to 20");

        do {
            for (i = 0; i < numbers.length; i++) { 
                if (numbers[0] == sentValue){
                    System.out.println("Error: Please enter a number");
                    break;
                }
                else {
                    numbers[i] = input.nextDouble(); 
                    sum += numbers[i];
                }
            } 

            while (i<numbers.length && numbers[i]!=sentValue); //part of do-while loop

            //calculate average and distance from average
            double average = (sum / i);
            System.out.println("This is your average:" + average);

            for (i = 0; i < numbers.length; i++) { //Display for loop
                double diffrence = (average-numbers[i]);
                System.out.println("This is how far number " +numbers[i] +" is from the average:" + diffrence);
            }
        }
    }

1 个答案:

答案 0 :(得分:1)

您可以在不执行do-while而是执行此操作的情况下执行此操作。

if (numbers[0]== sentValue){
            System.out.println("Error: Please enter a number");
            break;

在这里,您尝试在不使用用户输入初始化数组的情况下比较该值。

这可以通过一种非常简单的方式完成:

import java.util.Scanner;
public class Lab4 {

    public static void main(String[] args) {
        int i = 0;
        double [] numbers =new double[10];
        double sum =0;
        double sentValue=9999;
        int count = 0;
        System.out.println(numbers.length);
        System.out.print("Enter the numbers you want up to 20");
        Scanner input = new Scanner(System.in);
        while (i<numbers.length){
            double temp = input.nextDouble();
            if (temp >= sentValue){
                if(i==0){
                    System.out.println("Error Message Here");
                } else {
                    break;
                }
            }//if
            else {
                numbers[i] = temp;
                sum += numbers[i];
                i++;
                count++;
            }
        } //part of while loop*/

        //calculate average and distance from average
        double average=(sum/i);
        System.out.println("This is your average:" + average);

        for (i=0;i < count;i++){ //Display for loop
            double diffrence = (average-numbers[i]);
            System.out.println("This is how far number " +numbers[i] +" is from the average:" + diffrence);
        }//for loop
    }//main bracket

}//class lab4 bracket

您需要将 input.nextDouble()的值存储到变量中,因为当编译器读取 input.nextDouble()时,每次它都会询问用户输入。

PS。你不需要重新初始化这个部分:

java.util.Scanner input = new java.util.Scanner(System.in);

上述行可以简单地写成:

Scanner input = new Scanner(System.in);

因为您已经导入了扫描仪。

import java.util.Scanner;

希望这会有所帮助:)