2017-09-06 8 views
0

私のコードが正しく動作するように見えません。私は四角形の面積を求め、インチ単位、メートル単位、センチメートル単位、フィート単位のいずれかで使用するものに応じて単位を追加する必要があります。スイッチ/ケースの使用時に定数が無効ですか?

public static void main (String[] args) 
{ 

// create scanner to read the input 

     Scanner sc = new Scanner(System.in); 

//prompt the user to enter one side of the length 

     System.out.println("Enter one side of lenght of the square:"); 

     double side1 = sc.nextDouble(); 

     while (side1 < 0) 

     { 
//prompt the user to enter the input again in a positive value 

      System.out.println("Error, No Negative Number. Enter again:"); 

      side1 = sc.nextDouble(); 
     } 
char unitsMeasurement; 
// prompt the user to enter the measurement unit 
char units = Character.toUpperCase(status.charAt(0)); 
String unitsMeasurement = ""; 

    **{ 
     switch(units) 
     { 
     case "in": 
      unitsMeasurement = "inch"; break; 
     case "cm": 
      unitsMeasurement = "centimeter"; break; 
     case "ft": 
      unitsMeasurement = "feet"; break; 
     case "m": 
      unitsMeasurement = "meter"; break; 

     default:System.out.println("Invaild unit"); break; 
         }** 


//Area of Square = side*side 

      double area = side1*side1; 


     **System.out.println("Area of Square is: "+area, +unitsMeasurement+);** 

     } 

    } 
} 
+0

あなたが質問テキストに遭遇したエラーを含めてください。正確なエラーメッセージであることを確認してください。タイトルから、あなたはJavaパッケージをインポートすることができなかったかもしれないと思います。 import java.lang。*;を追加してみてください。 – Digits

答えて

1

あなたの主な問題は、すべてのケースがStringに基づいている間、あなたはchar上のスイッチ・ケース・ステートメントを使用していること、です。それは一緒には機能しません。 その他の問題は、statusが決して定義されていないことです。したがって、unitsに値を設定することはできません。

達成しようとしていることはよく分かりませんが、私は次のことを想定しています: ユーザーは単位(略称)で四角の長さを入力します。プログラムは、正方形の面積を計算し、それを単位(省略)とともに出力します。

サンプル入力:

5cm 

出力例:

Area of square is: 25 centimeter^2 

領域は二乗長さの単位を持っていることに留意してください!その上で

は、ここにいくつかの作業コードは次のとおりです。

public static void main (String[] args) { 

    // create scanner to read the input 
    Scanner sc = new Scanner(System.in); 

    //prompt the user to enter one side of the length 
    System.out.println("Enter one side of lenght of the square:"); 
    String input = sc.nextLine(); 

    //Remove anything but digits 
    double side1 = Double.parseDouble(input.replaceAll("\\D+","")); 
    //Remove all digits 
    String unit = input.replaceAll("\\d",""); 
    System.out.println(side1); 
    System.out.println(unit); 

    while (side1 < 0) { 
     //prompt the user to enter the input again in a positive value 
     System.out.println("Error, No Negative Number. Enter again:"); 
     input = sc.nextLine(); 

     //Remove anything but digits 
     side1 = Double.parseDouble(input.replaceAll("\\D+","")); 
    } 

    switch(unit) { 
     case "in": 
      unit = "inch"; 
      break; 
     case "cm": 
      unit = "centimeter"; 
      break; 
     case "ft": 
      unit = "feet"; 
      break; 
     case "m": 
      unit = "meter"; 
      break; 

     default: 
      System.out.println("Invaild unit"); 
      break; 
    } 

    double area = side1*side1; 
    System.out.println("Area of Square is: " + area + " " + unit + "^2"); 
} 
関連する問題