我们正在使用嵌套循环,它向我们表明我们尚未初始化变量。
我们尝试在循环内外进行变量初始化,但它表示变量尚未初始化或已经初始化。
import java.util.Scanner;
public class LargeSmallAverage
{
public static void main(String[]args)
{
Scanner keyboard = new Scanner (System.in);
int inputNumber, smallest, largest, sum,count;
sum=0;
count=0;
System.out.println("Enter a number, entering a negative number
will end the program");
inputNumber = keyboard.nextInt();
while (inputNumber >=0)
{
if (inputNumber>sum){
int largest= inputNumber;
int smallest= sum;
}
else if (sum<inputNumber){
int largest=sum;
int smallest=inputNumber;
}
sum += inputNumber;
count++;
System.out.println("Enter another number");
inputNumber = keyboard.nextInt();
}
double average = sum/count;
System.out.println("The largest number is "+largest+", the smallest number is "+smallest+", and the average of all positive numbers entered is "+average+".");
}
}
答案 0 :(得分:0)
您已经在第7行中定义了最小和最大
int inputNumber,最小,最大,总和,计数;
,然后您仍然在if和else if中定义最小和最大。 更正的代码
TextBlock