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

Thread: The program working fine but the "NO" button doesn't work

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default The program working fine but the "NO" button doesn't work

    Hi Expert,

    Anyone can help me to fix this problem. The program working fine but the "NO" button doesn't work.

    When I input the answer "NO", the program cannot exit unless I answer to system "Yes" until the point get "You bust".

    Thanks in advance to all expert. Thanks

    	class Card {
    		private String point, type;
    		private int facevalue;
     
    		public String getPoint(){
    			return point;
    			}
    		public String getType(){
    			return type;
    			}
    		public int getFacevalue(){
    			return facevalue;
    			}
    		public Card(String inPoint, String inType, int inFacevalue){
    			point=inPoint; type=inType; facevalue=inFacevalue;
    			}
    		public String toString(){
    			return point +" of " + type;
    			}
    		};
     
    		class Deck {
    			private Card[] deckcards;
    			private int topdeck;
     
    		public void genCard(){
    			for(int i=0; i < 52; i++) {
    				Random gen = new Random();
    				Card temp;
    				int number = gen.nextInt(52);
    				temp = deckcards[i];
    				deckcards[i] = deckcards[number];
    				deckcards[number] = temp;
    			}
    		}
     
    		public Deck(){
    			deckcards = new Card[52];
    			String[] types = {"Spades", "Hearts", "Clubs", "Diamonds"};
    			String[] points = {"Ace", "King", "Queen", "Jack", "Ten", "Nine", "Eight", "Seven", "Six", "Five", "Four", "Three", "Two"};
    			int[] facevalues = {11, 10, 10, 10, 10, 9, 8, 7, 6, 5, 4, 3, 2};
     
    			for(int i = 0; i < 52; i++){
    				deckcards[i] = new Card(points[i%13], types[i%4], facevalues[i%13]);
    			//	System.out.println(points[i%13]+" of "+types[i%4]+", Facevalue: "+facevalues[i%13]);
    			}
    				topdeck = 0;
    		}
     
    		public Card getCard(){
    			return deckcards[topdeck++];
    		}
    		};
     
    		class NewPlayer {
    			private String name;
    			private ArrayList<Card> hand;
    			private int aces=0, total=0, cards=0;
     
    			public String getName()	{
    				return name;
    			}
     
    			public int getTotal(){
    				return total;
    			}
     
    			public void setName(String name){
    				this.name=name;
    			}
     
    			public void hit(Card a){
    				hand.add(a);
    				cards++;
    				if(a.getPoint().equals("Ace"))
    				aces++;
    					if(aces >= 2)
    					total = total + 1;
    					else
    					total = total + a.getFacevalue();}
     
    			public void getHand(){
    				for(int i = 0; i < hand.size(); i++){
    						Card b = hand.get(i);
    						System.out.println(b);
    					}
    			}
     
    			public NewPlayer(){
    				hand = new ArrayList<Card>();
    				}
     
    		};
     
    public class Blackjack {
     
        public static void main(String[] args){
     
    		{
    			Scanner inputans = new Scanner(System.in);
    			String answer;
    			Deck myDeck = new Deck();
    			myDeck.genCard();
     
    			NewPlayer me = new NewPlayer();
    			System.out.print("Please enter your name: ");
    				me.setName(inputans.nextLine());
    				NewPlayer dealer = new NewPlayer();
    				dealer.setName("Dealer");
    				System.out.println("Hi......  Welcome   " + me.getName() + "\n");
     
    				Card a = myDeck.getCard();
    				me.hit(a);
    				a = myDeck.getCard();
     
    				dealer.hit(a);
    				System.out.println("The Dealer is showing :");
    				System.out.println(a + "\n");
     
    				a = myDeck.getCard();
    				me.hit(a);
    				System.out.println("Your hand is: ");
    				me.getHand();
    				System.out.println("Your total is: " + me.getTotal() + "\n");
     
    				a = myDeck.getCard();
    				dealer.hit(a);
     
    				if(me.getTotal()==(21)){
    					System.out.println("Dealer's hand is: ");
    					dealer.getHand();
    						System.out.println("Dealer's total is: " + dealer.getTotal());
    							if(me.getTotal() > dealer.getTotal())
    								System.out.println("Congratulations " + me.getName() + "! You win!!!");
    							else
    							System.out.println("It's a draw! Play again!");}
     
    						while(me.getTotal() < 21){
    							System.out.print("Do you want another card? Please enter YES or NO: ");
    							answer = inputans.nextLine();
     
    								if(answer.equalsIgnoreCase("No")){
    									System.out.println("Dealer's hand is: ");
    									dealer.getHand();
    									System.out.println("Dealer's total is: " + dealer.getTotal());
     
    										if(me.getTotal() > dealer.getTotal())
    										System.out.println("Congratulations " + me.getName()+"! You win!!!");
     
    										else if(me.getTotal() < dealer.getTotal())
    											System.out.println("Sorry " + me.getName() + "... You lose. Try again!");
     
    											else
    												System.out.println("It's a draw! Play again!");}
     
    												else if(answer.equalsIgnoreCase("Yes")){
    													a = myDeck.getCard();
    													me.hit(a);
    													System.out.println("Your hand is: ");
    													me.getHand();
    													System.out.println("Your total is: " + me.getTotal());
    													System.out.println();
     
    													{
    														if(me.getTotal() > 21)
    														System.out.println("Sorry " + me.getName()+"... You bust. Try again!");
    															else if(me.getTotal()==(21))
    															{
    																if(me.getTotal()>dealer.getTotal())
    																	{
    																		System.out.println("Dealer's hand is: ");
    																		dealer.getHand();
    																			System.out.println("Dealer's total is: "+dealer.getTotal());
    																			System.out.println("Congratulations "+me.getName()+"! You win!!");}
    																			else
    																			System.out.println("It's a draw! Play again!");}}}}
    																			System.out.println();
    		}
    	}
    }
    Last edited by JavaPF; October 5th, 2011 at 07:28 AM. Reason: Added highlight code


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: The program working fine but the "NO" button doesn't work

    Please edit your post and wrap the code in code tags.
    See: BB Code List - Java Programming Forums

    Unformated code is hard to read.

    Where is the code for the "No" button? The posted code does not have a GUI.

  3. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    8
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: The program working fine but the "NO" button doesn't work

    You have to terminate the while loop when NO is choosen. That can be done either with a break or an extra condition in the while statement.

  4. The Following User Says Thank You to qurtan For This Useful Post:

    ahcenpg (October 1st, 2011)

  5. #4
    Junior Member
    Join Date
    Oct 2011
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: The program working fine but the "NO" button doesn't work

    Hi qurtan,
    Thanks for your help.
    Actually, I'm not sure which line to insert the "break" command.

Similar Threads

  1. Replies: 3
    Last Post: October 4th, 2011, 09:03 PM
  2. using if statement on string System.getProperty("os.name") not working
    By sauyon in forum What's Wrong With My Code?
    Replies: 3
    Last Post: June 29th, 2011, 12:09 PM
  3. [SOLVED] "possible loss of precision", except not, code doesn't work, simple question
    By Perd1t1on in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 24th, 2010, 07:11 PM
  4. password field, with focused "Enter" button
    By chronoz13 in forum Java Swing Tutorials
    Replies: 1
    Last Post: June 13th, 2010, 05:00 PM
  5. password field, with focused "Enter" button
    By chronoz13 in forum Java Code Snippets and Tutorials
    Replies: 1
    Last Post: June 13th, 2010, 05:00 PM