1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
alexandr1967 [171]
3 years ago
6

Write a program that generates a random number and asks the user to guess what the number is. If the user's guess is higher than

the random number, the program should display "Too high, try again." if the user's guess is lower than the random number, the program should display "Too low, try again." The program should use a loop that repeats until the user correctly guesses the random number. You shall also keep a count of the number of guesses that the user makes. When the user correctly guesses the random number, the program should display the number of guesses. Now add another loop to ask the user if he or she wishes to play the guessing game again. If so, the loop should repeat, otherwise it should terminate.So far, this is what my program looks like, any help or suggestions would be greatly appreciated!// This program generates a random number and askes the user to guess what the number isimport java.util.Scanner;import java.util.Random;public class GuessingGame{public static void main(String [] args){//Create a scanner object to read from the keyboardScanner kb = new Scanner(System.in);//Create a random objectRandom rand = new Random();//Identifier declarationsint num = rand.nextInt(100) + 1;int guess = 0;int count = 0;int guesses = 0;do{System.out.println("Guess what number I have (1-100)? ");guess = kb.nextInt();guesses ++;if(num > guess) {System.out.println("Too high, try again.");} else if(num < guess) {System.out.println("Too low, try again.");} else {System.out.println("You're right, the number is" + num);System.out.println("You guessed" + guesses + "times");}}while(guess!=num);}}
Computers and Technology
1 answer:
JulsSmile [24]3 years ago
3 0

Answer:

import java.util.Scanner;

import java.util.Random;

public class Guesser {

// Create a scanner object to read from the keyboard

static Scanner kb = new Scanner(System.in);

// Create a random object

static Random rand = new Random();

public static void main(String[] args) {

 playGame();

}

public static void playGame() {

 // Identifier declarations

 int num = rand.nextInt(100) + 1;

 int guess = 0;

 int count = 0;

 int guesses = 0;

 do {

  System.out.println("Guess what number I have (1-100)? ");

  guess = kb.nextInt();

  guesses++;

  if (num > guess) {

   System.out.println("Too high, try again.");

  } else if (num < guess) {

   System.out.println("Too low, try again.");

  } else {

   System.out.println("You're right, the number is " + num);

   System.out.println("You guessed " + guesses + " times");

   //Ask the user if they want to play again

   //If yes, recursively call the playGame method again

   //Otherwise, break out of the loop

   System.out.println("Do you want to play again? (Y or N)");

   String answer = kb.next();

   if (answer.equalsIgnoreCase("Yes") || answer.equalsIgnoreCase("Y")) {

    playGame();

   }

   else {

    break;

   }

  }

 } while (guess != num);

}

}

Explanation:

There are a few things you need to do.

1. Make the creation of the objects of the Scanner and Random classes global. In other words, make them global variables.

2. Create a method, say playGame(), that is called, recursively, every time the user needs to play or replay the game.  

The source code file (Guesser.java) has been attached to this answer.

Note: You might want to consider also reducing the range of guess. I'd rather you reduced it to 1-10 rather than what you have. This will make the game much of a fun. With the range you currently have, the user might never guess right.

Hope it helps!

You might be interested in
Is part of a computer's hardware that executes each instruction in a program?
zlopas [31]
The CPU, or central processing unit, is the hardware that takes most of the load when running a program. It will perform complex calculations and execute every instruction as the program continues.

The GPU, or graphics procession unit, is the hardware that could also play a major roll in calculating the instructions to execute a program, though this depends on how graphically intensive the program is. For example, games would use a lot more GPU power than say a simple calculator application.
7 0
4 years ago
What are the four components of the Universal Systems Model?
kakasveta [241]

Answer:

output, process, input, and feedback

Explanation:

5 0
4 years ago
One type of CAPTCHA is ________. a rootkit virus the wavy hard-to-read letter and number sequence that you type to prove that yo
sergejj [24]
<h2>password method</h2>

Explanation:

Rootkit virus is one type of virus which is hidden about its existence and tries to affect the maximum number of system.

Password method: This is a graphical method to identify the text hidden in the signature and confirming that it is not a robot or any other system to protect the system from hacking. This would be really hard-to-read and thus ensures safety.

Antivirus software: This is a system to protect the system, when the virus tries to attack the system. An indication will be given through the software to the user.

4 0
3 years ago
Tim is in charge of the upcoming interschool baketball tournamnent. He wants to arrange all of the teams and their members in al
Scorpion4ik [409]
C sort will be the answer
5 0
3 years ago
JAVA PROGRAMMING
VashaNatasha [74]

Answer:

See explaination

Explanation:

import java.util.Scanner;

public class GtldValidation {

public static void main (String [ ] args) {

Scanner scnr = new Scanner(System.in);

// Define the list of valid core gTLDs

String [ ] validCoreGtld = { ".com", ".net", ".org", ".info" };

// FIXME: Define an array named validRestrictedGtld that has the names

// of the restricted domains, .biz, .name, and .pro

String [] validRestrictedGtld={".biz",".name",".pro"};

String inputName = "";

String searchName = "";

String theGtld = "";

boolean isValidDomainName = false;

boolean isCoreGtld = false;

boolean isRestrictedGtld = false;

int periodCounter = 0;

int periodPosition = 0;

int i = 0;

System.out.println("\nEnter the next domain name (<Enter> to exit): ");

inputName = scnr.nextLine();

while (inputName.length() > 0) {

searchName = inputName.toLowerCase();

isValidDomainName = false;

isCoreGtld = false;

isRestrictedGtld = false;

// Count the number of periods in the domain name

periodCounter = 0;

for (i = 0; i < searchName.length(); ++i) {

if (searchName.charAt(i) == '.') {

++periodCounter;

periodPosition = i;

}

}

// If there is exactly one period that is not at the start

// or end of searchName, check if the TLD is a core gTLD or a restricted gTLD

if ((periodCounter == 1) &&

(searchName.charAt(0) != '.') &&

(searchName.charAt(searchName.length() - 1) != '.')) {

isValidDomainName = true;

}

if (isValidDomainName) {

// Extract the Top-level Domain name starting at the period's position. Ex:

// If searchName = "example.com", the next statement extracts ".com"

theGtld = searchName.substring(periodPosition);

i = 0;

while ((i < validCoreGtld.length) && (!isCoreGtld)) {

if(theGtld.equals(validCoreGtld[i])) {

isCoreGtld = true;

}

else {

++i;

}

}

// FIXME: Check to see if the gTLD is not a core gTLD. If it is not,

// check to see whether the gTLD is a valid restricted gTLD.

// If it is, set isRestrictedGtld to true

}

System.out.print("\"" + inputName + "\" ");

if (isValidDomainName) {

System.out.print("is a valid domain name and ");

if (isCoreGtld) {

System.out.println("has a core gTLD of \"" + theGtld + "\".");

}

else if (isRestrictedGtld) {

System.out.println("has a restricted gTLD of \"" + theGtld + "\".");

}

else {

System.out.println("does not have a core gTLD."); // FIXME update message

}

}

else {

System.out.println("is not a valid domain name.");

}

System.out.println("\nEnter the next domain name (<Enter> to exit): ");

inputName = scnr.nextLine();

}

return;

}

}

8 0
3 years ago
Other questions:
  • The __is the temporary storange location for text when it is cut from a document
    12·1 answer
  • A storyboard is an example of an implementation tool.<br><br> A.<br> True<br><br> B.<br> False
    8·2 answers
  • Which of the following is not a common network architecture type?
    9·1 answer
  • What network setting do i need for a workgroup?
    5·1 answer
  • In which type of attack do you get malicious code in links from seemingly reliable websites?
    14·1 answer
  • When users talk about font size,
    10·2 answers
  • Q. Which protocol would best serve to authorize users to access directory services?
    8·1 answer
  • Which of the following contributes to your active digital footprint
    12·2 answers
  • JAVA
    7·1 answer
  • How can knowing internal and external parts of computer help me
    12·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!