Question

In: Computer Science

Hello! I need to add a method that will display the total number of times the...

Hello! I need to add a method that will display the total number of times the user answered the question correctly, how many time he was right or wrong and the percentage of time they were correct in answering.


import java.util.Random;
import java.util.Scanner;

public class Test {
    private static int getUserInput() {
        int n;
        Scanner scanner = new Scanner(System.in);
        n = scanner.nextInt();
        return n;
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String choice = "Yes";
        Random random = new Random();
        int[] userdata = new int[100];
        int count = 0;
        while (!choice.equals("No")) {
            int randomInt = 2 * (random.nextInt(5) + 1);
            System.out.println("write the number in half " + randomInt + "?");
            userdata[count++] = randomInt;
            int userInput = getUserInput();
            if (userInput == (randomInt / 2)) {
                System.out.println("That is correct!");
            }
            else {

                System.out.println("That is incorrect!");
            }
            System.out.print("Generate another random number?");
            choice = scanner.next();
        }
        int min = userdata[0], max = userdata[0];
        for (int i = 0; i < count; i++) {
            if (userdata[i] > max) max = userdata[i];
            if (userdata[i] < min) min = userdata[i];
        }

        System.out.println("The highest random number you were given: " + max);
        System.out.println("The lowest random number you were given: " + min);
    }
}

Solutions

Expert Solution

import java.util.Random;
import java.util.Scanner;

public class Test {
private static int getUserInput() {
int n;
Scanner scanner = new Scanner(System.in);
n = scanner.nextInt();
return n;
}

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String choice = "Yes";
Random random = new Random();
int[] userdata = new int[100];
int count = 0,right = 0,wrong = 0;
  
while (!choice.equals("No")) {
int randomInt = 2 * (random.nextInt(5) + 1);
System.out.println("write the number in half " + randomInt + "?");
userdata[count++] = randomInt;
int userInput = getUserInput();
if (userInput == (randomInt / 2)) {
System.out.println("That is correct!");
right++;
}
else {

System.out.println("That is incorrect!");
wrong++;
}
System.out.print("Generate another random number?");
choice = scanner.next();
}
int min = userdata[0], max = userdata[0];
for (int i = 0; i < count; i++) {
if (userdata[i] > max) max = userdata[i];
if (userdata[i] < min) min = userdata[i];
}

System.out.println("The highest random number you were given: " + max);
System.out.println("The lowest random number you were given: " + min);
display(count,right,wrong);
}

private static void display(int count, int right, int wrong) {
System.out.println("Right Answer: "+right);
System.out.println("Wrong Answer: "+wrong);
System.out.println("Right percent: "+((float)right/count)*100+"%");
}
}

/* OUTPUT */

run:
write the number in half 4?
2
That is correct!
Generate another random number?Yes
write the number in half 2?
5
That is incorrect!
Generate another random number?No
The highest random number you were given: 4
The lowest random number you were given: 2
Right Answer: 1
Wrong Answer: 1
Right percent: 50.0%


Related Solutions

I need to display the questions that the user answered total, wrong and right as well...
I need to display the questions that the user answered total, wrong and right as well as the average percentage they got correct of the following code please! public class TestCode { public static void main(String[] args) { String choice = "Yes"; Random random = new Random(); Scanner scanner = new Scanner(System.in); while(!choice.equals("No")){ int randomInt = 2 * (random.nextInteger(5) + 1); System.out.println(randomInteger); System.out.print("Want another random number (Yes / No)? "); choice = scanner.next(); } } }
I need the vhdl code to display the binary number specified by an 8 input dip...
I need the vhdl code to display the binary number specified by an 8 input dip switch on a 7 segment display.
I need to write a function that counts the number of total wins and losses. I...
I need to write a function that counts the number of total wins and losses. I have a text file called analysis_data.txt with 1000 lines written Won Loss Won Loss Won Won ... The function need to do the following: Opens the analysis_data.txt file and reads through all the data, counting number of 'Won' and 'Loss' words stored in the file. Returns two integers: count of wins and count of losses from the text file. **Can't use the break statement
Hello, I need to use Irac method to answer this case using contract terms Mustafa decides...
Hello, I need to use Irac method to answer this case using contract terms Mustafa decides he wants to encourage his son Karim to stop playing video games. Mustafa offers to give Karim a car if he “stops spending too much of his time playing on his Xbox over the next month”. They sign a written contract confirming this arrangement with the exact language above. Karim stops playing video games for three weeks. Mustafa tells Karim that the car is...
Hello I need some assistance with these questions I need not so long answers but not...
Hello I need some assistance with these questions I need not so long answers but not too short please Give some examples of How much decisions. What are the implicit costs of having an Airbnb in your neighborhood? What is marginal analysis? What is marginal cost? Under what conditions do marginal costs increase?
Hello, I need to develop a presentation on the International Trade Theory. I need at least...
Hello, I need to develop a presentation on the International Trade Theory. I need at least information for 3 slides and a conclusion. Thanks!
Hello, I need some advice on the case below. I need to know some arguments for...
Hello, I need some advice on the case below. I need to know some arguments for how Kant's ethics applies to this case, and why other people who believe in the Kantean theory of ethics would also support the arguments. I need to use Kants theory to determine if this is morally correct or incorrect. Casino Gambling on Wall Street Case 4.5 Casino Gambling on Wall Street CDO stands for “ collateralized debt obligation,” and before the financial meltdown of...
Hello! Html question. I am trying to add a background to a page. How would i...
Hello! Html question. I am trying to add a background to a page. How would i do this? I have tried using a jpg. The code is posted below. i used the background-image: url function. Any help of different ways to do this would be appreciated! Thank you! CODE: <!DOCTYPE html> <html> <head> <style> ul { list-style-type: none; margin: 0; padding: 0; overflow: hidden; background-color: #333; } li { float: left; } li a { display: block; color: white; text-align:...
Hello, I Have create this code and Tried to add do while loop but it gives...
Hello, I Have create this code and Tried to add do while loop but it gives me the error in string answar; and the areas where I blod So cloud you please help me to do ( do while ) in this code. // Program objective: requires user to input the data, program runs through the data,calcualtes the quantity, chacks prices for each iteam intered,calautes the prices seperatly for each item, and calculates the amount due without tax, and then...
Hello, I need an Implicit cost and Explicit cost introduction and outline
Hello, I need an Implicit cost and Explicit cost introduction and outline
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT