简体   繁体   English

我如何做到这一点,以便我的代码不断询问第1、2和3部分以及Choice :? 第三次尝试后,扫描仪将关闭

[英]How do I make it so my code continuously asks parts 1., 2., and 3. and Choice:? After a third attempt the scanner just closes

import java.lang.Math;
import java.util.Scanner;

public class Temperature {

    static int fahrenheit;
    static int celsius;

    public static int Celsius(int fahrenheit) {

        fahrenheit = (int) (5.0 / 9.0 * (fahrenheit - 32));
        return fahrenheit;

    }

    public static int Fahrenheit(int fahrenheit) {
        fahrenheit = (int) (9.0 / 5.0 * celsius + 32);
        return fahrenheit;
    }

    public static void main(String[] args) {

        // TODO Auto-generated method stub

        Scanner scan = new Scanner(System.in);
        System.out.println("1. Fahrenheit to Celsius");
        System.out.println("2. Celsius to Fahrenheit");
        System.out.println("3. Exit");
        System.out.println("Choice:");

        int choice = scan.nextInt();

        switch (choice) {

        case 1:
            System.out.println("Enter temperature: ");
            fahrenheit = scan.nextInt();
            System.out.println(fahrenheit + " Fahrenheit is " + Celsius(fahrenheit) + " Celsius");
            System.out.println("1. Fahrenheit to Celsius");
            System.out.println("2. Celsius to Fahrenheit");
            System.out.println("3. Exit");
            System.out.println("Choice:");
            choice = scan.nextInt();

        case 2:

            System.out.println("Enter temperature: ");
            celsius = scan.nextInt();
            System.out.println(celsius + " Celsius is " + Fahrenheit(celsius) + " Fahrenheit");
            System.out.println("1. Fahrenheit to Celsius");
            System.out.println("2. Celsius to Fahrenheit");
            System.out.println("3. Exit");
            System.out.println("Choice:");
            choice = scan.nextInt();

        case 3:
            break;

        }
  1. Fahrenheit to Celsius 华氏温度到摄氏温度
  2. Celsius to Fahrenheit 摄氏到华氏度
  3. Exit Choice: 1 Enter temperature: 100 100 Fahrenheit is 37 Celsius 退出选择:1输入温度:100 100华氏度是37摄氏度
  4. Fahrenheit to Celsius 华氏温度到摄氏温度
  5. Celsius to Fahrenheit 摄氏到华氏度
  6. Exit Choice: 2 Enter temperature: 0 0 Celsius is 32 Fahrenheit 退出选择:2输入温度:0 0摄氏度是32华氏度
  7. Fahrenheit to Celsius 华氏温度到摄氏温度
  8. Celsius to Fahrenheit 摄氏到华氏度
  9. Exit Choice: 1 出口选择:1

After the third attempt, the code doesn't run anymore no matter which choice I input. 第三次尝试后,无论我输入哪种选择,代码都不再运行。 I want it so it continues to ask as long as the input is either 1 or 2. 我想要它,只要输入是1或2,它就会继续询问。

Enclose it in a do-while loop 将其封装在do-while循环中

do {
    switch(choice) {
       ...
    }
}while(choice != 3);

You should have a menu method, for example: 您应该有一个菜单方法,例如:

public static void showMenu() {
    System.out.println("1. Fahrenheit to Celsius");
    System.out.println("2. Celsius to Fahrenheit");
    System.out.println("3. Exit");
    System.out.println("Choice:");
}

On your main method have a call to this method inside a do-while loop: 在您的main方法上,在do-while循环中调用此方法:

public static void main(String args[]) {
    //Add your Scanner code here, your variables, etc
    do {
        showMenu();
        choice = scan.nextInt();

        switch (choice) {
            case 1:
                System.out.println("Enter temperature: ");
                fahrenheit = scan.nextInt();
                System.out.println(fahrenheit + " Fahrenheit is " + Celsius(fahrenheit) + " Celsius");
                break;
            case 2:
                //Do the same for method 1 but for Celsius
    } while (choice != 3);
}

And, you're done! 并且,您完成了!

Just put while-loop or do-while a around your switch-case as likewise, 同样,只要在开关盒周围放置while-loopdo-while

while(choice!= 3) {
     switch (choice) {
      .....
     }
}

OR 要么

do {
     switch(choice) {
       ......
     }
} while (choice != 3) 

This will lets you to continuously iterate as many time as you want unless you enter 3 . unless输入3 unless您将连续不断地iterate时间。

you can even put the following condition in order to make more secure, 您甚至可以设置以下condition以提高安全性,

while ( choice >= 3 ) //not recommended but you can.

If you do not want the program to end put it in a while loop: 如果您不希望程序结束,则将其放入while循环中:

   Scanner scan = new Scanner(System.in);
   System.out.println("1. Fahrenheit to Celsius");
   System.out.println("2. Celsius to Fahrenheit");
   System.out.println("3. Exit");
   System.out.println("Choice:");

   int choice = scan.nextInt();

   do {
     switch (choice) {
        ...
         }
      } while (choice!= 3);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我对应用程序的要求如何拨打和管理电话? - How i do to my app asks to make and manage phone calls? 我将如何更改此代码以使其询问我要加载的文件的位置? - How would I change this code to make it so that it asks me the location of the file that I want to load? 如何确保Scanner hasNextInt()要求新输入? - How do I ensure that Scanner hasNextInt() asks for new input? 当用户输入 1 - 2 时,scanner.nextLine() 会导致错误。如何让我的程序接受此输入为 1? - scanner.nextLine() results in an error when user inputs 1 - 2. How do I allow my program to accept this input as 1? 如何向我的 ListView 中创建的每个 ListCell 添加一个侦听器以 1. 获取该单元格中的数据和 2. 调用方法? - How can I add a listener to each ListCell created in my ListView to 1. get the data in that cell and 2. call a method? 如何制作扫描仪,以便始终能够输入命令 - How do i make a scanner so that I'm always able to input commands 为什么即使我输入 5,我的代码仍然要求输入用户名? - Why do my code still asks for Username even if I input 5? 如何做到这一点,以便我的代码显示一定数量的行(像这样),但是向后 - How do I make it so my code displays a cerrtain amount of lines (like it does) but backwards 如何制作按钮:1.激活bean的方法,并2.导航到可添加书签(!)的位置 - How to make a button: 1. activate a bean's method and 2. navigate to a bookmarkable (!) location 我的扫描仪两次要求输入 - My scanner asks for an input twice
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM