-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

我收集到如果用户表示他们想继续,你想继续重复 switch 语句。在这种情况下,您需要将 switch 语句包装在一个循环中,该循环一直循环直到用户说“不”。考虑以下示例:

boolean shouldContinue = true;

while (shouldContinue == true){

    switch(Input.toLowerCase()) {

        case "earth":
            //calculate speed...

            System.out.print("\n \nEnter Yes for another calculation, else No: ");
            String Another = keyboard.next();
            Another.toLowerCase();

            //If the user inputs 'no', we change shouldContinue, that stops the loop from executing again
            if (Another.equals("no"){
                shouldContinue = false;
            }
        break;



         case "water":
            //calculate speed...

            System.out.print("\n \nEnter Yes for another calculation, else No: ");
            Another = keyboard.next();
            Another.toLowerCase();

            //If the user inputs 'no', we change shouldContinue, that stops the loop from executing again
            if (Another.equals("no"){
                shouldContinue = false;
            }
        break;

         case "steel":
            //calculate speed...

            System.out.print("\n \nEnter Yes for another calculation, else No: ");
            String Another = keyboard.next();
            Another.toLowerCase();

            //If the user inputs 'no', we change shouldContinue, that stops the loop from executing again
            if (Another.equals("no"){
                shouldContinue = false;
            } 
           break;     

        case "earth":
            //calculate speed...

            System.out.print("\n \nEnter Yes for another calculation, else No: ");
            String Another = keyboard.next();
            Another.toLowerCase();

            //If the user inputs 'no', we change shouldContinue, that stops the loop from executing again
            if (Another.equals("no"){
                shouldContinue = false;
            } 
       break;

        default :
            System.out.print("Invalid. Re-run the program. ");
            shouldContinue = false;    
        break;
   }
}

关于您的代码的其他一些说明:

您没有遵循变量的java 命名约定。所有变量名都应以小写字母开头。这是一个简单的外观更改,使您的代码更易于阅读。

除变量外,所有实例、类和类常量都是大小写混合,首字母小写

可以说,您也将太多代码放在一个地方,您应该尝试通过将其中一些代码移动到单独的(可重用)方法来改进您的代码。

于 2013-09-28T17:48:31.493 回答
-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 回答