Question

In: Computer Science

Write a program in java that deliberately contains an endless or infinite while loop. The loop...

Write a program in java that deliberately contains an endless or infinite while loop. The loop should generate multiplication questions with single-digit random integers. Users can answer the questions and get immediate feedback. After each question, the user should be able to stop the questions and get an overall result. See Example Output.

Example Output

What is 7 * 6 ? 42

Correct. Nice work!

Want more questions y or n ? y

What is 8 * 5 ? 40

Correct. Nice work!

Want more questions y or n ? y

What is 5 * 5 ? 25

Correct. Nice work!

Want more questions y or n ? y

What is 8 * 9 ? 66

Incorrect. The product is 72

Want more questions y or n ? n

You scored 3 out of 4

Solutions

Expert Solution

import java.util.Random;
import java.util.Scanner;
public class MultiplicationQuestions {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        Random rand = new Random();
        int n1, n2, result, total = 0, correct = 0;
        char ch = 'y';
        while(ch == 'y'){
            n1 = 1 + rand.nextInt(9);
            n2 = 1 + rand.nextInt(9);
            System.out.print("What is "+n1+" * "+n2+" ? ");
            result = scan.nextInt();
            if(result==n1*n2){
                System.out.println("Correct. Nice work!");
                correct++;
            }
            else{
                System.out.println("Incorrect. The product is "+(n1*n2));
            }
            System.out.print("Want more questions y or n ? ");
            ch = scan.next().charAt(0);
            total++;
        }
        System.out.println("You scored "+correct+" out of "+total);
    }
}


Related Solutions

​​​​​​​For java program. Write a while loop that will let the user enter a series of...
​​​​​​​For java program. Write a while loop that will let the user enter a series of integer values and compute the total values and number of values entered. An odd number will stop the loop. Display the number of iterations and the total of the values after the loop terminates. for Loop Write a for loop to display all numbers from 13 - 93 inclusive, ending in 3. Write a for loop to display a string entered by the user...
*Java program* Use while loop 1.) Write a program that reads an integer, and then prints...
*Java program* Use while loop 1.) Write a program that reads an integer, and then prints the sum of the even and odd integers. 2.) Write program to calculate the sum of the following series where in is input by user. (1/1 + 1/2 + 1/3 +..... 1/n)
QUESTION Write the main while loop for a Java program that processes a set of data...
QUESTION Write the main while loop for a Java program that processes a set of data as follows: Each line of the input consists of 2 numbers representing a quantity and price. Your loop should: 1. Read in the input 2. Calculate the tax. Tax will be 8.875% of price and will only be applicable if price is more than 110. Calculate the new price which is the price plus tax. 3. Calculate the final price by multiplying quantity by...
Using a while loop. Write a JAVA program that asks a user for an integer between...
Using a while loop. Write a JAVA program that asks a user for an integer between 1 and 9. Using the user input, print out the Fibonaccci series that contains that number of terms. Sample output: How many terms would like printed out for the Fibonacci Sequence? 7 Fibonacci Series of 7 numbers: 0 1 1 2 3 5 8
Write a Java program that uses a while loop to do the following: Repeatedly asks the...
Write a Java program that uses a while loop to do the following: Repeatedly asks the user to enter a number or -1 to exit the program. Keeps track of the smallest and largest numbers entered so far: You will need a variable for the smallest number and another variable for the largest number. Read the first number the user enters, and if it is not -1 set the smallest and largest variables to that number. Use a loop to...
Write a Java program using jGRASP directions are as follows: Uses a while loop to print...
Write a Java program using jGRASP directions are as follows: Uses a while loop to print the numbers from 3 - 19. Uses a do-while loop to print the numbers from 42 - 56. Uses a for loop to print the numbers from 87 - 95. Asks the user for 2 numbers. Uses a loop to print all numbers between the given numbers, inclusive. Note: Consider that your user's second number can be lower! (see example below) Note: Also consider...
every code should be in java Program 1: Write a while loop that sums the integers...
every code should be in java Program 1: Write a while loop that sums the integers from 1 to 10, excluding 3 and 6. Print the sum. [Hint: Use logical operators] Program 2: Write a for loop that attempts to display the numbers from 1 to 10, but terminates when the control variable reaches the value 6. Program 3: Write a do...while loop that prints the integers from 10 to 0, inclusive.
Write an example of a Python while loop that is infinite, calls the functions first(), second()...
Write an example of a Python while loop that is infinite, calls the functions first(), second() and third() in that order in the body of the loop, but skips calling second() and third() if first() returns a value of False, and exits if second() returns a value of False. Declare a Python function named sumEm() that accepts three numeric parameters and returns the sum of the three numbers. Assume sumEm() is declared in a module named myMod.py, which your program...
Write a Java program using using WHILE loop to find the sum of all integers between...
Write a Java program using using WHILE loop to find the sum of all integers between 200 to 250 which are divisible by 7. Sample Output: Numbers between 200 and 250, divisible by 7: 203 210 217 224 231 238 245 The sum is: 1568
Re-write following while loop into Java statements that use a Do-while loop. Your final code should...
Re-write following while loop into Java statements that use a Do-while loop. Your final code should result in the same output as the original code below. int total = 0; while(total<100) { System.out.println("you can still buy for"+(100-total)+"Dollars"); total=total+5; }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT