I\'m in java one and need help with this code. please follow the directions give
ID: 3682882 • Letter: I
Question
I'm in java one and need help with this code. please follow the directions given and test your program to make sure it works as it is required. I will apreciate your help. thank you.
Programming Concepts
1. Random Number Generation
Exercise Location
For this exercise, you will copy a program from my directory into yours. It doesn't matter what directory this exercise is located in. You will need to copy this command and paste it into Loki:
cp ~lkoperski/1404_Lab/Exercises/Exercise10.java .
Exercise Description
^ You will create a game that has the user guess a number between 1 and 10.
^ Generate a random number that the user will attempt to guess.
^ If the user guesses a number that is lower than the answer, print a message that says that.
^ If the user guesses a number that is higher than the answer, print a message that says that.
^ If the user guesses a number that is equal to the answer, print a message that says that.
user@loki:~$ java Exercise10
I am thinking of a number between 1 and 10!
Enter a guess: 5 Too low!
Enter a guess: 8 Too high!
Enter a guess: 6 Too low!
Enter a guess: 7 You guessed it!
Explanation / Answer
import java.util.*;
public class HelloWorld{
public static void main(String []args){
int choice,randomNum,flag=1;
Random rand=new Random();
randomNum=rand.nextInt((10 - 1) + 1) + 1;
Scanner read=new Scanner(System.in);
System.out.println("I am thinking of a number between 1 and 10");
while(flag==1)
{
System.out.print(" Enter a guess: ");
choice=read.nextInt();
if(choice==randomNum)
{
System.out.print("You guessed it!");
flag=0;
}
else
if(choice>randomNum)
System.out.print("Too High!");
else
System.out.print("Too Low!");
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.