Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Hi Lo card game

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Hi Lo card game

    I'm a beginner programmer and I have to complete a simple hi lo card game (no suit necessary). I've encountered a problem. I have it all written out, no errors shown but my program just won't run. Any help is greatly appreciated. Here's what I have.

    import javax.swing.JOptionPane;
    import java.util.Random;
    import java.util.Scanner;
     
    public class HiLoCardGame {
     
    	public static final int OFFSET_NUMBER = 2;
    	public static final int CORRECT_ANSWERS_TO_WIN = 4;
    	public static final int WRONG_ANSWERS_TO_LOSE = 1;
    	public static void main(String[] args) {
     
    		int correctAnswerCount = 0;
    	    int wrongAnswerCount = 0;
    	    while (correctAnswerCount < CORRECT_ANSWERS_TO_WIN && wrongAnswerCount < WRONG_ANSWERS_TO_LOSE);
     
    	    {
    	    	Random generator = new Random();
    	    	int currentCard = generator.nextInt(13 + OFFSET_NUMBER);
    	    	String input = JOptionPane.showInputDialog(null, "The current card is " + currentCard + "\nWill the next card be higher, lower or equal?\nType score for current score.");
    	    	Scanner scanner = new Scanner (input);
    	    	int nextCard = generator.nextInt(13 + OFFSET_NUMBER);
     
    	    	if (nextCard > currentCard && scanner.hasNext("Higher"))
    	    	{
     
    	          ++correctAnswerCount; 
    	          JOptionPane.showMessageDialog(null, "You have guessed correctly,\nYour score is now "  + correctAnswerCount);            
     
    	    	}
     
    	    	if (nextCard > currentCard && scanner.hasNext("Lower"))
    	    	{
    	    		++wrongAnswerCount;
    	            JOptionPane.showMessageDialog(null, "You have guessed incorrectly,\nPlease restart the game.");
    	    	}
     
    	    	if (nextCard > currentCard && scanner.hasNext("Equal"))
    	    	{
    	    		++wrongAnswerCount;
    	    		JOptionPane.showMessageDialog(null,  "You have guessed incorrectly,\nPlease restart the game.");
    	    	}
     
    	    	if (nextCard < currentCard && scanner.hasNext("Higher"))
    	    	{
    	    		++wrongAnswerCount;
    	    		JOptionPane.showMessageDialog(null, "You have guessed incorrectly,\nPlease restart the game.");
    	    	}
     
    	    	if (nextCard < currentCard && scanner.hasNext("Lower"));
    	    	{
    	    		++correctAnswerCount;
    	    		JOptionPane.showMessageDialog(null, "You have guessed correctly,\nYour score is now " + correctAnswerCount);
    	    	}
     
    	    	if (nextCard < currentCard && scanner.hasNext("Equal"));
    	    	{
    	    		++wrongAnswerCount;
    	    		JOptionPane.showMessageDialog(null, "You have guessed incorrectly,\nPlease restart the game.");
    	    	}
     
    	    	if (nextCard == currentCard && scanner.hasNext("Higher"));
    	    	{
    	    		++wrongAnswerCount;
    	    		JOptionPane.showMessageDialog(null, "You have guesseed incorrectly,\nPlease restart the game.");
    	    	}
     
    	    	if (nextCard == currentCard && scanner.hasNext("Lower"));
    	    	{
    	    		++wrongAnswerCount;
    	    		JOptionPane.showMessageDialog(null, "You have guessed incorrectly,\nPlease restart the game.");
    	    	}
     
    	    	if  (nextCard == currentCard && scanner.hasNext("Equal"));
    	    	{
    	    		++correctAnswerCount;
    	    		JOptionPane.showMessageDialog(null, "You have guessed correctly,\nYour score is now " + correctAnswerCount);
    	    	}
     
    	    	if (scanner.hasNext("Score"));
    	    	{
    	    		JOptionPane.showMessageDialog(null, "Your score is " + correctAnswerCount + "\n You must answer " + (4-correctAnswerCount) + "more correctly to win.");
    	    	}
     
    	    	if (correctAnswerCount==4);
    	    	{
    	    		JOptionPane.showMessageDialog(null, "Congratulations, you have won!\nPlease play again sometime.");
    	    	}
     
    	    }
     
    	}
     
    }
    Last edited by jps; November 13th, 2012 at 06:52 PM. Reason: added code tags


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Hi Lo card game

    Please see the announcements page for the use of code tags when posting code.

    my program just won't run
    Add some printlns to see what is going on. For example:
    import javax.swing.JOptionPane;
    import java.util.Random;
    import java.util.Scanner;
     
    public class HiLoCardGame {
     
    	public static final int OFFSET_NUMBER = 2;
    	public static final int CORRECT_ANSWERS_TO_WIN = 4;
    	public static final int WRONG_ANSWERS_TO_LOSE = 1;
    	public static void main(String[] args) {
    		int correctAnswerCount = 0;
    	    int wrongAnswerCount = 0;
                    /*--------*/
                    System.out.println("correctAnswerCount" + correctAnswerCount);
                    System.out.println("CORRECT_ANSWERS_TO_WIN" + CORRECT_ANSWERS_TO_WIN);
                    System.out.println("wrongAnswerCount" + wrongAnswerCount);
                    System.out.println("WRONG_ANSWERS_TO_LOSE" + WRONG_ANSWERS_TO_LOSE);
                    System.out.println(correctAnswerCount < CORRECT_ANSWERS_TO_WIN && wrongAnswerCount < WRONG_ANSWERS_TO_LOSE);//true or false
    	    while (correctAnswerCount < CORRECT_ANSWERS_TO_WIN && wrongAnswerCount < WRONG_ANSWERS_TO_LOSE);
    	    {
    ...and if at this point everything is in working order, move deeper into the code using printlns to see where things go wrong.

Similar Threads

  1. Help with Card Game
    By JSeol14 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 21st, 2012, 10:46 PM
  2. A Card Game
    By Paytheprice in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 25th, 2012, 07:30 AM
  3. Card Game Problem.
    By d'fitz in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 5th, 2011, 07:30 AM
  4. Card Game help....
    By macFs89H in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 2nd, 2011, 07:55 AM
  5. Card Game Problem....Need Help
    By macFs89H in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 28th, 2011, 07:30 AM