Java - 循环无法跟踪正确的计数

时间:2016-02-02 07:07:07

标签: java

import java.util.Scanner;
import java.util.Random;



public class BlackJack {
Scanner input = new Scanner(System.in); //Instance of the Scanner 
public static void main(String[] args) {
    playGame();


}

public static void playGame() {
    //int newTotal;
    int total;
    int firstCard;
    int secondCard;
    String readLine;

    Scanner input = new Scanner(System.in); //Instance of the Scanner 
    Random random = new Random(); // Instance of Random
    firstCard = 1 + random.nextInt(10);
    secondCard = 1 + random.nextInt(10);

    System.out.println(firstCard + " , "+ secondCard + " and + 1");
    total = firstCard + secondCard + 1;
    System.out.println("Your total is: " + total);

    System.out.println("Want another card? (y/n)");
    readLine = input.nextLine();
    //           

    int newTotal;

    while (readLine.equals("y")) {
        int finalCard = random.nextInt(10);
        System.out.println(finalCard);
        newTotal = finalCard + total;
        System.out.println("You new total is: " + newTotal );

        if (newTotal == 21) {
            System.out.println("BlackJack!! You won!");
            break;
        } else if (newTotal > 21) {
            System.out.println("You lose.." );
            break;  
        } else 
            System.out.println("Want another card? (y/n)");
        readLine = input.nextLine();
        newTotal = finalCard + total;

    } 
}

}

目前,我正在尝试更新正确数量的newCard总数;然而,在第二次计数之后,它又回到旧值。

例如,

**

  • 控制台输出

**

4 , 4 and + 1
Your total is: 9
Want another card? (y/n)
y
8
You new total is: 17
Want another card? (y/n)
y
5
You new total is: 14 // this should be 22 
Want another card? (y/n) 

第3个输出数应为17 + 5;但是,我的节目目前正在增加9 + 5.请帮助..

3 个答案:

答案 0 :(得分:0)

newTotal = finalCard + total; 

使用旧的总数(在第一轮中给出)将其更改为仅使用总数而不应该有效。

如前所述,请删除else语句中的newTotal = finalCard + total;

答案 1 :(得分:0)

   public static void playGame() {
    int total;
    int firstCard;
    int secondCard;
    String readLine;

    Scanner input = new Scanner(System.in); //Instance of the Scanner 
    Random random = new Random(); // Instance of Random
    firstCard = 1 + random.nextInt(10);
    secondCard = 1 + random.nextInt(10);

    System.out.println(firstCard + " , "+ secondCard + " and + 1");
    total = firstCard + secondCard + 1;
    System.out.println("Your total is: " + total);

    System.out.println("Want another card? (y/n)");
    readLine = input.nextLine();


    while (readLine.equals("y")) {
        int finalCard = random.nextInt(10);
        System.out.println(finalCard);
        total += finalCard ;
        System.out.println("You new total is: " + total );

        if (total == 21) {
            System.out.println("BlackJack!! You won!");
            break;
        } else if (total > 21) {
            System.out.println("You lose.." );
            break;  
        } else {
            System.out.println("Want another card? (y/n)");
            readLine = input.nextLine();
        }


    } 
}

你的新游戏循环;)

答案 2 :(得分:0)

你的while循环的最后一行应该是 total = finalCard + total;

而不是 newTotal = finalCard + total;