Question

What is wrong with my code? Trying to fix the Boolean situation and it keeps skipping...

What is wrong with my code? Trying to fix the Boolean situation and it keeps skipping my boolean question and repeating.

import java.util.Scanner;

public class MyTest{
  
   public static void main (String [] args){
      
       Scanner input = new Scanner(System.in);
      
       System.out.println("JAVA QUIZ");
       System.out.println("This quiz includes three questions about the Java Programming Language.");
       System.out.println("Each question has 4 possible answers. (numbered 1,2,3,4)");
       System.out.println("Enter 0 to exit the test.");
      
       System.out.println();
      
      
       boolean retryTest = true;
      
       while(retryTest == true){
          
           int correctCount = 0;
           int menuChoice = 1;

           System.out.println("What is the default value of String variable?");
           System.out.println("1) \"\" ");   
           System.out.println("2) \" ");   
           System.out.println("3) not defined ");   
           System.out.println("4) null ");
          
           System.out.println();

           menuChoice = input.nextInt();//user input from first question
          
          
           if(menuChoice < 0){
               System.out.println("Number must be between 1,2,3,4 or 0 to stop");
           }
          
           //if user enters anything greater than three invalid number
           else if(menuChoice > 4){
               System.out.println("Number must be between 1,2,3,4 or 0 to stop");
           }
          
           if(menuChoice == 3){
               correctCount++;
           }
          
           if(menuChoice == 0){
               break;
           }
          
          
           System.out.println("What is true about a final class?");   
           System.out.println("1) Class declared final is a final class ");   
           System.out.println("2) Final classes are created so the methods implemented by that class cannot be overridden ");   
           System.out.println("3) It cant be overridden ");   
           System.out.println("4) All of the above ");
          
           System.out.println();
          
           menuChoice = input.nextInt();
          
           if(menuChoice < 0){
               System.out.println("Number must be between 1,2,3,4 or 0 to stop");
           }
          
           //if user enters anything greater than three invalid number
           else if(menuChoice > 4){
               System.out.println("Number must be between 1,2,3,4 or 0 to stop");
           }
          
           if(menuChoice == 4){
               correctCount++;
           }
          
           if(menuChoice == 0){
               break;
           }
          
           System.out.println(" What is the size of int variable?");   
           System.out.println("1) 8 bit ");   
           System.out.println("2) 32 bit ");   
           System.out.println("3) 12 bit ");   
           System.out.println("4) 64 bit ");
          
           System.out.println();
          
           menuChoice = input.nextInt();
          
           if(menuChoice < 0){
               System.out.println("Number must be between 1,2,3,4 or 0 to stop");
           }
          
           //if user enters anything greater than three invalid number
           else if(menuChoice > 4){
               System.out.println("Number must be between 1,2,3,4 or 0 to stop");
           }
          
           if(menuChoice == 2){
               correctCount++;
           }
          
           if(menuChoice == 0){
               break;
           }
          
           if(correctCount == 3){
               System.out.println("Excellent");
           }
          
           else if(correctCount == 2){
               System.out.println("Very Good");
           }
          
           else{
               System.out.println("It is time to start to learning Java");
           }
          
           System.out.println();
          
           System.out.println("Do you want to continue? (Enter True to continue False to exit)");
          
           System.out.println();
          
           if(retryTest == false){
               System.out.print("Exiting Program");
               System.exit(0);
           }
       }
      
       System.out.println("Good Bye");
      
   }
  
  
  
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

THAT IS HAPPENING BECAUSE YOU FORGOT TO TAKE THE INPUT FROM THE USER AFTER THE BOOLEAN SITUATION. THE UPDATED CODE IS BELOW:-

YOU JUST NEED TO ADD THIS:-

retryTest = input.nextBoolean();

NOW WHOLE CODE:-

import java.util.*;

public class HomeworkLib {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        System.out.println("JAVA QUIZ");

        System.out.println("This quiz includes three questions about the Java Programming Language.");

        System.out.println("Each question has 4 possible answers. (numbered 1,2,3,4)");

        System.out.println("Enter 0 to exit the test.");

        System.out.println();

        boolean retryTest = true;

        while (retryTest == true) {

            int correctCount = 0;

            int menuChoice = 1;

            System.out.println("What is the default value of String variable?");

            System.out.println("1) \"\" ");

            System.out.println("2) \" ");

            System.out.println("3) not defined ");

            System.out.println("4) null ");

            System.out.println();

            menuChoice = input.nextInt();// user input from first question

            if (menuChoice < 0) {

                System.out.println("Number must be between 1,2,3,4 or 0 to stop");

            }

            // if user enters anything greater than three invalid number

            else if (menuChoice > 4) {

                System.out.println("Number must be between 1,2,3,4 or 0 to stop");

            }

            if (menuChoice == 3) {

                correctCount++;

            }

            if (menuChoice == 0) {

                break;

            }

            System.out.println("What is true about a final class?");

            System.out.println("1) Class declared final is a final class ");

            System.out.println(

                    "2) Final classes are created so the methods implemented by that class cannot be overridden ");

            System.out.println("3) It cant be overridden ");

            System.out.println("4) All of the above ");

            System.out.println();

            menuChoice = input.nextInt();

            if (menuChoice < 0) {

                System.out.println("Number must be between 1,2,3,4 or 0 to stop");

            }

            // if user enters anything greater than three invalid number

            else if (menuChoice > 4) {

                System.out.println("Number must be between 1,2,3,4 or 0 to stop");

            }

            if (menuChoice == 4) {

                correctCount++;

            }

            if (menuChoice == 0) {

                break;

            }

            System.out.println(" What is the size of int variable?");

            System.out.println("1) 8 bit ");

            System.out.println("2) 32 bit ");

            System.out.println("3) 12 bit ");

            System.out.println("4) 64 bit ");

            System.out.println();

            menuChoice = input.nextInt();

            if (menuChoice < 0) {

                System.out.println("Number must be between 1,2,3,4 or 0 to stop");

            }

            // if user enters anything greater than three invalid number

            else if (menuChoice > 4) {

                System.out.println("Number must be between 1,2,3,4 or 0 to stop");

            }

            if (menuChoice == 2) {

                correctCount++;

            }

            if (menuChoice == 0) {

                break;

            }

            if (correctCount == 3) {

                System.out.println("Excellent");

            } else if (correctCount == 2) {

                System.out.println("Very Good");

            } else {

                System.out.println("It is time to start to learning Java");

            }

            System.out.println();

            System.out.println("Do you want to continue? (Enter true to continue false to exit)");

            retryTest = input.nextBoolean();

            System.out.println();

            if (retryTest == false) {

                System.out.print("Exiting Program");

                System.exit(0);

            }

        }

        System.out.println("Good Bye");

    }

}

FOR ANY OTHER PLEASE COMMENT.

Add a comment
Know the answer?
Add Answer to:
What is wrong with my code? Trying to fix the Boolean situation and it keeps skipping...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • My Java code from last assignment: Previous Java code: public static void main(String args[]) { //...

    My Java code from last assignment: Previous Java code: public static void main(String args[]) { // While loop set-up boolean flag = true; while (flag) { Scanner sc = new Scanner(System.in); // Ask user to enter employee number System.out.print("Enter employee number: "); int employee_number = sc.nextInt(); // Ask user to enter last name System.out.print("Enter employee last name: "); String last_name = sc.next(); // Ask user to enter number of hours worked System.out.print("Enter number of hours worked: "); int hours_worked =...

  • Need help debugging. Create an application that keeps track of the items that a wizard can...

    Need help debugging. Create an application that keeps track of the items that a wizard can carry. console application which has no errors: import java.util.Scanner; public class Console {        private static Scanner sc = new Scanner(System.in);     public static String getString(String prompt) {         System.out.print(prompt);         String s = sc.nextLine();         return s;     }     public static int getInt(String prompt) {         int i = 0;         boolean isValid = false;         while (!isValid) {             System.out.print(prompt);...

  • My Java code from last assignment: Previous Java code: public static void main(String args[]) { //...

    My Java code from last assignment: Previous Java code: public static void main(String args[]) { // While loop set-up boolean flag = true; while (flag) { Scanner sc = new Scanner(System.in); // Ask user to enter employee number System.out.print("Enter employee number: "); int employee_number = sc.nextInt(); // Ask user to enter last name System.out.print("Enter employee last name: "); String last_name = sc.next(); // Ask user to enter number of hours worked System.out.print("Enter number of hours worked: "); int hours_worked =...

  • Java BNF How to write pseudocode for this snippet? Java code for finding a prime number...

    Java BNF How to write pseudocode for this snippet? Java code for finding a prime number public class Prime { public static void main(String[] args) { int num = 29; boolean flag = false; for(int i = 2; i <= num/2; ++i) { // condition for nonprime number if(num % i == 0) { flag = true; break; } } if (!flag) System.out.println(num + " is a prime number."); else System.out.println(num + " is not a prime number."); } }

  • Consider the interface Predicate defined as follows. interface Predicate { boolean eval(int j); } Recall that...

    Consider the interface Predicate defined as follows. interface Predicate { boolean eval(int j); } Recall that you can check if an integer “i” is even by using the expression “i%2 == 0”. Write a class IsEven that determines whether a number is even: class IsEven implements Predicate { public boolean eval(int j) { } } For example, the following code Predicate p = new IsEven(); if ( p.eval(2)) { System.out.println("2 is even"); } if (! p.eval(3)) { System.out.println("3 is not...

  • Java Assignment Calculator with methods. My code appears below what I need to correct. What I...

    Java Assignment Calculator with methods. My code appears below what I need to correct. What I need to correct is if the user attempts to divide a number by 0, the divide() method is supposed to return Double.NaN, but your divide() method doesn't do this. Instead it does this:     public static double divide(double operand1, double operand2) {         return operand1 / operand2;     } The random method is supposed to return a double within a lower limit and...

  • Debug the following java code so that it will have given out put at the bottom....

    Debug the following java code so that it will have given out put at the bottom. Make sure to fix all syntax and logical errors. import java.util.Scanner; public class Graphs { // draws 5 histograms public void drawHistograms() Scanner input = new Scanner( System.in ); int number1 = 0; // first number int number2 = 0; // second number int number3 = 0; // third number int number4 = 0; // fourth number int number5 = 0; // fifth number...

  • Debug and fix the Java console application that uses 2 dimensional arrays but the application does...

    Debug and fix the Java console application that uses 2 dimensional arrays but the application does not compile nor execute. Student enters only integers to select courses for registration from a menu. No data type validation of the user menu selection is checked or required. The program terminates only when the student closes it. No registration of other courses not displayed by the program . No registration more than once for the same course. No registration for more than 9...

  • Java - Car Dealership Hey, so i am having a little trouble with my code, so...

    Java - Car Dealership Hey, so i am having a little trouble with my code, so in my dealer class each of the setName for the salesman have errors and im not sure why. Any and all help is greatly appreciated. package app5; import java.util.Scanner; class Salesman { private int ID; private String name; private double commRate; private double totalComm; private int numberOfSales; } class Car { static int count = 0; public String year; public String model; public String...

  • [Java] PLEASE FIX MY CODE I think I'm thinking in wrong way. I'm not sure what...

    [Java] PLEASE FIX MY CODE I think I'm thinking in wrong way. I'm not sure what is wrong with my code. Here'e the problem: Write a class SemiCircle that represents the northern half of a circle in 2D space. A SemiCircle has center coordinates and a radius. Define a constructor: public SemiCircle(int centerX, int centerY, int theRadius) Implement the following methods: public boolean contains(int otherX, int otherY) returns true if the point given by the coordinates is inside the SemiCircle....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT