简体   繁体   English

如何为该二十一点游戏增加可重玩性?

[英]How can i add replayability to this blackjack game?

    import java.util.*;
import java.util.Scanner;

public class Assignment2 {
    public static void main(String args[]){ 
        Scanner stdin = new Scanner(System.in);

        Random random = new Random();     
        int ran2 = (random.nextInt(10));
        int ran1 = (random.nextInt(10));
        int total = ran1 + ran2;
        char exit = 'y';

        System.out.println("First cards: " + ran1 + ", " + ran2);
        System.out.println("Total: " + total);

        while(exit != 'n' && total < 21){
            System.out.println("Do you want another card? (y/n): ");
            exit = stdin.next().charAt(0);

            if (exit =='n'){
                System.out.println("Would you like to play again? (y/n): ");
                exit = stdin.next().charAt(0);                       
                if(total > 21 || exit == 'n'){          
                    break;
                }
            }

            int next = random.nextInt(10);
            System.out.println("Card: "+ next);
            total = total + next;//adds 
            System.out.println("Total: "+ total);

            if(total > 21){
                System.out.println("Bust.");
                break;
            }

            if(total == 21){
                System.out.println("You win!");
                break;
            }

        }   


    }
}

i got the game working and all, but if i wanted to play from the beginning to where you get your first cards, how would i do it? 我可以正常运行游戏,但是如果我想从一开始就玩到第一张牌,我该怎么办? after you win or lose how would i make it restart the game? 在您输赢之后,我将如何重新启动游戏? i've been trying to figure this out and still cant find out how do it 我一直在试图找出答案,但仍然找不到答案

Wrap your code in another loop :) 将代码包装在另一个循环中:)

Scanner stdin = new Scanner(System.in);
char exit = 'n';

while( exit != 'n' ) {
  // your old code here
  System.out.println("Play again ? (y/n):")
  exit = stdin.next().charAt(0);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM