I have to create a complete java program, which generates 10 randon numbers between 1 and 100. Each time a random number is generated, i have to ask the user to guess the number. If his guess is within 10 of the random number, he earns 10 points. If his guess is more than 10 from the random number, he earns no points. After each guess, i have to display the random number, the guess, the number of points(if any) earned bu the guess, and the user's score. The user have 3 times to guess. Also for each guess i have to give a feed back such as "the number is lower" so that the next guess will be closer. After 10 random numbers have been generated and 10 guesses have been entered, i have to give the user the choice
a)to play a new round of 10 or
b)to terminate. At termination, print the average score of all the rounds played by the user.
import javax.swing.JOptionPane; public class RandomNum { private int x, sum,points;
public RandomNum() { x = 0; sum = 0; }
public int getRandomValue() { x = (int)(Math.random()*1000) % 10 + 1; return x; }
public int getGuess() { int points = 0; int y;
String input = JOptionPane.showInputDialog("Guess a number between 1 - 100"+ "\n"); y = Integer.parseInt(input); if (Math.abs(x-y)<=10) { System.out.println("You are getting closer"+ "\n"); points += 10; } else System.out.println("Give a lower number"+ "\n"); return points; } }
class TestRandomNum { public static void main (String [] args) { RandomNum value = new RandomNum(); int counter = 1; int randValue; int total,sum;
while (counter<= 3) { System.out.println("Your Total points: " + value.getGuess()+ "\n"); randValue = value.getRandomValue(); System.out.println("The random number was: "+ randValue + "\n"); counter ++; }
// System.out.println("The sum is:" + value.get()); System.exit(0); } }
RandomNum value = new RandomNum();
int counter = 1;
int tenCounter = 1;
int randValue;
int total,sum;
randValue = value.getRandomValue();
int points = 0;
while (true)
{
while (counter<= 3)
{
points = points+value.getGuess();
System.out.println("Your Total points: " +points + "\n");
counter ++;
}
System.out.println("The random number was: "+ randValue + "\n");
randValue = value.getRandomValue();
tenCounter =tenCounter+counter;
counter = 1;
if (tenCounter>10)
{
System.out.println("tenCounter::"+tenCounter);
String input = JOptionPane.showInputDialog("Do you want to play another round ofTen[Yes\\No]"+ "\n");
if (input.equalsIgnoreCase("no")||input.equalsIgnoreCase("n"))
{
System.exit(0);
}
tenCounter = 1;
}
}
Write code for average also. Change code to catch exception if user does not input any number value.