#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    7
    Rep Power
    0

    Calling method into main


    I am new to Java and trying to create a program that is a guessing game. The user inputs a number 1-20. The program tells the user if it is correct or not. The method giveHint() must display a hint but not return a value. The program must keep asking the user to try again until it is correct. I am not sure how to call the method into the main. Any help or advice would be appreciated. I thank you for your time.
    Code:
    package guessinggame;
    import java.util.Scanner;
    
    public class GuessingGame {
    
    
        public static void giveHint(int guess,int answer) {
                 
            if (answer < guess){
                System.out.println("Incorrect! \n Hint: Try a higher number");
                
            }
            else if (answer > guess){
                System.out.println("Incorrect! \n Hint: Try a lower number");
                
            }
        }    
      
        public static void main(String[] args) {
            
            int guess;
            int answer;
            
            Scanner my_input = new Scanner(System.in);
            answer = (int)(Math.random()*20) + 1;
            System.out.println("Enter a number between 1 and 20");
            guess = my_input.nextInt();
            if (answer == guess){
                System.out.println("Correct!");
                }      
          else if (answer < guess || answer > guess) {
              //This is where I need to import the giveHint() method 
                 }
        } 
          
         
        
    }
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    7
    Rep Power
    0
    Here is my current progress. I just cant seem to get it to compile thought. Thoughts?
    Code:
    package guessinggame;
    import java.util.Scanner;
    
    public class GuessingGame {
    
             public static void main(String[] args) {
            
            int guess;
            int answer;
            
            Scanner my_input = new Scanner(System.in);
            answer = (int)(Math.random()*20) + 1;
            System.out.println("Enter a number between 1 and 20");
            guess = my_input.nextInt();
                if (answer == guess){
                System.out.println("Correct!");
                }      
                else  {
                giveHint();
                 }
        } 
          
            public static void giveHint(int answer, int guess) {
                 
            if (answer < guess){
                System.out.println("Incorrect! \n Hint: Try a higher number");
                
            }
            else if (answer > guess){
                System.out.println("Incorrect! \n Hint: Try a lower number");
                
            }
        }   
            
        
    }
  4. #3
  5. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,696
    Rep Power
    347
    I just cant seem to get it to compile
    Please copy the full text of the error messages and paste it here.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    7
    Rep Power
    0
    Originally Posted by NormR
    Please copy the full text of the error messages and paste it here.
    Thanks NormR, but I think I got it. I am not receiving errors. Here is my code:

    Code:
    package guessinggame;
    import java.util.Scanner;
    
    public class GuessingGame {
    
       public static void giveHint(int answer, int guess) {             
            if (answer < guess){
                System.out.println("Incorrect! \n Hint: Try a lower number.");            
            }
            else if (answer > guess){
                System.out.println("Incorrect! \n Hint: Try a higher number.");            
            }
        } 
       
       public static void main(String[] args) {        
            int guess;
            int answer;        
            Scanner my_input = new Scanner(System.in);
            answer = (int)(Math.random()*20) + 1;
                System.out.println("Enter a number between 1 and 20");
            guess = my_input.nextInt();
                while (guess!=answer) {
            giveHint(answer,guess);
            guess = my_input.nextInt();
                 }
                if (answer == guess){
                System.out.println("|***** CORRECT! *****|");
                }
        } 
          
                   
        
    }
  8. #5
  9. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,696
    Rep Power
    347
    Does it work now?

    Can you mark it as solved?
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    7
    Rep Power
    0
    Appears so.

IMN logo majestic logo threadwatch logo seochat tools logo