Passing a variable between classes
Hi, I have two files one called MyProgram.java and one called GuessingGame.java(which I got off a java site). My aim is to create 5 rounds of different mini games. Ok, so what I want to do is keep track of the result of each game, then at the end do some calculations. What I want to do is pass the 'numberOfTries' variable from the GuessingGame class into the game2 method in the MyProgram class.
Here is my code, first one is my own, second one is copied.
Code:
import java.util.Scanner;
import java.util.Random;
public class MyProgram {
public static void main(String[] args) {
boolean decide;
String input;
int tracker = 0;
String[] questions = new String[11];
questions[1] = "What is the largest continent?";
questions[2] = "Which animal has black and white stripes?";
questions[3] = "What is a skeleton made of?";
questions[4] = "Where do bats live?";
questions[5] = "Where does a king and queen live?";
questions[6] = "What colour does a rainbow start with?";
questions[7] = "What sport does Wayne Rooney play?";
questions[8] = "What animal goes 'oink oink'?";
questions[9] = "Which planet is the nearest to Earth?";
questions[10] = "What colour is a banana?";
String[] PosAns = new String[49];
PosAns[1] = "asia";
PosAns[2] = "Asia";
PosAns[3] = "ASIA";
PosAns[4] = "zebra";
PosAns[5] = "Zebra";
PosAns[6] = "zebras";
PosAns[7] = "Zebras";
PosAns[8] = "ZEBRAS";
PosAns[9] = "ZEBRA";
PosAns[10] = "BONE";
PosAns[11] = "BONES";
PosAns[12] = "bone";
PosAns[13] = "bones";
PosAns[14] = "Bone";
PosAns[15] = "Bones";
PosAns[16] = "cave";
PosAns[17] = "caves";
PosAns[18] = "Cave";
PosAns[19] = "Caves";
PosAns[20] = "CAVES";
PosAns[21] = "CAVE";
PosAns[22] = "castle";
PosAns[23] = "castles";
PosAns[24] = "Castle";
PosAns[25] = "Castles";
PosAns[26] = "CASTLE";
PosAns[27] = "CASTLES";
PosAns[28] = "red";
PosAns[29] = "Red";
PosAns[30] = "RED";
PosAns[31] = "FOOTBALL";
PosAns[32] = "Football";
PosAns[33] = "football";
PosAns[34] = "SOCCER";
PosAns[35] = "Soccer";
PosAns[36] = "soccer";
PosAns[37] = "pig";
PosAns[38] = "PIG";
PosAns[39] = "Pig";
PosAns[40] = "PIGS";
PosAns[41] = "Pigs";
PosAns[42] = "pigs";
PosAns[43] = "moon";
PosAns[44] = "Moon";
PosAns[45] = "MOON";
PosAns[46] = "YELLOW";
PosAns[47] = "yellow";
PosAns[48] = "Yellow";
System.out.println("Welcome to my custom made, fun and testing game.");
System.out.println("Are you brainy enough to tackle the questions?!?");
System.out.println("");
System.out.println("The first game is a simple question and answer excercise.");
System.out.println("You will be asked various questions which you will be asked to answer.");
System.out.println("--------------------------------------------------------------------------------");
System.out.println("Round 1 Commencing...");
System.out.println("");
for (int dx=1;dx<=questions.length-1;dx++) {
decide = false;
System.out.println(questions[dx]);
Scanner scan = new Scanner(System.in);
input = scan.nextLine();
for (int ab=1;ab<PosAns.length-1;ab++) {
if(PosAns[ab].equals(input)) {
decide = true;
}
}
if (decide == true) {
System.out.println("Correct!");
System.out.println("");
tracker++;
}
else {
System.out.println("Wrong!");
System.out.println("");
}
}
System.out.println("");
System.out.println("You scored " + tracker + "/10");
System.out.println("");
game2(tracker);
}
public static void game2(int tracker) {
System.out.println("In the last round you scored " + tracker + "/10.");
System.out.println("This will count towards your overall score.");
System.out.println("");
System.out.println("The next round is a numeric based guessing game.");
System.out.println("The aim of the game is to complete it with the lowest number of guesses.");
GuessingGame.method();
}
}
//END OF PROGRAM++
Code:
import java.util.Random;
import java.util.Scanner;
public class GuessingGame {
public static void main(String[] args) {
method();
}
public static void method() {
Random rand = new Random();
int numberToGuess = rand.nextInt(1000);
int numberOfTries = 0;
Scanner input = new Scanner(System.in);
int guess;
boolean win = false;
while (win == false) {
System.out.println("Guess a number between 1 and 1000: ");
guess = input.nextInt();
numberOfTries++;
if (guess == numberToGuess) {
win = true;
}
else if (guess < numberToGuess) {
System.out.println("Your guess is too low!");
}
else if (guess > numberToGuess) {
System.out.println("Your guess is too high!");
}
}
System.out.println("You win!");
System.out.println("The number was " + numberToGuess + ".");
System.out.println("It took you " + numberOfTries + " tries.");
}
}
Thank you in advance for any information. :(y):