The Artima Developer Community
Sponsored Link

Java Answers Forum
Random Numbers

2 replies on 1 page. Most recent reply: Mar 22, 2004 11:28 PM by mausam

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
Scarlett

Posts: 3
Nickname: scar78
Registered: Mar, 2004

Random Numbers Posted: Mar 22, 2004 7:51 PM
Reply to this message Reply
Advertisement
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);
}
}


Scarlett

Posts: 3
Nickname: scar78
Registered: Mar, 2004

Re: Random Numbers Posted: Mar 22, 2004 7:55 PM
Reply to this message Reply
my problem is that i can't get the sum of all the points that the user earn, nor the total average...
can somebody help me?

mausam

Posts: 243
Nickname: mausam
Registered: Sep, 2003

Re: Random Numbers Posted: Mar 22, 2004 11:28 PM
Reply to this message Reply
modify ur test class with something like
		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.

Flat View: This topic has 2 replies on 1 page
Topic: networking Previous Topic   Next Topic Topic: MouseListener freezing up the repaint()

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use