import java.io.*;
public class Alone13_4 {
int a;
void getsum(int a){
this.a=a;
a+=a;
System.out.println("总数为:"+a);
System.out.println("平均数为:"+a/3);
}
void set(){
this.a=0;
}
public static void main(String [] args){
Alone13_4 al=new Alone13_4();
int i=1;
outer: for( ;true; ){
int b=0;
System.out.print("第"+i+"个整数:");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
try{
String s=br.readLine();
if(s!=null){
System.out.println(s);
b=Integer.parseInt(s);
b+=b;
s=null;
i++;
}
else{
continue outer;
}
}catch(Exception e){}
if(i==4){
al.getsum(b);
i=1;
al.set();
}
}
}
}
//我想输入三个数字,它就会进行计算,然后再问我输入数字.一直问下去.
可以它的计算结果错了.帮忙看一下
public class Alone13_4 {
int a;
void getsum(int a){
this.a=a;
a+=a;
System.out.println("总数为:"+a);
System.out.println("平均数为:"+a/3);
}
void set(){
this.a=0;
}
public static void main(String [] args){
Alone13_4 al=new Alone13_4();
int i=1;
outer: for( ;true; ){
int b=0;
System.out.print("第"+i+"个整数:");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
try{
String s=br.readLine();
if(s!=null){
System.out.println(s);
b=Integer.parseInt(s);
b+=b;
s=null;
i++;
}
else{
continue outer;
}
}catch(Exception e){}
if(i==4){
al.getsum(b);
i=1;
al.set();
}
}
}
}
//我想输入三个数字,它就会进行计算,然后再问我输入数字.一直问下去.
可以它的计算结果错了.帮忙看一下
this.a=a;
a+=a;b=Integer.parseInt(s);
b+=b;写的有问题 错在B这里
this.a=a;
a+=a;
删掉
可能不是楼主想要的,
import java.io.*;public class Alone13_4 {
int sum = 0; void getSum(int sum, int count) {
this.sum += sum;// this.a=a; a+=a; error!
System.out.println("总数为:" + sum);// 所有数字和
System.out.println("平均数为:" + sum / (count * 3));// 所有数字的平均数
} public static void main(String[] args) {
Alone13_4 al = new Alone13_4();
int i = 1; // 输入序号
int count = 0; // 计算次数,每输三个数计算一次即count+1
int theSum = 0; // 每三个数字和
outer: for (; true;) {
int b = 0;
System.out.print("第" + i + "个整数:");
BufferedReader br = new BufferedReader(new InputStreamReader(
System.in));
try {
String s = br.readLine();
if (s != null) {
System.out.println(s);
b = Integer.parseInt(s);
theSum += b;// b+=b; error!
s = null;
i++;
} else {
continue outer;
}
} catch (Exception e) {
}
if (i == 4) {
count++;
al.getSum(theSum, count);//
i = 1;
}
} }
}
this.a=0;
}
为什么我这方法不能实现?
因为这方法实现不了,所有当第二回输入时,计算出错了.....
我这样改还是不行
void set(int a){
this.a=a;
}
调用方法,我有用al.set(0);