-1

ユーザーが距離と通過する媒体(空気、水、地球、鋼)を入力した後、私のプログラムは音速を計算します。ユーザーが「はい」と入力した場合は計算が完了した後、メイン プログラムに戻る方法を知る必要があるだけです。

System.out.print(" What is the distance in feet:" );
//ask the user to input variables

while (!keyboard.hasNextDouble()) {
    System.out.println("Please enter a valid numeric value, try again: ");
    keyboard.next();
}
Distance =keyboard.nextDouble();
{
    System.out.print("Input the media: Air, Water, Steel, or Earth: ");
    String Input = keyboard.next();

    switch(Input.toLowerCase()) {

        case "air":
            AirSpeed = Distance/Air;
            System.out.print("\n \nThe time to for sound to travel ");
            System.out.print(Distance);
            System.out.print(" feet through AIR" +"\n");
            System.out.printf("%.6f", AirSpeed);
            System.out.print(" seconds or ");
            System.out.printf("%.1f", OneFootPerSecond*Air);
            System.out.print(" miles per hour."); 
            System.out.print("\n \nEnter Yes for another calculation, else No: ");
            String Another = keyboard.next();
            Another.toLowerCase();

            break;

        case "water":
            WaterSpeed = Distance/Water;
            System.out.print("\nThe time to for sound to travel ");
            System.out.print(Distance);
            System.out.print(" feet through WATER" +"\n");
            System.out.printf("%.6f",WaterSpeed);
            System.out.print(" seconds or ");
            System.out.printf("%.1f", OneFootPerSecond*Water);
            System.out.print(" miles per hour."); 
            System.out.print("\n \nEnter Yes for another calculation, else No: ");
            Another = keyboard.next();
            Another.toLowerCase();

            break;

        case "steel":
            SteelSpeed = Distance/Steel;
            System.out.print("\nThe time to for sound to travel ");
            System.out.print(Distance);
            System.out.print(" feet through STEEL" +"\n");
            System.out.printf("%.6f",SteelSpeed);
            System.out.print(" seconds or ");
            System.out.printf("%.1f", OneFootPerSecond*Steel);
            System.out.print(" miles per hour."); 
            System.out.print("\n \nEnter Yes for another calculation, else No: ");
            String Another = keyboard.next();
            Another.toLowerCase();

            break;

        case "earth":
            EarthSpeed = Distance/Water;
            System.out.print("\nThe time to for sound to travel ");
            System.out.print(Distance);
            System.out.print(" feet through EARTH" +"\n");
            System.out.printf("%.6f",EarthSpeed);
            System.out.print(" seconds or ");
            System.out.printf("%.1f", OneFootPerSecond*Earth);
            System.out.print(" miles per hour."); 
            System.out.print("\n \nEnter Yes for another calculation, else No: ");
            String Another = keyboard.next();
            Another.toLowerCase();

            break;

        // TODO code application logic here
        default :
            System.out.print("Invalid. Re-run the program. ");

            break;
    }
} 
4

2 に答える 2

-1

ある種のループ内からそのメソッドを (スイッチを使用して) 呼び出す必要があります。例えば

public static void main() {


    //start loop

    //invoke method containing switch statement.

    //endloop

    }

それ以外の場合は、switch が実行され、何もすることがなく、プログラムが終了します。

おそらく試してみてください:

while(true) {
  //invoke switch code containing switch statement
}

手動で強制終了する必要があります。その後、必要な場合は、コマンドで正常に終了するようにしてください (ヒントを出しすぎないようにします)。

于 2013-09-28T17:36:04.977 に答える