为什么我的循环在第一次后跳过我的 if/else 语句?

Why is my loop skipping my if/else statements after the first time?

if 语句在第一次后被忽略

import java.util.Scanner;

public class practiceProgram3SecondTry
{
    static Scanner scan = new Scanner(System.in);

    public static void main (String[] args)
    {
        System.out.println("This program is intended to convert a temperature from Celcius to Fahrenheit, and other way around.");
        int infiniteLoop = 0;
        int oneLimitLoop = 0;
        for(int x = 0 ; x < 1 ; x--)
        {
            //**System.out.println(infiniteLoop); // loop tester

            System.out.println("Is it is Celcius, or Fahrenheit");
            System.out.println("Please enter C/c frr celcius, or F/f for Fahrenheit");                      

            String tempType = scan.nextLine();  
            scan.nextLine();
            System.out.println("You may now enter the desisred temperature you would like to convert");

            int tempNumber = scan.nextInt();

            if (tempType.equalsIgnoreCase("c"))
            {
                int celcius = tempNumber;           
                int celciuscConverter = (9*(celcius)/5)+32;         
                System.out.println(celciuscConverter);          
            }       
            else if (tempType.equalsIgnoreCase("f"))
            {
                int fahrenheit = tempNumber;
                int farenheitConverter = 5 * (fahrenheit-32)/9; 
                System.out.println(farenheitConverter); 
            }
        }
    }
}

快速回答你的问题:当你调用scan.nextInt()时,只读取你输入的整数,换行符'\n'保留在缓冲区中,所以下一个scanNextLine将读取该新行在以下循环中作为空字符串。

一个快速的解决方法是简单地读取整行并尝试解析为 int。如果你打算做无限循环,你也应该真正使用 while(true)。

public static void main(String[] args) {
    System.out.println(
            "This program is intended to convert a temperature from Celcius to Fahrenheit, and other way around.");
    int infiniteLoop = 0;
    int oneLimitLoop = 0;
    for (int x = 0; x < 1; x--) {

        // **System.out.println(infiniteLoop); // loop tester

        System.out.println("Is it is Celcius, or Fahrenheit");
        System.out.println("Please enter C/c frr celcius, or F/f for Fahrenheit");

        String tempType = scan.nextLine();
        //scan.nextLine();
        System.out.println("You may now enter the desisred temperature you would like to convert");

        int tempNumber = Integer.parseInt(scan.nextLine())

        if (tempType.equalsIgnoreCase("c")) {
            int celcius = tempNumber;
            int celciuscConverter = (9 * (celcius) / 5) + 32;
            System.out.println(celciuscConverter);
        } else if (tempType.equalsIgnoreCase("f")) {
            int fahrenheit = tempNumber;
            int farenheitConverter = 5 * (fahrenheit - 32) / 9;
            System.out.println(farenheitConverter);
        }

    }

}