JAVA do-while题目

作业题:求平均数,用户输入任意个非零的数,输入0退出循环,系统计算并显示这些数的平均值。
我们目前学到的是
import java.util.*;
public class ******{
开头的变量要用do-while实现变量,
不要出现BufferedReader IOException
大概是这个效果
请输入数字:XX.X
请输入数字:X
请输入数字:X.X
请注入数字:0

共输入4个数字,和是xxx.x,平均数是xx.x

Scanner t = new Scanner(System.in);
int a, b, c, d;
do {
System.out.println("请输入第一个的值: ");
a = t.nextInt();
System.out.println("请输入第二个的值: ");
b = t.nextInt();
System.out.println("请输入第三个的值: ");
c = t.nextInt();
System.out.println("请输入第四个的值: ");
d = t.nextInt();
try {
System.out.println(a + "/" + b + "的余为 " + (a % b));
System.out.println(a + "+" + b + "= " + (a + b));

System.out.println(c + "/" + d + "的余为 " + (c % d));
System.out.println(c + "+" + d + "= " + (c + d));
} catch (ArithmeticException e) {
System.out.println("被除数为零 ");
}
} while (0 != a || 0 != b || c != d);

不知道是不是你想要的效果?
温馨提示:内容为网友见解,仅供参考
第1个回答  2010-08-07
//编译该程序后,运行的时候在命令行下输入 java Test 5.0 6.0 7.0
public class Test{
public static void main(String[] args)
{
int count = 0;
double sum = 0;

do{
if(Double.valueOf(args[count])==0)
System.exit(-1);
sum +=Double.valueOf(args[count]);
System.out.print(args[count] + " ");
count ++;

if(count >= args.length)
break;
}while(Double.valueOf(args[count])!=0);

System.out.println("这" + count + "个数的平均值为: " + sum/args.length);
}
}本回答被提问者采纳
相似回答
大家正在搜