Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Closed Thread
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 1st, 2012, 01:35 PM
kinison kinison is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 7 kinison User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 57 m 59 sec
Reputation Power: 0
Homework - 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 
             }
    } 
      
     
    
}

Reply With Quote
  #2  
Old October 1st, 2012, 02:41 PM
kinison kinison is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 7 kinison User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 57 m 59 sec
Reputation 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");
            
        }
    }   
        
    
}

Reply With Quote
  #3  
Old October 1st, 2012, 07:32 PM
NormR's Avatar
NormR NormR is online now
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 2,959 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 3 h 17 m 24 sec
Reputation Power: 345
Quote:
I just cant seem to get it to compile

Please copy the full text of the error messages and paste it here.

Reply With Quote
  #4  
Old October 1st, 2012, 08:19 PM
kinison kinison is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 7 kinison User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 57 m 59 sec
Reputation Power: 0
Quote:
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! *****|");
            }
    } 
      
               
    
}

Reply With Quote
  #5  
Old October 1st, 2012, 08:22 PM
NormR's Avatar
NormR NormR is online now
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 2,959 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 3 h 17 m 24 sec
Reputation Power: 345
Does it work now?

Can you mark it as solved?

Reply With Quote
  #6  
Old October 1st, 2012, 08:40 PM
kinison kinison is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 7 kinison User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 57 m 59 sec
Reputation Power: 0
Appears so.

Reply With Quote
Closed Thread

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > Homework - Calling method into main

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap