java - 带 Switch 语句的 While 循环

标签 java while-loop switch-statement

我无法让开关不断循环,直到用户告诉程序停止。 当提示用户放入 N 循环时,应将其发送回开关顶部

        char stop;
    while(stop == 'N'){
        switch(choice){
            case 1:
                System.out.println("Enter the time in seconds:");
                time = input.nextInt();
                displacement = (Math.pow(time,4)) + 16;
                System.out.println("Felix's displacement is equal to " + displacement + " meters");
                System.out.println("Stop the application(Y/N)");
                stop = input.findWithinHorizon(".",0).charAt(0);
                break;
            case 2:
                System.out.println("Enter the time in seconds:");
                time = input.nextInt();
                velocity = 4*(Math.pow(time,3));
                System.out.println("Felix's velocity is equal to " + velocity + " m/s");
                break;
            case 3:
                System.out.println("Enter the time in seconds:");
                time = input.nextInt();
                acceleration = 12*(Math.pow(time,2));
                System.out.println("Felix's acceleration is equal to " + acceleration + " m/(s*s)");
                break;
            default:
                System.out.println("Please select a choice");

        }            
    }
}

最佳答案

  1. 粗略地说:初始化 stop关于声明:

char stop = 'N';

  • 更好:替换您的 whiledo while循环:

    做{

    } while (stop == 'N')

  • 关于java - 带 Switch 语句的 While 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29372691/

    相关文章:

    javascript - while 循环中的 IF 语句不起作用,我哪里出错了?

    python - 如何退出这个 while 循环?

    matlab - 如何获得函数内的语句以随机选择的顺序执行?

    java - Spring MongoDB 和 Apache Shiro

    java - 在哪里可以找到 Artifactory 存储库的 Ivy 描述符

    java - spring boot无法加载css文件

    c# - 如何用 IOC 替换 switch 语句以便我可以保持 SOLID 原则

    Java JCE 无法在 jarsplice 创建的 jar 中验证提供者 BC

    C for/while 循环不求和

    java - 数词,java