2017-02-10 2 views
-2

質問する前に、私はすでに他の質問を見てきましたが、ちょっと違うもののようです。 私はJavaを使い慣れていないし、現在学んでいるので、私はこのナルシシズムプログラムを作って、マルチツールのようなものにするつもりです。その部分は本当に重要ではありません。 基本的に、私は数字と操作と物事を入力するテキスト入力電卓を作っていますが、それはかなり簡単です。しかし、入力領域では、ユーザーが数字ではないものを入力する場合には、私は試しています。しかし、xとyの変数(ユーザ入力)は初期化されず、別のの空白計算機(変数)を初期化するときには読み込み不可能になります。これは私のコードである私の公開の空白でエラーを引き起こす試行とキャッチ

 public void calculatorvariables() { 
    System.out.println("Please enter the first number in your calculation."); 
    double x = scanner.nextDouble(); 
    if(x != (double) x) { 
     System.out.println("An error occurred! Did you input nothing or something other than a number? Returning to variable input screen!"); 
    } else 
    System.out.println("Please enter the operator. Valid operators are: '+', '-', '*', '/'"); 
    String operation = scanner.next(); 
    System.out.println("Please enter the second number in your calculation."); 
    try { 
    double y = scanner.nextDouble(); 
    } catch (NumberFormatException e) { 
     System.out.println("An error occurred! Error message: " + e.getMessage() + " Did you input nothing or something other than a number? Returning to variable input screen!"); 
    } 
    new FirstClass().calculator(x, operation, y); 
} 
public void calculator(double x, String operation, double y) { 
    if(operation.equals("+")) { 
     System.out.println(x + y); 
    } 
    else if(operation.equals("-")) { 
     System.out.println(x - y); 
    } 
    else if(operation.equals("*")) { 
     System.out.println(x * y); 
    } 
    else if(operation.equals("/")) { 
     System.out.println(x/y); 
    } 
    else { 
     System.out.println("Unknown operation! Returning to input area!"); 
     new FirstClass().calculatorvariables(); 
    } 
    System.out.println("Do you want to do another calculation? Y/N"); 
    doAnotherCalculation = scanner.next().equalsIgnoreCase("Y"); 
    if (doAnotherCalculation == true) { 
     new FirstClass().calculatorvariables(); 
    } 
    else { 
     new FirstClass().mainmenu(); 
    } 

} 

すべてのヘルプ(私は電卓の一部が含まれていましたが、残りは無関係であり、すべての作業罰金が、また、私はXが異なってyに処理されます知っている、私はそれらの両方をテストしていました)ありがとう!

答えて

1

あなたはこのように、のtry/catchビットの外で変数を宣言する必要があります

double y; 

try { 
    y = scanner.nextDouble(); 
catch (Exception e) { 
    // Exception handling 
} 
+0

を動作するはずです。それは解決策の半分です。 'try-catch'の外側で実際に初期化する(つまり、値を割り当てる)ことはさらに助けになります。 – Carsten

+0

おっと、私はそれを修正しました。 –

0

これは、変数を "宣言" するだろう

import java.util.Scanner; 

public class TestSo{ 
    Scanner scanner = new Scanner(System.in); 
    public static void main(String[] args){ 
     new TestSo().calculatorvariables(); 
    } 

    public void calculatorvariables() { 
     System.out.println("Please enter the first number in your calculation."); 

     double x = scanner.nextDouble(); 
     if(x != (double) x) { 
      System.out.println("An error occurred! Did you input nothing or something other than a number? Returning to variable input screen!"); 
     } else 
      System.out.println("Please enter the operator. Valid operators are: '+', '-', '*', '/'"); 
      String operation = scanner.next(); 
      System.out.println("Please enter the second number in your calculation."); 
      double y = scanner.nextDouble(); 
      calculator(x, operation, y); 
     } 
    } 

    public void calculator(double x, String operation, double y) { 
     if(operation.equals("+")) { 
      System.out.println(x + y); 
     } else if(operation.equals("-")) { 
      System.out.println(x - y); 
     } else if(operation.equals("*")) { 
      System.out.println(x * y); 
     } else if(operation.equals("/")) { 
      System.out.println(x/y); 
     } else { 
      System.out.println("Unknown operation! Returning to input area!"); 
      calculatorvariables(); 
     } 
     System.out.println("Do you want to do another calculation? Y/N"); 
     boolean doAnotherCalculation = scanner.next().equalsIgnoreCase("Y"); 
     if (doAnotherCalculation == true) { 
      calculatorvariables(); 
     } else { 
      System.out.println("Done! Exiting"); 
     } 

    } 
} 
関連する問題