Page 1 of 1

JAVA Random Number Rate Topic: -----

#1 computerfox  Icon User is offline

  • straight vegetarian kid

Reputation: 50
  • View blog
  • 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;
			line=("Number: "+NUM+"\nYour number: "+number);
		}
		
		else
			line=("Number: "+NUM+"\nYour number: "+number);
			   
				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;
			line=("Number: "+NUM+"\nYour number: "+number);
		}
		
		else
			line=("Number: "+NUM+"\nYour number: "+number);
			   
				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