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 3 of 3

Thread: Need help with Pick A Card Program Please

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

    Default Need help with Pick A Card Program Please

    Here is the program I have so far for the Pick A Card assignment. It almost works but gives the wrong answer? Is something backwards in my code?

     
    import java.util.*;
     
    public class assign_7
    {
        static Scanner js = new Scanner(System.in);
     
        public static void main (String[] args)
        {
            int num1, num2;
            int guess;
    		char choice;
    		String aString;
     
            num1 = (int) (Math.random() * 100);
            num2 = (int) (Math.random() * 100);
     
     
            System.out.println("Enter 1 to see card 1 OR enter 2 to see card 2:");
    		guess = js.nextInt();
     
     
    // Player picks card 1
    		if (guess == 1) {
    			guess = num1;
    			System.out.println("You picked card 1, which has the value of " + num1);
    			System.out.println("Is this the highest card? (y/n)");
     
    			aString = js.next();
    			choice = aString.charAt(0);
     
    			System.out.println();
     
    		if ((aString.equals ("y")) && (num1 < num2))
    			System.out.println("Correct. The other card is " + num2);
     
    		if ((aString.equals ("y")) && (num1 > num2))
    			System.out.println("Incorrect. The other card is " + num2);
     
    		if ((aString.equals ("n")) && (num1 < num2))
    			System.out.println("Correct. The other card is " + num2);
     
    		else if ((aString.equals ("n")) && (num1 > num2))
    			System.out.println("Incorrect. The other card is " + num2);
     
    	}
     
    // Player picks card 2
    		if (guess == 2) {
    			guess = num2;
    			System.out.println("You picked card 2, which has the value of " + num2);
    			System.out.println("Is this the highest card? (y/n)");
     
    			aString = js.next();
    			choice = aString.charAt(0);
     
    			System.out.println();
     
    		if ((aString.equals ("y")) && (num1 < num2))
    			System.out.println("Incorrect. The other card is " + num1);
     
    		if ((aString.equals ("y")) && (num1 > num2))
    			System.out.println("Correct. The other card is " + num1);
     
    		if ((aString.equals ("n")) && (num1 < num2))
    			System.out.println("Incorrect. The other card is " + num1);
     
    		else if ((aString.equals ("n")) && (num1 > num2))
    			System.out.println("Correct. The other card is " + num1);
    		}
     
     
        }
    }


  2. #2
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Need help with Pick A Card Program Please

    Probably .
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  3. #3
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: Need help with Pick A Card Program Please

    Hello gcjava!
    Quote Originally Posted by gcjava View Post
    It almost works but gives the wrong answer? Is something backwards in my code?
    You have some logic errors. You need to check again some of your if statements. I think it would help if you printed both cards (for debbuging), tested your code with every possible input and then go back to your code (the if statements) to see what is the bug.

Similar Threads

  1. [SOLVED] Card Program"War" Class help
    By Usoda in forum What's Wrong With My Code?
    Replies: 7
    Last Post: February 16th, 2012, 08:43 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. Credit card help
    By kostas198 in forum Java Theory & Questions
    Replies: 1
    Last Post: November 6th, 2011, 10:50 PM
  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. Pick random objects from array/arraylist. Newbie.
    By Sputnik in forum Collections and Generics
    Replies: 3
    Last Post: October 29th, 2010, 11:59 AM