2

我遇到了这个 java 问题,我想看看为它编写代码是多么容易。

编写一个 Java 程序,让两个人玩以下游戏。我们从一堆筹码开始。您的游戏将允许奇数。第一个玩家从筹码堆中取出 1 到(筹码数 - 1)/2。从那时起,玩家交替轮流,从堆中取出筹码,直到它为空。一旦堆空了,游戏就结束了。

一切都很顺利,直到我不得不交替转弯。我做对了,但是当玩家 1 输入一个值,而玩家 2 也这样做时,当 while 循环再次进行时,它从 0 开始并且不添加过去的输入。

这就是我的意思:

while(chips!=0){
    if(counter%2==0){
        System.out.println(fname + " has " + (one) + " chips"  + "\n" + sname + " has " + (two) + " chips." + "\n" + "It is your turn, " + fname + "\n" + "There are " + chips + " remaining." + "\n" + "You may take any number of chips from 1 to " + (chips-1)/2 + ". How many will you take, " + fname + "?") ;
        one +=input.nextInt();
        chips -=one;
    } else {
        System.out.println(fname + " has " + (one) + " chips"  + "\n" + sname + " has " + (two) + " chips." + "\n" + "It is your turn, " + sname + "\n" + "There are " + chips + " remaining." + "\n" + "You may take any number of chips from 1 to " + (chips-1)/2 + ". How many will you take, " + sname + "?") ;
        two +=input.nextInt();
        chips -=two;
    }
    counter++;
}

有没有人有解决方案?

4

2 回答 2

1

这应该有效:

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

public class Test {

public static Scanner input;
public static void main(String[] args) {

    System.out.println("Enter number of chips to start the game with:");
    input = new Scanner(System.in);
    int chips = input.nextInt();
    turn_one(chips, 0 ,0);              
}


 public static void turn_one(int chips, int playerone, int playertwo){
        int max;
        if(chips <= 2){
            max = 1;               
        }
        else{
            max = (chips-1)/2;
        }
        if(chips <= 0){
            System.out.println("Game over!");
            return;
        }

        System.out.println("Playerone has " + playerone + " chips"  + "\n" + "playertwo" + " has " + playertwo + " chips." + "\n" + "It is your turn, " + "playerone" + "\n" + "There are " + chips + " remaining." + "\n" + "You may take any number of chips from 1 to " + max + ". How many will you take, playerone?") ;

        int taken = input.nextInt();
        if (taken > max){
            System.out.println("You can't take that many chips!");
            turn_one(chips, playerone, playertwo);
        }
        playerone = playerone + taken;
        chips = chips - taken;
        turn_two(chips, playerone, playertwo);
    }


    public static void turn_two(int chips, int playerone, int playertwo){
        int max;
        if(chips <= 2){
            max = 1;               
        }
        else{
            max = (chips-1)/2;
        }
        if(chips <= 0){
            System.out.println("Game over!");
            return;
        }
        System.out.println("Playerone has " + playerone + " chips"  + "\n" + "playertwo" + " has " + playertwo + " chips." + "\n" + "It is your turn, " + "playertwo" + "\n" + "There are " + chips + " remaining." + "\n" + "You may take any number of chips from 1 to " + max + ". How many will you take, playertwo?") ;

        int taken = input.nextInt();
        if (taken > max){
            System.out.println("You can't take that many chips!");
            turn_one(chips, playerone, playertwo);
        }
        playertwo = playertwo + taken;
        chips = chips - taken;
        turn_one(chips, playerone, playertwo);
    }

}

于 2015-10-02T17:24:38.957 回答
1

您需要将玩家的筹码增加一个值,而不是分配它。

int amount = input.nextInt();
one += amount;
chips -= amount;
于 2015-10-02T16:15:10.610 回答