Java 程序将无法识别标记值
Java program will not recognize sentinel value
我的程序接受来自用户的输入数据(最多 20 个值)并计算 average/find 与平均值的距离。如果用户在尚未添加任何数字时输入“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);
}
}
}
您可以不执行 do-while 而是执行 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;
希望这对您有所帮助:)
我的程序接受来自用户的输入数据(最多 20 个值)并计算 average/find 与平均值的距离。如果用户在尚未添加任何数字时输入“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);
}
}
}
您可以不执行 do-while 而是执行 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;
希望这对您有所帮助:)