Java School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a Java Expert!

Join 306,986 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,968 people online right now. Registration is fast and FREE... Join Now!




JAVA Random Number

 
Reply to this topicStart new topic

> JAVA Random Number

computerfox
*****



post 15 Aug, 2009 - 09:35 AM
Post #1


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:

CODE


import java.util.*;



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

CODE


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:

CODE


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.

CODE


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.

CODE

    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

CODE


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:

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!
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 


Lo-Fi Version Time is now: 11/21/09 05:35AM

Live Java Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month