提示用户输入两个数,并输出这两个数的平均值(若输入个数或格式有误,提示用户输入错误),使用try,catch,finally报错

charles_su
  • 42
package com.xmu.hellojava.main;
import com.xmu.hellojava.domain.Student;
import java.util.Scanner;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class StudentTest {

    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub
    //Calculating the average
    System.out.printf("Please enter two numbers:(Separated by spaces)\n");//Prompt message
    StudentTest t = new StudentTest();
    System.out.println("Average Value:"+t.average());
    //end calculating the average
 }
    public double average() throws IOException
    {
        double []result=new double[2];
       while(true)
        {
           BufferedReader input5= new BufferedReader(new InputStreamReader(System.in));
            String str=input5.readLine();//读取字符串
            String[]new_str= str.split("\\s+");//以空格分割字符串
            double []num=new double[new_str.length];
            int flag=-1,flag1=-1;//用于判断输入结果是否正确
            for (int i = 0; i<new_str.length; i++){
                try {
                       num[i]=Double.valueOf(new_str[i].toString());
                       flag1=2;
                     } //end try
                    catch (NumberFormatException e) {
                        e.printStackTrace();
                   }//end catch
               }//将字符串转化为数,结束循环
            finally{
        try{
            flag=new_str.length;
            if(flag!=2||flag1!=2){
            Exception me=new Exception("Invalid input");
            throw me;
            }//end if
            result[0]=num[0];
            result[1]=num[1];
            break;
        }//end try
        catch(Exception e)
        {//若输入个数或格式有误,提示用户输入错误
            System.out.println(e.getMessage()+",try again\n");//Prompt message
         }//end catch
        }//end finally
        }//end while
        return (result[0]+result[1])/2.0;
        
    }//average
    
}
回复
阅读 3.1k
2 个回答
import java.util.Scanner;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Test {
    public static void main(String[] args) throws IOException {
        Test t = new Test();
        t.waitInput();
        System.out.println("Average Value:" + t.average());
    }

    private double num1;
    private double num2;

    public void waitInput() throws IOException {
        while (true) {
            System.out.printf("Please enter two numbers:(Separated by spaces)\n");
            BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
            String line = reader.readLine();
            String[] parts = line.trim().split("\\s+");

            if (parts.length < 2) {
                System.out.println("ERROR: incorrect input");
                continue;
            }

            try {
                num1 = Double.parseDouble(parts[0]);
                num2 = Double.parseDouble(parts[1]);
                break;
            } catch (NumberFormatException e) {
                e.printStackTrace();
            }
        }
    }

    public double average() throws IOException {
        return (num1 + num2) / 2;
    }
}

每次while循环就生成一个BufferedReader对象,而且不关闭?

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
宣传栏