简体   繁体   中英

How to make a particular string input the condition for while loop and it checks everytime an input is entered

If none of these conditions are met or the output has been printed then it will return to ask the user to enter new values. If at any point during the input stage the user enters the word "Quit" then end the program.

Making my program follow these conditions is the only missing piece before I can solve my problem. A while loop at the very start with the condition being as long as the string input "Quit" is entered, it will end is what I have been trying to do but keep failing on.

Our class just started and we haven't even covered methods or OOP yet. Our professor only taught us scanner system.in and the.next(datatype) methods so the other methods of the scanner class which I think has the solution I am looking for keeps going over my head.

This is the input phase of my program:

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
        System.out.println("Please give three numbers");
        int num1 = sc.nextInt();
        int num2 = sc.nextInt();
        int num3 = sc.nextInt();
        System.out.println("Choose the temperature unit of the three numbers.");
        System.out.println("Enter 1 for Celsius, 2 for Fahrenheit and 3 for Kelvin");
        int tempUnit = sc.nextInt();
        switch (tempUnit) {
            case (1) -> System.out.println("Celsius was chosen.");
            case (2) -> System.out.println("Fahrenheit was chosen.");
            case (3) -> System.out.println("Kelvin was chosen.");
        }
        System.out.println("Choose the temperature unit you want to convert it into.");
        System.out.println("Enter 1 for Celsius, 2 for Fahrenheit and 3 for Kelvin");
        int chosenTemp = sc.nextInt();
        switch (chosenTemp) {
            case (1) -> System.out.println("Celsius was chosen.");
            case (2) -> System.out.println("Fahrenheit was chosen.");
            case (3) -> System.out.println("Kelvin was chosen.");
        } 

I tried to come up with something with what you guys told me and this is what I came up with?

if (!sc.hasNextInt()) {
        String end = sc.nextLine();
        if (end.equalsIgnoreCase("Quit")) {
            System.exit(0);

It worked but I feel like this isn't what you guys told me to do. Can you anyone give me an example? I read that this isn't how you set up the "Quit" condition, can anybody teach me how?

I think you wanted achieve something as below

Scanner sc = new Scanner(System.in);
while(true) {
    System.out.println("Please give three numbers");
    int num1 = sc.nextInt();
    int num2 = sc.nextInt();
    int num3 = sc.nextInt();
    System.out.println("Choose the temperature unit of the three numbers.");
    System.out.println("Enter 1 for Celsius, 2 for Fahrenheit, 3 for Kelvin and 4 for Quit");

    int tempUnit = sc.nextInt();
    switch (tempUnit) {
    case 1 : 
        System.out.println("Celsius was chosen.");
        break;
    case 2 : 
        System.out.println("Fahrenheit was chosen.");
        break;
    case 3 : 
        System.out.println("Kelvin was chosen.");
        break;
    case 4 : 
        System.out.println("Quit");
        System.exit(0);
    }
}

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