Page 1 of 1

JAVA Random Number Rate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=120386&amp;s=272ce4095e715270761ef0a437a5f583&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 computerfox

• straight vegetarian kid

Reputation: 50
• Posts: 3,772
• Joined: 29-January 09

Posted 15 August 2009 - 10:35 AM

Let's take a look at making a guessing game. It's simple and I'm sure there are a couple other ones, but here's my version:

first off you need to do your imports:

```
import java.util.*;

```

You will also need to make a Scanner that is public for later on

```
public static Scanner fox=new Scanner(System.in);

```

since i love making methods (because they're a lot more mobile because you can use the same method over and over again in different parts of the program without copy and pasting the whole code), we will use two main methods:

```
public class numberGame {		 //creates the random number within a range
public static int randomNum(){

}

public static String check(String name,int number,int NUM){ //actual method that checks the numbers

}

```

Since we just declared the methods, let's take a look at what each of them would have to do:

the smaller method is the random number because all you're doing is creating the number. this might be simple, but it's also the key. if you don't make a proper random number, the program wouldn't be doing what it's suppose to.

```
public class numberGame {
public static int randomNum(){

Random gen=new Random();
int NUM=gen.nextInt(5); // generates a number from 0-4

return NUM;//returns that random number
}

```

that was simple enough, no?

now comes the other half of the project.

```	public static String check(String name,int number,int NUM){
name=name.toLowerCase();   //makes it so that you can compare two strings easier
String line;
int value=name.compareTo("fox");   //this is just a hack

//hack making your number and "random" number the same
if(value==0){
NUM=number;
}

else

if(NUM==number)
line="WIN";

//otherwise gives you 2 more chances before telling you, you suck
counter+=1;
while(counter<4){

if(number==NUM){
line="WIN!!!!!!!";
break;
}

if(counter==3){
line="FAIL"
break;
}

if(number>NUM){
System.out.print("Lower: ");
number=fox.nextInt();			//this is where the public Scanner comes in
}

else

if(number<NUM){
System.out.print("Higher: ");
number=fox.nextInt();		   //this is where the public Scanner comes in
}
}

return line;//returns line
}

```

And now the driver method

```
public static void main (String args[]) {

String name;
int number,NUM=randomNum();

System.out.print("Name: ");
name=fox.nextLine();

System.out.print("Number: ");
number=fox.nextInt();

System.out.println(check(name,number,NUM));	//executes check()

}

```

And now the whole code:

```
import java.util.*;

public class numberGame {

public static Scanner fox=new Scanner(System.in);
public static int randomNum(){

Random gen=new Random();
int NUM=gen.nextInt(5);

return NUM;
}
//uses the values in the variables outside of this method
public static String check(String name,int number,int NUM){
name=name.toLowerCase();   //makes it so that you can compare two strings easier
String line;
int value=name.compareTo("fox");   //this is just a hack

//hack making your number and "random" number the same
if(value==0){
NUM=number;
}

else

if(NUM==number)
line="WIN";

//otherwise gives you 2 more chances before telling you, you suck
counter+=1;
while(counter<4){

if(number==NUM){
line="WIN!!!!!!!";
break;
}

if(counter==3){
line="FAIL"
break;
}

if(number>NUM){
System.out.print("Lower: ");
number=fox.nextInt();			//this is where the public Scanner comes in
}

else

if(number<NUM){
System.out.print("Higher: ");
number=fox.nextInt();		   //this is where the public Scanner comes in
}
}

return line;//returns line
}

public static void main (String args[]) {

String name;
int number,NUM=randomNum();

System.out.print("Name: ");
name=fox.nextLine();

System.out.print("Number: ");
number=fox.nextInt();

System.out.println(check(name,number,NUM));

}
}

```

i hope this was helpful. thank you for stopping by. have a great day!

Is This A Good Question/Topic? 0

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }