2016-09-12 25 views
0

三角形の欠けている部分を見つけるこのプログラムをビルドする必要がありますが、エラーメッセージが表示され続けます。これは私のコードです:Javaの基本的な電卓

import java.util.Scanner; 

public class MissingSide { 

    static java.util.Scanner userInput = new Scanner(System.in); 

    public static void main(String[] args) { 

    System.out.print("What is the first side, other than the hypotenuse?"); 

    if (userInput.hasNextInt()) { 
     int firstsideGiven = userInput.nextInt(); 
    } else { 
     System.out.println("Enter something acceptable"); 
    } 
    System.out.println("What is the hypotenuse?"); 

    if (userInput.hasNextInt()) { 
     int hypotenuseGiven = userInput.nextInt(); 
    } else { 
     System.out.print("Really?"); 
    } 
    System.out.print("Your missing side value is: " + 
    System.out.print((Math.pow(firstsideGiven, 2) - Math.pow(hypotenuseGiven, 2)) + "this"); 
    } 
} 

それは「hypotenuseGiven」と「firstsideGivenは」変数に解決できないことを私に言って続けています。これは個人的な使用のためであり、学校のものではありません。ありがとうございました。

+2

あなたは、その範囲は、if文に限定されることを意味し、内部の変数を宣言しました。 – 4castle

+0

また、ユーザーが数字を入力しなかった場合は、「Enter something acceptable」または「Really?」と印刷します。あなたはそのような状況で再び尋ねるためにループバックする必要があると思いませんか? – Andreas

答えて

5

firstsideGivenの有効範囲は、コード内のif() {...}ステートメントに限定されています。

これらのスコープ外では使用できません。その場合は、if() {...}ブロックの外に宣言してください。

0

変数の範囲はifブロックに限定されます。

エクストラ注:

1)構文エラーがコードのあなたのSystem.out.print()の部分でもあります。

2)計算には、pythagorasの公式に従ってsqurorootが必要です。次のように

デバッグさのコードサンプルは次のとおりです。if文

import java.util.*; 
import java.lang.Math; 

public class MissingSide 
{ 
    public static void main(String[] args) 
    { 
     int firstsideGiven = 0; 
     int hypotenuseGiven = 0; 
     Scanner userInput = new Scanner(System.in); 
     System.out.print("What is the first side, other than the hypotenuse?"); 

     if (userInput.hasNextInt()){ 

     firstsideGiven = userInput.nextInt(); 

     }else { 
      System.out.println("Enter something acceptable"); 
     } 
     System.out.println("What is the hypotenuse?"); 

     if (userInput.hasNextInt()){ 
      hypotenuseGiven = userInput.nextInt(); 
     }else{ 
      System.out.print("Really?"); 
     } 
     System.out.print("Your missing side value is: " +(Math.sqrt((hypotenuseGiven*hypotenuseGiven)-(firstsideGiven*firstsideGiven)))); 
     } 
    } 
関連する問題