The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Java Help
|
Homework - Calling method into main
Discuss Calling method into main in the Java Help forum on Dev Shed. Calling method into main Java Help forum discussing all Java platforms - J2ME, J2SE and J2EE - as well as relevant standards, APIs and frameworks such as Swing, Servlets, JSPs, Applets, Struts, Spring, Hibernate, ANT, EJB, and other Java-related topics.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

October 1st, 2012, 01:35 PM
|
|
Registered User
|
|
Join Date: Oct 2012
Posts: 7
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
}
}
}
|

October 1st, 2012, 02:41 PM
|
|
Registered User
|
|
Join Date: Oct 2012
Posts: 7
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");
}
}
}
|

October 1st, 2012, 07:32 PM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: SW Missouri
|
|
Quote: | I just cant seem to get it to compile |
Please copy the full text of the error messages and paste it here.
|

October 1st, 2012, 08:19 PM
|
|
Registered User
|
|
Join Date: Oct 2012
Posts: 7
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! *****|");
}
}
}
|

October 1st, 2012, 08:22 PM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: SW Missouri
|
|
|
Does it work now?
Can you mark it as solved?
|

October 1st, 2012, 08:40 PM
|
|
Registered User
|
|
Join Date: Oct 2012
Posts: 7
Time spent in forums: 57 m 59 sec
Reputation Power: 0
|
|
|
Appears so.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|