answer.
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]
2 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]2 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
What is the area of a parallelogram whose vertices are A(−1, 12) , B(13, 12) , C(2, −5) , and D(−12, −5) ? Enter your answer in
11Alexandr11 [23.1K]
<span>Looking at the first two vertices, A and B, we see that the x values have a difference of (-1 - 13) units, or 14 units. This gives us the base of the parallelogram. Next, we look at the difference between the y values between B and C to find the height. In this case, they are (12 - [-5] ) units, or 17 units. The area of a parallelogram is simply base multiplied by height, which is (14 * 17), or 238 square units.</span>
3 0
2 years ago
Read 2 more answers
What answer best explains why improper netiquette is considered dangerous? Individuals who violate user policies are often charg
adell [148]

Answer:

Students become vulnerable to cyberbullying and harassment.

Explanation:

3 0
2 years ago
Read 2 more answers
python (Business: check ISBN-10) An ISBN-10 (International Standard Book Number) consists of 10 digits: d1d2d3d4d5d6d7d8d9d10. T
iogann1982 [59]

Answer:

<em>The programming language is not stated;</em>

<em>However, I'll answer this question using C++ programming language</em>

<em>The program uses few comments; See explanation section for more detail</em>

<em>Also, the program assumes that all input will always be an integer</em>

#include<iostream>

#include<sstream>

#include<string>

using namespace std;

int main()

{

string input;

cout<<"Enter the first 9 digits of an ISBN as a string: ";

cin>>input;

//Check length

if(input.length() != 9)

{

 cout<<"Invalid input\nLength must be exact 9";

}

else

{

 int num = 0;

//Calculate sum of products

for(int i=0;i<9;i++)

{

 num += (input[i]-'0') * (i+1);    

}

//Determine checksum

if(num%11==10)

{

 input += "X";

 cout<<"The ISBN-10 number is "<<input;

}

else

{

 ostringstream ss;

 ss<<num%11;

 string dig = ss.str();

 cout<<"The ISBN-10 number is "<<input+dig;

}

}  

 return 0;

}

Explanation:

string input;  -> This line declares user input as string

cout<<"Enter the first 9 digits of an ISBN as a string: ";  -> This line prompts the user for input

cin>>input;  -> The user input is stored here

if(input.length() != 9)  { cout<<"Invalid input\nLength must be exact 9";  }  -> Here, the length of input string is checked; If it's not equal to then, a message will be displayed to the screen

If otherwise, the following code segment is executed

else  {

int num = 0; -> The sum of products  of individual digit is initialized to 0

The sum of products  of individual digit is calculated as follows

for(int i=0;i<9;i++)

{

num += (input[i]-'0') * (i+1);

}

The next lines of code checks if the remainder of the above calculations divided by 11 is 10;

If Yes, X is added as a suffix to the user input

Otherwise, the remainder number is added as a suffix to the user input

if(num%11==10)  {  input += "X";  cout<<"The ISBN-10 number is "<<input; }

else

{

ostringstream ss;

ss<<num%11;

string dig = ss.str();

cout<<"The ISBN-10 number is "<<input+dig;

}

}  

5 0
2 years ago
____ twisted pair is the least quality twisted pair wire that should be used in a data/voice application.
xxMikexx [17]

Answer:

Category 5 (Cat 5) twisted pair is the least quality twisted pair wire that should be used.

Explanation:

The reason is as follows:

  1. Category 1 twisted pair is not suitable for transmitting data but comes in handy for telephone communications. Hence, it should not be used in a data/voice application.
  2. Category 2 twisted pair is suitable for data transmission, however, it can only transmit at 4Mbps which is very slow. Hence, it should not be used in a data/voice application.
  3. Category 3 twisted pair can transmit data with speed reaching 10Mbps which is slow for voice/data communication.
  4. Category 4 twisted pair can transmit data with speed reaching 16Mbps and is used in token ring networks. It is slow for communicating in a voice/data application.
  5. Category 5 twisted pair is the least qualified to be used in a voice/data application because it can transmit data at speeds that reach 100Mbps.

Hence, Cat 5 or Category 5 twisted pair is the least quality twisted pair that should be used in a data/voice application.

6 0
2 years ago
Write the prototype for a function named showValues. It should accept an array of integers and an integer for the array size as
aalyn [17]

Answer:

void showValues(int [<em>maximum</em><em> </em><em>volume</em>],int);

4 0
2 years ago
Read 3 more answers
Other questions:
  • When your computer runs out of ram, the operating system borrows space from the cpu.
    14·1 answer
  • n the video, McWhorter says that “textspeak” might be a good thing for young people’s brains. Why does he think this?
    13·1 answer
  • Suppose a computer using direct mapped cache has 220 bytes of byte-addressable main memory, and a cache of 32 blocks, where each
    5·1 answer
  • Which mechanisms do we use to transport binary data and memory addresses?
    7·1 answer
  • What technology gets its name from the notion that it ignores the traditional A, B, and C class designations for IP addresses?
    14·1 answer
  • Write a program that removes all spaces from the given input.
    6·1 answer
  • Select which type of computer you would like to begin building, based on the needs of your new job: Frequent travel Maintaining
    7·1 answer
  • Of these two types of programs:a. I/O -bound b. CPU -bound which is more likely to have voluntary context switches, and which is
    6·1 answer
  • Write a MATLAB function named lin_spaced_vector with two inputs and one return value. The first input will be a single real numb
    7·1 answer
  • The Coins class was created to hold all your loose change, kind of like a piggy bank! For this exercise, you are going to simula
    15·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!