简体   繁体   中英

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

the if statements get ignored after the first time

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

Quick answer to your question: when you call scan.nextInt(), only the Integer you entered is read, the new line character '\\n' is kept on the buffer, so the next scanNextLine will read that new line as an empty string in the following loop.

A quick fix would be to simple read the whole line and try to parse as int. Also you should really use a while(true) if you intend to do infinite loop.

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

    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM