我无法让这个程序正常运行。我的所有答案,Min,Max,Average均为零。我知道我在开始时将它们初始化为零,但它们应该通过代码进行更改?
public static void main(String[] args) {
// TODO Auto-generated method stub
int a,b;
int max;
double average;
int min;
int total;
max=0;
min=0;
total=0;
Scanner keyboardInput = new Scanner(System.in);
System.out.println(" Enter a number :");
a = keyboardInput.nextInt();
System.out.println(" You entered :"+ a);
if (a>=0)
{a=max;
max=min;}
total=a;
System.out.println(" Max and min are "+ max +" & " + min + " The total and average is :" +total + " & "+a);
System.out.println(" Enter a second number");
b = keyboardInput.nextInt();
System.out.println(" You entered :"+ b);
if (b>=a)
{b=max;}
if (b<=a)
{b=min;}
average= (a+b)/2;
total= a+b;
System.out.println(" Max and min are "+ max + " & " + min + " and the total & average are :" +total + " & " +average +" respectively.");
//c = keyboardInput.nextDouble();
}
}
输出: 输入一个数字:
2
您输入了:2
最大值和最小值为0&amp; 0总和平均值为:0&amp; 0
输入第二个数字
3
您输入了:3
最大值和最小值为0&amp; 0和总和&amp;平均值是:0&amp;分别为0.0。
答案 0 :(得分:1)
因为你的作业被逆转了:
这一行:
a=max;
应该是:
max=a;
max
为零,您使用a
覆盖max
,使a
为零。
你也犯了b
同样的错误。
答案 1 :(得分:0)
而不是
if (a>=0)
{ max=a;
max=min; }
最大值应分配给a,min应分配到最大max=a
和min=max
。否则,它会将a
设置为max
0
,然后将max
设置为min
,这又是0
。
if (a>=0)
{ max=a;
min=max; }
到处更新分配问题。记住左变量被赋值为右变量的值
答案 2 :(得分:0)
public static void main(String[] args) {
int a, b;
int max;
double average;
int min;
int total;
max = 0;
min = 0;
total = 0;
Scanner keyboardInput = new Scanner(System.in);
System.out.println(" Enter a number :");
a = keyboardInput.nextInt();
System.out.println(" You entered :" + a);
if (a >= 0) {
//modify a=max;
max = a;
//modify max=min;
min = max;
}
total = a;
System.out.println(" Max and min are " + max + " & " + min
+ " The total and average is :" + total + " & " + a);
System.out.println(" Enter a second number");
b = keyboardInput.nextInt();
System.out.println(" You entered :" + b);
if (b >= a) {
//modify b=max;
max = b ;
}
if (b <= a) {
b = min;
}
// modify average= (a+b)/2;
average = (double)(a + b) / 2;
total = a + b;
System.out.println(" Max and min are " + max + " & " + min
+ " and the total & average are :" + total + " & "
+ average + " respectively.");
// c = keyboardInput.nextDouble();
}