This discussion is archived
7 Replies Latest reply: Sep 6, 2012 9:40 AM by doremifasollatido RSS

Random Generating Number Problems

960425 Newbie
Currently Being Moderated
I have been working on this random number generation game were you have to guess a number between 0-15 and it wont run please help me to see what is wrong with my code.


     import java.until.Random;
     import java.until.Scanner;
     
     public class Main
     {
          public static void main(String[] args)
          {
               System.out.print("input Number:");
          
               // Get number between 0-15
               int in = getInput();
               
               //Generate Random Number
               int val = getRandomNumber();
               
               //Test if numbers are the same
               if (in == val)
                    System.out.println("We Win");
               else
                    System.out.println("We Lose");
                    
               System.out.println("our number:" + in);
               System.out.println("Computers Number:" + val);
               
          
               
          }
          
          public static int getInput();
          {
               Scanner scan = new Scanner(System.in);
               
               String temp = scan.next();
               
               int res = Integer.parseInt(temp);
               
               if (res < 0)
                    res=0;
               if (res > 15)
                    res=15;
               
               return res;
          }
          
          
          public static int `()
          {
               Random rand = new Random();
               
               int res = rand.nextInt (16);
               
               return res;
          }
          
     }
  • 1. Re: Random Generating Number Problems
    EJP Guru
    Currently Being Moderated
    Define "won't run".
  • 2. Re: Random Generating Number Problems
    960425 Newbie
    Currently Being Moderated
    when I push the run button in Eclipse it give's me this error and wont let me use the program


    Exception in thread "main" java.lang.Error: Unresolved compilation problem:
         The method getRandomNumber() is undefined for the type Main

         at Main.main(Main.java:15)
  • 3. Re: Random Generating Number Problems
    doremifasollatido Journeyer
    Currently Being Moderated
    Look at the last method in your original post. It looks like you typed over the desired method name (i.e., getRandomNumber) with a backward apostrophe. You will have a few other compilation problems, too, if the code posted is exactly what you have in Eclipse.
  • 4. Re: Random Generating Number Problems
    DrClap Expert
    Currently Being Moderated
    957422 wrote:
         The method getRandomNumber() is undefined for the type Main
    So your code tries to call a method called "getRandomNumber()" but there is no such method.

    Either fix the code to call a method which does exist, or write that method and add it into your class.
  • 5. Re: Random Generating Number Problems
    817788 Newbie
    Currently Being Moderated
    Firstly: package java.until - absent in java.
    Secondly: you didn't define method getRandomNumber().
    So, there is the question: is this your code or it is a home-work that you don't know how to do?

    Working code:
    import java.util.Random;
    import java.util.Scanner;
    
    public class Main{
    
        public static void main(String[] args){
            System.out.print("input Number:");
            // Get number between 0-15
            int in=getInput();
            //Generate Random Number
            int val=getRandomNumber();
            //Test if numbers are the same
            if(in==val){
                System.out.println("We Win");
            }else{
                System.out.println("We Lose");
            }
            System.out.println("our number:"+in);
            System.out.println("Computers Number:"+val);
        }
    
        public static int getInput(){
            Scanner scan=new Scanner(System.in);
            String temp=scan.next();
            int res=Integer.parseInt(temp);
            if(res<0){
                res=0;
            }
            if(res>15){
                res=15;
            }
            return res;
        }
    
        public static int getRandomNumber(){
            Random rand=new Random();
            int res=rand.nextInt(16);
            return res;
        }
    }
  • 6. Re: Random Generating Number Problems
    960425 Newbie
    Currently Being Moderated
    this is for a project im doing at school
  • 7. Re: Random Generating Number Problems
    960425 Newbie
    Currently Being Moderated
    Thanks for every ones help!

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points