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

标签 java if-statement for-loop skip

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() 时,仅读取您输入的 Integer,新行字符 '\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);
        }

    }

}

关于java - 为什么我的循环在第一次之后会跳过 if/else 语句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32620895/

相关文章:

javascript - 如何在 mongoose 函数的 if/else block 中来回跳转?

java - 为什么我不能以这种方式在 for 循环中定义变量?

Java自签名证书和 keystore 的使用

java - 如何从 itext7 中的现有 PdfDocument 打开 PdfADocument?

windows - 使用 Windows 命令行查看文件是否存在并重命名

Javascript if block 的奇怪行为

c++ - 仅打印数组的某些元素

swift - 在 Xcode 的 UILabel 上显示 Double 类型的数组

java - 打印数组中的元素,元素之间用逗号分隔

java - 通过蓝牙将字节从 Raspberry Pi(使用 python)发送到 java android 应用程序