交替播放器并添加用户输入
alternating players and adding user inputs
我遇到了这个 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++;
}
有人对此有解决方案吗?
您需要将玩家的筹码增加一个值而不是分配它。
int amount = input.nextInt();
one += amount;
chips -= amount;
这应该有效:
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);
}
}
我遇到了这个 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++;
}
有人对此有解决方案吗?
您需要将玩家的筹码增加一个值而不是分配它。
int amount = input.nextInt();
one += amount;
chips -= amount;
这应该有效:
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);
}
}