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

Java 为什么我的循环在第一次之后跳过if/else语句?,java,if-statement,for-loop,skip,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 t

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); 
            }
        }
    }
}
import java.util.Scanner;
公共课堂实践程序3秒
{
静态扫描仪扫描=新扫描仪(System.in);
公共静态void main(字符串[]args)
{
println(“此程序旨在将温度从摄氏度转换为华氏度,以及其他方式。”);
int无穷远=0;
int-oneLimitLoop=0;
对于(int x=0;x<1;x--)
{
//**System.out.println(无穷大);//循环测试器
System.out.println(“是Celcius还是华氏度”);
System.out.println(“请输入C/C frr celcius,或F/F表示华氏度”);
String testype=scan.nextLine();
scan.nextLine();
System.out.println(“您现在可以输入要转换的设计温度”);
int tempNumber=scan.nextInt();
if(试探类型等信号案例(“c”))
{
int celcius=临时数字;
int-celciusconverter=(9*(celcius)/5)+32;
系统输出打印LN(celciuscConverter);
}       
else if(试探类型.相等信号(“f”))
{
内华氏温度=温度数值;
int farenheitConverter=5*(华氏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);
        }

    }

}
publicstaticvoidmain(字符串[]args){
System.out.println(
“该程序旨在将温度从摄氏度转换为华氏度,以及其他方式。”);
int无穷远=0;
int-oneLimitLoop=0;
对于(int x=0;x<1;x--){
//**System.out.println(InfiniteLop);//循环测试仪
System.out.println(“是Celcius还是华氏度”);
System.out.println(“请输入C/C frr celcius,或F/F表示华氏度”);
String testype=scan.nextLine();
//scan.nextLine();
System.out.println(“您现在可以输入要转换的设计温度”);
int tempNumber=Integer.parseInt(scan.nextLine())
if(试探类型等信号案例(“c”)){
int celcius=临时数字;
int-celciusconverter=(9*(celcius)/5)+32;
系统输出打印LN(celciuscConverter);
}else if(试探类型.相等信号(“f”)){
内华氏温度=温度数值;
int farenheitConverter=5*(华氏-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);
        }

    }

}
publicstaticvoidmain(字符串[]args){
System.out.println(
“该程序旨在将温度从摄氏度转换为华氏度,以及其他方式。”);
int无穷远=0;
int-oneLimitLoop=0;
对于(int x=0;x<1;x--){
//**System.out.println(InfiniteLop);//循环测试仪
System.out.println(“是Celcius还是华氏度”);
System.out.println(“请输入C/C frr celcius,或F/F表示华氏度”);
String testype=scan.nextLine();
//scan.nextLine();
System.out.println(“您现在可以输入要转换的设计温度”);
int tempNumber=Integer.parseInt(scan.nextLine())
if(试探类型等信号案例(“c”)){
int celcius=临时数字;
int-celciusconverter=(9*(celcius)/5)+32;
系统输出打印LN(celciuscConverter);
}else if(试探类型.相等信号(“f”)){
内华氏温度=温度数值;
int farenheitConverter=5*(华氏-32)/9;
System.out.println(farenheitConverter);
}
}
}

此外,这应该是一个无限循环,我想不断向用户询问温度变化,情况是,它在第一个循环后没有通过if语句。此外,这应该是一个无限循环,我想不断向用户询问温度变化,情况是,在第一个循环之后,它不会通过if语句。非常感谢,我确实知道!非常感谢你,我知道!