Results 1 to 4 of 4
Thread: [Help] Basic Java GuessingGame
- 10-08-2011, 09:46 PM #1
Member
- Join Date
- Oct 2011
- Posts
- 3
- Rep Power
- 0
[Help] Basic Java GuessingGame (sorting arrays)
Hello, I'm trying to sort the highscore at the end, by making players with the less number of tries come first and so on.. I've been trying different things for hours now.. but it didn't seem to work. Please Help me!
Java Code:/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package GuessGame; /** * * @author ToMz */ import java.util.Scanner; import java.util.ArrayList; public class GuessGame { static ArrayList<Integer> score = new ArrayList<Integer>(); static ArrayList<String> playern = new ArrayList<String>(); public static void main(String[] args) { // create a random number int number = (int)(Math.random() * 10 + 1); Scanner input = new Scanner(System.in); System.out.println("Try guessing a number between 1 and 10"); int guess = -1; int tries = 0; // where the loop starts while (guess != number) { tries++; // the user's number input System.out.println("Enter your guess: "); // only allow digits while (!input.hasNextInt()) { System.out.println("You have entered invaid value! Try again."); input.next(); } guess = input.nextInt(); // conditions if (guess < 1 || guess > 10) { System.out.println(guess + " is not what the system's expected, try again."); } else if (guess == number) { System.out.println(number + " is correct!"); } else if (guess > number) { System.out.println(guess + " is too high. Try again!"); } else if (guess < number) { System.out.println(guess + " is too low. Better luck next time!"); } // where the loop ends } System.out.println("You have guessed " + tries + " time(s)"); // asking for the user's name System.out.println("Please enter your name: "); String name = input.next(); // creating high score score.add(tries) ; playern.add(name); //show the scores System.out.println("History"); System.out.println("Try(s)" + "\t\t" + "Player(s)"); for (int j = 0; j < score.size(); j++){ System.out.println(score.get(j) + "\t\t" + playern.get(j)); } // continue or out System.out.println("Do you want to try again? (y/n)"); String answer = input.next(); // conditions for y and n if ("y".equals(answer)) { main(null); } else if ("n".equals(answer)) { System.out.println("Session Terminated"); System.exit(0); } else { System.out.println("GET THE HELL OUT, TROLL"); } // where the conditions end } }Last edited by replaywork; 10-09-2011 at 01:14 AM. Reason: Another problem occured
-
Re: [Help] Basic Java GuessingGame
Your problem is that you're looping through your program again by calling the main method recursively, that is, you're calling the main method from within the main method. When you do this, all the variables declared inside of main get re-declared and everything starts anew. Solution: don't do this. Instead repeat the game by putting it into a do-while loop. Use a boolean variable to decide whether or not to keep on playing (in fact you can call it keepOnPlaying) and set it to false if the user types in "n" or "N".
- 10-09-2011, 01:18 AM #3
Member
- Join Date
- Oct 2011
- Posts
- 3
- Rep Power
- 0
Re: [Help] Basic Java GuessingGame
Thank you! I found another different solution but then another comes up.. I am now trying to sort the highscore at the end, by making players with the less number of tries come first and so on. But it doesn't seem to be working at all.. please help me!
Java Code:import java.util.Scanner; import java.util.ArrayList; public class GuessGame { static ArrayList<Integer> score = new ArrayList<Integer>(); static ArrayList<String> playern = new ArrayList<String>(); public static void main(String[] args) { // create a random number int number = (int)(Math.random() * 10 + 1); Scanner input = new Scanner(System.in); System.out.println("Try guessing a number between 1 and 10"); int guess = -1; int tries = 0; // where the loop starts while (guess != number) { tries++; // the user's number input System.out.println("Enter your guess: "); // only allow digits while (!input.hasNextInt()) { System.out.println("You have entered invaid value! Try again."); input.next(); } guess = input.nextInt(); // conditions if (guess < 1 || guess > 10) { System.out.println(guess + " is not what the system's expected, try again."); } else if (guess == number) { System.out.println(number + " is correct!"); } else if (guess > number) { System.out.println(guess + " is too high. Try again!"); } else if (guess < number) { System.out.println(guess + " is too low. Better luck next time!"); } // where the loop ends } System.out.println("You have guessed " + tries + " time(s)"); // asking for the user's name System.out.println("Please enter your name: "); String name = input.next(); // creating high score score.add(tries) ; playern.add(name); //show the scores System.out.println("History"); System.out.println("Try(s)" + "\t\t" + "Player(s)"); for (int j = 0; j < score.size(); j++){ System.out.println(score.get(j) + "\t\t" + playern.get(j)); } // continue or out System.out.println("Do you want to try again? (y/n)"); String answer = input.next(); // conditions for y and n if ("y".equals(answer)) { main(null); } else if ("n".equals(answer)) { System.out.println("Session Terminated"); System.exit(0); } else { System.out.println("GET THE HELL OUT, TROLL"); } // where the conditions end } }
-
Re: [Help] Basic Java GuessingGame
Your "different" solution is faulty. Again, do not use recursion, and do not try to use static variables as a kludge to perpetuate this mistake.
Similar Threads
-
Basic Java help, AIM?
By jkswebsite in forum New To JavaReplies: 4Last Post: 07-11-2012, 06:17 PM -
Basic java help =)
By Xycose in forum New To JavaReplies: 1Last Post: 06-30-2010, 02:20 AM -
Basic java
By santa in forum New To JavaReplies: 5Last Post: 11-16-2009, 10:16 AM -
basic java
By vijay24805 in forum New To JavaReplies: 25Last Post: 04-14-2009, 02:46 AM -
basic java help
By adred in forum New To JavaReplies: 0Last Post: 03-08-2008, 12:36 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks