Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使我的代码不断地询问第1、2和3部分。还有选择:?第三次尝试后,扫描仪刚好关闭 华氏到摄氏度 摄氏到华氏 出口 选择: 1. 输入温度: 100 100华氏度是37摄氏度 华氏到摄氏度 摄氏到华氏 出口 选择: 2. 输入温度: 0 0摄氏度是32华氏度 华氏到摄氏度 摄氏到华氏 出口 选择: 一,_Java - Fatal编程技术网

Java 如何使我的代码不断地询问第1、2和3部分。还有选择:?第三次尝试后,扫描仪刚好关闭 华氏到摄氏度 摄氏到华氏 出口 选择: 1. 输入温度: 100 100华氏度是37摄氏度 华氏到摄氏度 摄氏到华氏 出口 选择: 2. 输入温度: 0 0摄氏度是32华氏度 华氏到摄氏度 摄氏到华氏 出口 选择: 一,

Java 如何使我的代码不断地询问第1、2和3部分。还有选择:?第三次尝试后,扫描仪刚好关闭 华氏到摄氏度 摄氏到华氏 出口 选择: 1. 输入温度: 100 100华氏度是37摄氏度 华氏到摄氏度 摄氏到华氏 出口 选择: 2. 输入温度: 0 0摄氏度是32华氏度 华氏到摄氏度 摄氏到华氏 出口 选择: 一,,java,Java,第三次尝试后,无论我输入哪个选项,代码都不再运行。我想要它,所以只要输入是1或2,它就会继续询问。只需在开关盒周围放置while loop或do whilea,同样 import java.lang.Math; import java.util.Scanner; public class Temperature { static int fahrenheit; static int celsius; public static int Celsius(int fah

第三次尝试后,无论我输入哪个选项,代码都不再运行。我想要它,所以只要输入是1或2,它就会继续询问。

只需在开关盒周围放置
while loop
do while
a,同样

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;

        }

这将允许您连续地
迭代
任意次数
,除非
输入
3

您甚至可以设置以下
条件
,以提高安全性

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

如果不希望程序结束,请将其放入while循环:

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

将其包含在do 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);

您应该有一个菜单方法,例如:

do {
    switch(choice) {
       ...
    }
}while(choice != 3);
在main方法中,在
do while
循环中调用此方法:

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:");
}

而且,你完了

对不起,我不知道如何格式化这个lol
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:");
}
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);
}