Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How Can I Simplify the Yes/No Value I Get From the User for Java?

package hw;
import java.util.Scanner;
import java.util.Random;

public class hw8 {
    public static void main(String[] args) {
        int min = 0;
        int max = 9;
        boolean continueinput = true;
        while (continueinput == true) { //user can use the program without having to run it again.
            int number1 = (int)Math.floor(Math.random() * (max - min + 1) + min);
            int number2 = (int)Math.floor(Math.random() * (max - min + 1) + min);
            Scanner input = new Scanner(System.in);
            System.out.println(number1+" + "+number2+" = ?");
            int studentanswer = input.nextInt();
            int correctanswer = number1 + number2;
            if (studentanswer == correctanswer) {
                System.out.println("Congratulations, you solved it correctly!");
            } else {
                System.out.println("Oh, "+studentanswer+" is not correct, correct answer was "+correctanswer+". But don't worry! I'm sure you'll get it right next time!");
            }
            System.out.println("Do you want to continue solving problems? true(Yes)/false(No) (example: enter false for No)");
            continueinput = input.nextBoolean();
            if (continueinput == false) {
                System.out.println("So you're tired! That's completely okay but don't be a stranger!");
            }
        }
    }
}

For my homework, I am trying to write a math program that can be used by elementary school students. I associated "yes" with true and "no" with false for user to understand. But I don’t think that’s very nice.
Is there any way to run this code when the user types "yes" or "no" directly? Also, if the user types a different value instead of true or false, how can I explain to the user that this is an invalid value and that he/she should enter the value again? I have a little difficulty when it comes to boolean. Thanks in advance.

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

First of all take care of java naming conventions.
Class names should start with upper case character and all names should be camelCase.
Also for boolean values you can write:

if (continueinput) 

You do not need to write == true it makes it more readable

No to your question:

read the line feed after nextInt() by using nextline
and than you can use

String inputYesNo = input.nextLine();
continueinput = inputYesNo.equalsIgnoreCase("YES") ? true :  false;

to map Yes to true and other values to false

class Hw8 {
    public static void main(String[] args) {
        int min = 0;
        int max = 9;
        boolean continueinput = true;
        while (continueinput) { //user can use the program without having to run it again.
            int number1 = (int) Math.floor(Math.random() * (max - min + 1) + min);
            int number2 = (int) Math.floor(Math.random() * (max - min + 1) + min);
            Scanner input = new Scanner(System.in);
            System.out.println(number1 + " + " + number2 + " = ?");
            int studentanswer = input.nextInt();
            input.nextLine();
            int correctanswer = number1 + number2;
            if (studentanswer == correctanswer) {
                System.out.println("Congratulations, you solved it correctly!");
            } else {
                System.out.println("Oh, " + studentanswer + " is not correct, correct answer was " + correctanswer + ". But don't worry! I'm sure you'll get it right next time!");
            }
            System.out.println("Do you want to continue solving problems? true(Yes)/false(No) (example: enter false for No)");
            String inputYesNo = input.nextLine();
            continueinput = inputYesNo.equalsIgnoreCase("YES") ? true :  false;
            if (!continueinput) {
                System.out.println("So you're tired! That's completely okay but don't be a stranger!");
            }
        }
    }
 }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading