2017-10-20 1 views
0

現在、私は単純な代数式の計算機を使って、いくつかのJavaの基本を少し上手くやっていますが、問題が発生しています。同じ行の複数の入力が有効でない場合、whileループを使用してエラーメッセージを表示しますか?

私の方法の1つは、二次方程式です。ユーザーにA、B、Cを同じ行に入力するように求め、プログラムが計算を実行します。しかし、入力がdoubleかどうかを調べるwhileループを作成すると、間違った値(たとえば3 5 x)を入れるとエラーになります。

個々の変数ごとにループを実行する必要がありますか、または1つの入力内の3つの変数すべてが受け入れ可能かどうかをより迅速に確認できますか?

public static void quadraticFormula() { 
    Scanner sc = new Scanner(System.in); 

    System.out.print("Please enter the values(in order) for A, B and C: "); 

    while(!sc.hasNextDouble()) { 
     System.out.println("Error! One or more of your inputs are not acceptable. Please try again: "); 
     sc.nextDouble(); 
    } 

    double a = sc.nextDouble(); 
    double b = sc.nextDouble(); 
    double c = sc.nextDouble(); 

    int discriminant = (int) (Math.pow(b, 2) - 4 * a * c); 

    double rootPlus = (-1 * b + Math.sqrt(discriminant))/(2 * a); 
    double rootMinus = (-1 * b - Math.sqrt(discriminant))/(2 * a); 

    System.out.println("Your answers are as follows: "); 
    System.out.println("\tB + : " + rootPlus); 
    System.out.println("\tB - : " + rootMinus); 
} 

答えて

0

投稿したコードは、6つの値、3つのループ、3つの値を読み込もうとします。

私は文字列として値を読み込み、trycatchブロック(変数ごとに1つ)内で解析します。このようにして、入力が2倍を表し、計算を実行できることを検証できます。

0

これを試してください。私はブール値のフラグを使用し、ダブルをテストするために解析します。 その計算は正しいかどうかわかりませんが、入力の問題は正しいです。

 


    import java.util.Scanner; 

    public class Teste { 

     public Teste() { 

      Scanner sc = new Scanner(System.in); 
      String[] input = null; 
      boolean flag = true; 
      double a = 0; 
      double b = 0; 
      double c = 0; 

      while (flag) { 
       System.out.print("Please enter the values(in order) for A, B and C: "); 
       String str = sc.nextLine(); 
       input = str.split(" "); 
       System.out.println("You entered: " + str); 
       if (input.length == 3) { 
        try { 
         a = Double.parseDouble(input[0].trim()); 
         b = Double.parseDouble(input[1].trim()); 
         c = Double.parseDouble(input[2].trim()); 
         flag = false; 
        } catch (NumberFormatException e) { 
         flag = true; 
         System.out.println("Input an integer"); 
        } 
       } else { 
        flag = true; 
        System.out.print("Please enter the values(in order) for A, B and C: "); 
       } 
      } 
      sc.close(); 

      int discriminant = (int) (Math.pow(b, 2) - 4 * a * c); 
      System.out.println(discriminant); 
      double rootPlus = (-1 * b + Math.sqrt(discriminant))/(2 * a); 
      double rootMinus = (-1 * b - Math.sqrt(discriminant))/(2 * a); 

      System.out.println("Your answers are as follows: "); 
      System.out.println("\tB + : " + rootPlus); 
      System.out.println("\tB - : " + rootMinus); 
     } 

     public static void main(String[] args) { 
      Teste app = new Teste(); 
     } 
    } 

+0

パーフェクト!これはまさに私が探していたものです。前にtry/catchを使ったことは一度もなかったので、もう少し詳しく説明します。ありがとう! –

0

私はあなたの問題についていくつか考えています。私はあなたがスキャナを一度しか反復することができないと思うので、あなたがそれらを得るときにあなたが望む値をつかみたいと思う。入力を取得している間は、それがダブルであることを確認するだけでよいのです。そうでなければ、try/catchで制御されたエラーを投げ、ユーザーに再度プロンプトを出すだけです。

あなたは既に正確に3倍が必要であることを知っているので、a、b、cを格納するリストを作成すると、インデックス0 = a、インデックス1 = b、インデックス2 = cの良いオプションになる可能性があります。この戦略を使用するのに役立つ以下の方法を作成しました。

public static void scannerPractice() { 

     Scanner sc = new Scanner(System.in); 
     System.out.print("Please enter the values(in order) for A, B and C:  
      "); 

     //create list to store your a, b, c 
     ArrayList<Double> doubles = new ArrayList<Double>(); 

     //iterate over doubles until it has 3 elements (a, b, c) 
     while (doubles.size() < 3) { 
      //You can use a try to make sure the user is entering doubles 
      try { 
       doubles.add(sc.nextDouble()); 
       //found a mismatch, so print error message, enter method to try 
       //try again and break 
      } catch (InputMismatchException e) { 
       System.out.println("Not a double. Please try again: "); 
       scannerPractice(); 
       break; 
      } 
     } 
     //list now has a, b, c 
     if (doubles.size() == 3) { 
      //iterate over the list to make sure you're getting what's 
      //expected 
      for (int i = 0; i < doubles.size(); i++) { 
       System.out.println(doubles.get(i)); 

      } 
      //enter math logic here 
     } 
     //good practice to close scanner when through 
     sc.close(); 
    } 
関連する問題