非法输入的二次方程(处理异常)

时间:2016-07-01 21:59:14

标签: java

我正在尝试创建一个程序,询问用户二次方程中变量的输入值。我希望代码使用异常处理来处理错误的输入。错误输入将是:非数字值,其中数值除外。我遇到问题的地方是当输入错误值而不是终止程序时,我希望代码重新请求输入。因此,在非数字,A = 0或B和C都= 0的情况下。 下面的代码有一些错误,但我想知道是否有人可以帮我破解这个!非常感谢

import java.util.InputMismatchException;
import java.util.Scanner;
//Program that does quadratic equations with exceptions

public class Quadratic{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        double a ; 
        do{
            try{ 
                System.out.print("Enter a value for 'a': ");
                a =sc.nextDouble(); 
            }                 
            catch (InputMismatchException ex)
            {
                System.out.println("not a valid character..");
                System.out.println("please try again:");
                sc.nextDouble(); //prompt user again
            }
        }while (a==0);
        System.out.println("value can not be equal to 0. Please enter another value:\n");
        a= sc.nextDouble();
    }
}

2 个答案:

答案 0 :(得分:1)

您的代码目前没有显示您获取变量B或C的任何输入,但它们的错误处理应与此处'a'的输入相似:

import java.util.InputMismatchException;
import java.util.Scanner;

//Program that does quadratic equations with exceptions                                                                

public class Quadratic{

    public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    double a = 0.0 ;
    boolean valid_a = false;
    while(!valid_a){
            try{
                System.out.print("Enter a value for 'a': ");
                a = sc.nextDouble();

                if (a==0){
                    System.out.println("value can not be equal to 0. Please enter another value:");
                }
                else{
                    valid_a = true;
                }
            }
            catch (InputMismatchException ex){
                System.out.println(ex);
                sc.next();
                System.out.println("not a valid character..");
                System.out.println("please try again:");
            }
        }
    }
}

希望这有帮助!

答案 1 :(得分:0)

import java.util.InputMismatchException;
import java.util.Scanner;

//Program that does quadratic equations with exceptions

public class Quadratic{

    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        double a, b, c;

        boolean valid_a = false;
        a = 0.0;
        while(!valid_a){
                try{
                    System.out.print("Please enter value for 'a': ");
                    a = sc.nextDouble();

                    if (a == 0){
                        System.out.println("VALUE CANNOT BE 0, PLEASE TRY AGAIN.");
                    }
                    else{
                        valid_a = true;
                    }
                }
                catch (InputMismatchException e){
                    System.out.println(e);
                    sc.next();
                    System.out.println("INVALID INPUT PLEASE TRY AGAIN");
                }
        }

        boolean valid_b = false;
        b = 0.0;
        while(!valid_b){
            System.out.println("Please enter value for 'b': ");
            try{
                b = sc.nextDouble();
                sc.nextLine();
                valid_b = true;         
            }
            catch(InputMismatchException e){
                System.out.println(e);
                valid_b = false;
                System.out.println("INVALID INPUT PLEASE TRY AGAIN");
                sc.nextLine();          
            }
        }

        boolean valid_c = false;
        c = 0.0;
        while(!valid_c){
             System.out.println("Please enter value for 'c': ");
             try{
                 c = sc.nextDouble();
                 sc.nextLine();
                 valid_c = true;
             }
             catch(InputMismatchException e){
                 System.out.println(e);
                 valid_c = false;
                 System.out.println("INVALID INPUT PLEASE TRY AGAIN");
                 sc.next();
             }
        }

        while(b == 0 && c == 0){
            System.out.println("B AND C CANNOT BE BOTH 0. PLEASE RE-ENTER VALID VALUES.");      
            System.out.println("Value B: ");
            b = sc.nextDouble();
            System.out.println("Value C: ");
            c = sc.nextDouble(); 
        }

        System.out.println("Value a:" +a);
        System.out.println("Value b:" +b);
        System.out.println("Value c:" +c);

        double determinate = Math.pow(b,2)- (4 * a * c);

        if (determinate >= 0){
            double qf1 = ((-b + Math.sqrt(Math.pow(b, 2) - (4 * a * c)))) / (2 * a);
            double qf2 = ((-b - Math.sqrt(Math.pow(b, 2) -(4 * a * c))))/ (2 * a);
                 // qf stands for Quadratic Formula
            System.out.printf("Anwser One: %s \n", qf1); 
            System.out.printf("Anwser Two: %s \n", qf2); 
        }
        else{
            System.out.println("DETERMINATE IS NEGATIVE, THEREFORE THERE ARE NO REAL ROOTS.");
        }


            System.out.println("PROGRAM IS COMPLETE");

            return;
    }
}``