Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The goal of this exercise is to program a \"Guess My Number\" game. When it\'s f

ID: 3885768 • Letter: T

Question

The goal of this exercise is to program a "Guess My Number" game. When it's finished, it will work like this: I'm thinking of a number between 1 and 100 (including both). Can you guess what it is? Type a number: 45 Your guess is: 45 The number I was thinking of is: 14 You were off by: 31 To choose a random number, you can use the Random class in java.util. Here's how it works: import java.util.Random: public class GuessStarter { public static void main(String[] args) { // pick a random number Random random = new Random(): int number = random.nextInt(100) + 1: System.out.println(number): } }

Explanation / Answer

import java.util.Random;
import java.util.Scanner;
public class GuessStarter{

public static void main(String args[]){

//pick a random number
Random random = new Random();
  
Scanner sc = new Scanner(System.in);
  
System.out.print("I'm thinking of a number between 1 and 100(including both). Can you guess what it is? Type your number :");
  
//get user input
int inp = sc.nextInt();
  
//generate random number
int number = random.nextInt(100) + 1;
  
System.out.println("The number I was thinking of is : "+number);

System.out.println("You were off by : "+(number-inp));

}

}

/*
sample output

I'm thinking of a number between 1 and 100(including both). Can you guess what it is?
Type your number : 30
The number I was thinking of is : 49
You were off by : 19

*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote