Program with transfer value from one class to another
I am trying to create a Who Wants To Be A Millionaire Game - just like the Tv show. However, I keep getting an error message saying "returnValue cannont be resolved to a variable". I am just wondering how to solve the problem when passing the value back to the game class from computer class.
I am using eclipse to write the source code.
Part of the Game Class
Code Java:
boolean correctAnswer = true;
while (correctAnswer == true) {
String pAnswer = player.getUserInput("Enter a number");
String rAnswer = computer.checkAnswerQ1(returnValue);
if (rAnswer.equals("Incorrect")) {
correctAnswer = false;
System.out.println("THAT IS THE WRONG Answer. The answer you where looking for was:" + rAnswer);
}
Computer Class
Code Java:
public class Computer {
public Integer checkAnswerQ1(String intAnswer) {
int num = Integer.parseInt(intAnswer);
int playerAnswer = num;
int[] index = new int[4];
index[0] = 0;
index[1] = 1;
index[2] = 2;
index[3] = 3;
String[] RealAnswer = new String[4];
RealAnswer[0] = "Incorrect";
RealAnswer[1] = "Incorrect";
RealAnswer[2] = "Correct";
RealAnswer[3] = "Incorrect";
String returnValue = "Incorrect";
index[num] = playerAnswer; // assigning the playerAnswer as number value which is en look
// up by the string array to give either correct or incorrect answer
int actualAnswer = 3; // correct answer for the question
System.out.println("Host: Your Answer is:" + RealAnswer[playerAnswer]);
System.out.println("Host: Final Answer?");
System.out.println("Player: Final Answer");
System.out.println("For 500 pounds the answer is:");
if(playerAnswer == actualAnswer) {
System.out.println("A London");
System.out.println("Congraulations you have just won 500 pounds");
} else if (playerAnswer != actualAnswer) {
System.out.println("The corred answer is" + actualAnswer);
System.out.println("You just lost 500 pounds");
} // closes the else if statement
return returnValue;
} // closes the method
} // closes the class
Re: Program with transfer value from one class to another
Pretty sure you want to be using pAnswer instead of returnValue.
Use code tags.
Re: Program with transfer value from one class to another
Ok I have changed the return in the computer class to playerAnswer which got rid of one of the problem, however, for the
String rAnswer = computer.checkAnswerQ1(playerAnswer);
it is still saying: playerAnswer cannot be resolved to a variable
Re: Program with transfer value from one class to another
Re: Program with transfer value from one class to another
Hi,
I have changed the playerAnswer to pAnswer, however, it still displays the same error below is the full code if this is of any help.
Game Class
Code Java:
public class Game {
public static void main(String[] args) {
Player player = new Player();
Computer computer = new Computer();
// introduction to the game
System.out.println("Hello and welcome to another show of Who Wants To Be a Millionaire");
System.out.println("Tonight we have a new contestan, hoping to get their hands on jackpot of ONE MILLION POUNDS");
System.out.println("There is just one problem. Between him and the money are sixteen questions");
System.out.println("");
System.out.println("Ok First i'll explain the prize breakdown");
System.out.println("");
System.out.println("> The first question is worth: £500");
System.out.println("> The second question is worth: £1,000");
System.out.println("> The third question is worth: £2,000");
System.out.println("> The fouth question is worth: £3,000");
System.out.println("> The fifth question is worth: £4,000 with a gurantee of atleast going home with this sum of money");
System.out.println("> The sixth question is worth: £5,000 ");
System.out.println("> The seventh question is worth: £10,000");
System.out.println("> The eight question is worth: £15,000");
System.out.println("> The nineth question is worth: £18,000");
System.out.println("> The tenth question is worth: £23,000 with a gurantee of atleast going home with this sum of money");
System.out.println("> The eleveth question is worth: £28,000");
System.out.println("> The twevleth question is worth: £37,000");
System.out.println("> The thirteenth question is worth: £50,000");
System.out.println("> The fourteenth question is worth: £125,000");
System.out.println("> The fiftheenth question is worth: £250,000");
System.out.println("> The sixteenth question is worth: £1,000,000");
// beginning of the game
System.out.println("");
System.out.println("Ok, lets begin.");
// first question
System.out.println("");
System.out.println("Question 1");
System.out.println("");
System.out.println("Ok here the first question for 500 pounds");
System.out.println("What is the captial of England?");
System.out.println("> Is it A: Paris?");
System.out.println("> Is it B: Washington DC?");
System.out.println("> Is it C: London?"); // correct answer to question
System.out.println("> or is it D: Moscow?");
// User input
boolean correctAnswer = true;
while (correctAnswer == true) {
String pAnswer = player.getUserInput("Enter a number");
String rAnswer = computer.checkAnswerQ1(pAnswer);
if (rAnswer.equals("Incorrect")) {
correctAnswer = false;
System.out.println("THAT IS THE WRONG Answer. The answer you where looking for was:" + rAnswer);
Stop stop = new Stop();
stop.stopProgram();
}
}
}
}
Computer Class
Code Java:
public class Computer {
public Integer checkAnswerQ1(String intAnswer) {
int num = Integer.parseInt(intAnswer);
int playerAnswer = num;
int[] index = new int[4]; // integer index for numbers
index[0] = 0;
index[1] = 1;
index[2] = 2;
index[3] = 3;
String[] RealAnswer = new String[4]; // string array for finding the correct answer
RealAnswer[0] = "Incorrect";
RealAnswer[1] = "Incorrect";
RealAnswer[2] = "Correct";
RealAnswer[3] = "Incorrect";
index[num] = playerAnswer; // assigning the playerAnswer as number value which is then look
// up by the string array to give either correct or incorrect answer
int actualAnswer = 3; // correct answer for the question
System.out.println("Host: Your Answer is:" + RealAnswer[playerAnswer]); // prints the message plus the user response
System.out.println("Host: Final Answer?"); // message
System.out.println("Player: Final Answer"); // message
System.out.println("Host: Ok, for 500 pounds the answer is: "); // message
if(playerAnswer == actualAnswer) { // tests to see if the player is the same a the actual answer to the question
System.out.print("A London"); // user message
System.out.println("Host: Congraulations you have just won 500 pounds"); // user message
} else if (playerAnswer != actualAnswer) { // if the player and actual answer aren't the same it throws an error / incorrect answer
System.out.println("The correct answer is" + RealAnswer[actualAnswer]); // user message
System.out.println("You just lost 500 pounds"); // user message
} // closes the else if statement
return pAnswer; // returns the value of playerAnswer
} // closes the method
} // closes the class
Re: Program with transfer value from one class to another
You still have to use code tags.
Post the full stack trace, and point out any line numbers referenced in the stack trace so we don't have to count manually.
Also, your code should be in the form of an SSCCE (that's a link). So take out all of the extra stuff, throw everything into one class, and make sure it demonstrates the problem.
Re: Program with transfer value from one class to another
The program takes a users reponse to the question, which can be 1 to 4. After the number is tested via looking in the computer class to see if the playerAnswer and actucalAnswer match. If they do the program moves onto the next question, if they don't, the program displays a message and closes the program.
If you like i can attach the src file so you can run the program.
Where the errors are appearing are shown in red.
P.s. I am not sure i have used the code tags properly.
[code]
boolean correctAnswer = true;
while (correctAnswer == true) {
String pAnswer = player.getUserInput("Enter a number");
String rAnswer = computer.checkAnswerQ1( playerAnswer);
if (rAnswer.equals("Incorrect")) {
correctAnswer = false;
System.out.println("THAT IS THE WRONG Answer. The answer you where looking for was:" + rAnswer);
Stop stop = new Stop();
stop.stopProgram();
[code]
public class Computer {
public Integer checkAnswerQ1(String intAnswer) {
int num = Integer.parseInt(intAnswer);
int playerAnswer = num;
int[] index = new int[4]; // integer index for numbers
index[0] = 0;
index[1] = 1;
index[2] = 2;
index[3] = 3;
String[] RealAnswer = new String[4]; // string array for finding the correct answer
RealAnswer[0] = "Incorrect";
RealAnswer[1] = "Incorrect";
RealAnswer[2] = "Correct";
RealAnswer[3] = "Incorrect";
index[num] = playerAnswer; // assigning the playerAnswer as number value which is then look
// up by the string array to give either correct or incorrect answer
int actualAnswer = 3; // correct answer for the question
System.out.println("Host: Your Answer is:" + RealAnswer[playerAnswer]); // prints the message plus the user response
System.out.println("Host: Final Answer?"); // message
System.out.println("Player: Final Answer"); // message
System.out.println("Host: Ok, for 500 pounds the answer is: "); // message
if(playerAnswer == actualAnswer) { // tests to see if the player is the same a the actual answer to the question
System.out.print("A London"); // user message
System.out.println("Host: Congraulations you have just won 500 pounds"); // user message
} else if (playerAnswer != actualAnswer) { // if the player and actual answer aren't the same it throws an error / incorrect answer
System.out.println("The correct answer is" + RealAnswer[actualAnswer]); // user message
System.out.println("You just lost 500 pounds"); // user message
} // closes the else if statement
return playerAnswer; // returns the value of playerAnswer
} // closes the method
} // closes the class
[code]
Re: Program with transfer value from one class to another
You really need to know how to use code tags! :mad:
Then, you need to pass the pAnswer to your checkAnswerQ1 method. Because pAnswer is what your user has selected as an answer.
Code java:
String rAnswer = computer.checkAnswerQ1(pAnswer);
Your actual checkAnswerQ1 method is returning an Integer, and you are trying to store it in String, why and how?
You are comparing the outcome of checkAnswerQ1 with String, so it should return a String like this,
Now in this method, have a String variable defaulted to "Incorrect". When you evaluate your pAnswer, if it matches with the actual answer, make that String "Correct" or else leave it as it is. Finally return that String.
Hope that helps,
Goldest