日期:2014-05-20 浏览次数:20812 次
大哥,不会吧,这样的问题也要求人。。。。。。 import java.io.*; class NotValidNumException extends Exception{ } public class Score{ public static int getChoose() throws IoException,NumberNotFormatException,NotValidNumException{ InputStreamReader ir = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(ir); int i = 0; i = Integer.parseInt(br.readLine()); if(i != 1 || i != 2){ throw new NotValidNumException(); } return i; } public static int getArraySum(int[] num){ int sum = 0; for(int i = 0 ; i < num.length ; i++ ){ sum += num[i]; } return sum; } public static int getArrayAvg(int[] num){ int sum = Score.getArraySum(num); int avg = sum/num.length; return avg; } public static void main(String[] args){ int[] num = {98,79,67,90,88,64,53,97}; boolean flag = true; Integer choose = null; while(flag){ System.out.println("please input your choose:"); try{ choose = Score.getChoose(); }catch(NumberNotFormatException e){ System.out.println("the inputed number is error!please input again!"); continue; }catch(NotValidNumException ee){ System.out.println("the inputed number is invalid number!" + "please input again!"); continue; }catch(IOException eee){ System.out.println(eee.getMessage()); continue; } flag = false; } if(choose == 1){ System.out.println("These number's Avg : " + getArrayAvg); } if(choose == 2){ System.out.println("These number's Sum : " + getArraySum); } } }
------解决方案--------------------
呵呵,按楼主的要求,应该不用写的这么麻烦吧~~
------解决方案--------------------
if(choose == 1){
System.out.println("These number's Avg : " + getArrayAvg);
}
if(choose == 2){
System.out.println("These number's Sum : " + getArraySum);
}
红色部分编绎能过么???
------解决方案--------------------
上面楼主写的不错啊,
public static float getAverage(float[] temp) {
float tempV = 0;
int m = 0;
for (int i = 0; i < temp.length; i++) {
tempV += temp[i];
m++;
}
return tempV / m;
}
public static float getSum(float[] temp) {
float tempV = 0;
for (int i = 0; i < temp.length; i++) {
tempV += temp[i];
}
return tempV;
}
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
float[] temp = { 89, 97, 23, 47, 86, 80, 32, 128 };
while(true){
System.out.println("start");
InputStreamReader input = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(input);
char tempCharater = (char) br.read();
System.out.println(tempCharater);
if (tempCharater == '1') {
System.out.println("成绩的平均数:");
System.out.println(getAverage(temp));
} else if (tempCharater == '2') {
System.out.println("成绩的总和:");