r/programmingbydoing • u/RCDebug93 • Dec 03 '13
#47 Three-Card-Monte
Does this code look ok to you? For some reason i think that there would be an easier way that all the if statements?
import java.util.Scanner; import java.util.Random;
public class ThreeCardMonte { public static void main (String [] args) { Scanner keyboard = new Scanner (System.in); Random r = new Random();
int x = 1 + r.nextInt(3);
int guess;
System.out.println("You go to the cassino and you see a guy with three cards on the table.");
System.out.println("You aprouch him and ask him about the game. He Tells you to pick one of the three cards");
System.out.println("if you get an Ace you win if not you dont." + "\n");
System.out.println("\t" + "##" + "\t" + "##" + "\t" + "##");
System.out.println("\t" + "##" + "\t" + "##" + "\t" + "##" +"\n");
System.out.println("\t" + " 1" + "\t" + " 2" + "\t" + " 3" );
guess= keyboard.nextInt();
if ((guess == x)&&(x==1))
{
System.out.println("\t" + "AA" + "\t" + "##" + "\t" + "##");
System.out.println("\t" + "AA" + "\t" + "##" + "\t" + "##" +"\n");
System.out.println("\t" + " 1" + "\t" + " 2" + "\t" + " 3" + "\n");
System.out.println("You naied it the dealer hands over the money ");
}
else if ((guess == x)&&(x==2))
{
System.out.println("\t" + "##" + "\t" + "AA" + "\t" + "##");
System.out.println("\t" + "##" + "\t" + "AA" + "\t" + "##" +"\n");
System.out.println("\t" + " 1" + "\t" + " 2" + "\t" + " 3" + "\n");
System.out.println("You naied it the dealer hands over the money");
}
else if ((guess == x)&&(x==3))
{
System.out.println("\t" + "##" + "\t" + "##" + "\t" + "AA");
System.out.println("\t" + "##" + "\t" + "##" + "\t" + "AA" +"\n");
System.out.println("\t" + " 1" + "\t" + " 2" + "\t" + " 3" + "\n");
System.out.println("You naied it the dealer hands over the money");
}
//For when the guess is greater than the random number
else if ((x < guess)&&(x==1))
{
System.out.println("\t" + "AA" + "\t" + "##" + "\t" + "##");
System.out.println("\t" + "AA" + "\t" + "##" + "\t" + "##" +"\n");
System.out.println("\t" + " 1" + "\t" + " 2" + "\t" + " 3" + "\n");
System.out.println("You lost sorry your guess was to high give the dealer your money. It was number " +x);
}
else if ((x< guess)&&(x==2))
{
System.out.println("\t" + "##" + "\t" + "AA" + "\t" + "##");
System.out.println("\t" + "##" + "\t" + "AA" + "\t" + "##" +"\n");
System.out.println("\t" + " 1" + "\t" + " 2" + "\t" + " 3" + "\n");
System.out.println("You lost sorry your guess was to high give the dealer your money. It was number " +x);
}
// The if statments for when the guess is less than the random number
else if ((x > guess)&&(x==2))
{
System.out.println("\t" + "##" + "\t" + "AA" + "\t" + "##");
System.out.println("\t" + "##" + "\t" + "AA" + "\t" + "##" +"\n");
System.out.println("\t" + " 1" + "\t" + " 2" + "\t" + " 3" + "\n");
System.out.println("You lost sorry your guess was to low give the dealer your money. It was number " +x);
}
else if ((x > guess)&&(x==3))
{
System.out.println("\t" + "##" + "\t" + "##" + "\t" + "AA");
System.out.println("\t" + "##" + "\t" + "##" + "\t" + "AA" +"\n");
System.out.println("\t" + " 1" + "\t" + " 2" + "\t" + " 3" + "\n");
System.out.println("You lost sorry your guess was to low give the dealer your money. It was number " +x);
}
}//end of main
}//end of class
3
Upvotes
View all comments
Show parent comment
1
u/RCDebug93 Dec 06 '13
Sorry for my late reply i was trying to wrap my head around what you said so as not to annoy you!! However i have failed.. In your example programmingbydoing.com if the user gets there guess wrong you tell them that they ow the dealer the money but you also show them what the right answer actually was if the form
System.out.println("You lost sorry give the dealer your money. It was number " +x);
## AA
## AA
1 2 3
is that possible using two if statements?
Was that part of the assignment? Sorry for irritating you :)