gpt4 book ai didi

java - Java 中的 "Stick Game"程序无法正常运行?

转载 作者:行者123 更新时间:2023-11-29 04:56:59 25 4
gpt4 key购买 nike

我最近决定要编写一个程序来玩名为“Nim”的游戏,在该游戏中,您从预定数量的“木棍”开始,每个玩家轮流移除 1 到 3 个木棍坚持。谁拿走最后一根棍子谁就输了。

无论如何,我已经编写了我的程序,它几乎可以完美地编译和运行。只有一个小问题。游戏结束后,它会显示两次“good game”画面,游戏的第一行出现在中间(我会在最后张贴截图)。这很奇怪,我只是想知道你们是否可以看一看。

我正在截取程序的一部分(只有一个类,名为 Cup()),因为它有点长,所以如果您看到一个您不认识的类,请忽略它。类在程序中的作用很容易解释,而不是错误发生的地方。这是代码。

class SticksGame
{
public static void main(String[] args) throws InputMismatchException
{
Random r = new Random();
int score1 = 0, score2 = 0;
Cup c = new Cup();
int j = 0, d = 0, i = 0, k = 0;
boolean b = true;
String exit = "default";
Scanner input = new Scanner(System.in);
System.out.println("Welcome to the Sticks Game! Last Stick loses! Must pick 1 - 3 sticks.");
System.out.println();
do
{
i = r.nextInt(15) + 9;
System.out.println("We begin with " + i + " sticks");
System.out.println();
while (b == true)
{
System.out.println("Your move");
k = input.nextInt();
if (k > 3)
{
System.out.println("You must select between 1 and 3 sticks");
k = input.nextInt();
}
else if (k < 1)
{
System.out.println("You must select between 1 and 3 sticks");
k = input.nextInt();
}
else
{
j = i;
i = i - k;
if (i <= 0)
{
System.out.println("Computer wins!");
score2 = (score2 + 1);
b = false;
}
else
{
System.out.println("We now have " + i + " sticks.");
}
d = c.select();
System.out.println("Computer removes " + d + " sticks");
i = i - d;
System.out.println("We now have " + i + " sticks");
if (i <= 0)
{
System.out.println("You Win!");
score1 = (score1 + 1);
b = false;
}
}
}
System.out.println();
System.out.println("Good game!");
System.out.println("Your score: " + score1 + " Computer's Score: " + score2);
System.out.println("Press enter if you'd like to play again. Otherwise, type \"quit\"");
exit = input.nextLine();
b = true;
}
while(!"quit".equals(exit));
}

感谢任何帮助!谢谢:)

~安德鲁

为 JANOS 编辑的代码

The Output at the end of the game

有点晚了,我知道,但这是为任何想玩的人准备的完整游戏!随时将其复制并粘贴到您的记事本中,然后使用 cmd 执行(您必须将我的名字作为评论放在最上面!):)

//Andrew Mancinelli: 2015
import java.util.*;
import java.io.*;
class Cup
{
private ArrayList<Integer> c = new ArrayList<Integer>();
public Cup()
{
c.add(1);
c.add(2);
c.add(3);
}
public int count()
{
return c.size();
}
public int select()
{
int index = (int)(c.size() * Math.random());
return c.get(index);
}
public void remove(Integer move)
{
c.remove(move);
}
}
class SticksGame
{
public static void help()
{
System.out.println();
System.out.println("Okay, so here's how it works... The object of the game is to NOT have the last stick. Whoever ends up with the very last stick loses.");
System.out.println();
System.out.println("Rule 1: You will each take turns removing sticks. you may only remove 1, 2, or 3 sticks in a turn");
System.out.println();
System.out.println("Rule 2: The beginning number of sticks is always random between 9 and 24 sticks");
System.out.println();
System.out.println("Rule 3: Whoever chooses the last stick, LOSES!");
System.out.println();
System.out.println("And that's it! Simple, right?");
}
public static void main(String[] args) throws InputMismatchException
{
Random r = new Random();
int score1 = 0, score2 = 0;
Cup c = new Cup();
int j = 0, d = 0, i = 0, k = 0;
boolean b = true;
String exit = "default", inst = "default";
Scanner input = new Scanner(System.in);
System.out.println("Welcome to the Sticks Game! Last Stick loses!");
System.out.println();
System.out.println("Need some instructions? Type \"help\" now to see the instructions. Otherwise, press enter to play!");
inst = input.nextLine();
if (inst.equals("help"))
{
help();
System.out.println();
System.out.println("press \"enter\" to begin!");
inst = input.nextLine();
}
do
{
i = r.nextInt(15) + 9;
System.out.println();
System.out.println("We begin with " + i + " sticks");
System.out.println();
while (b == true)
{
System.out.println("Your move");
k = input.nextInt();
if (k > 3)
{
System.out.println("You must select between 1 and 3 sticks");
k = input.nextInt();
}
else if (k < 1)
{
System.out.println("You must select between 1 and 3 sticks");
k = input.nextInt();
}
else
{
j = i;
i = i - k;
if (i <= 0)
{
System.out.println("Computer wins!");
score2 = (score2 + 1);
b = false;
break;
}
else
{
System.out.println("We now have " + i + " sticks.");
}
d = c.select();
i = i - d;
if (i >= 0)
{
System.out.println("Computer removes " + d + " sticks");
System.out.println("We now have " + i + " sticks");
}
if (i <= 0)
{
System.out.println("You Win!");
score1 = (score1 + 1);
b = false;
break;
}
}
}
System.out.println();
System.out.println("Good game!");
System.out.println("Your score: " + score1 + " Computer's Score: " + score2);
System.out.println("Press enter if you'd like to play again. Otherwise, type \"quit\"");
input.nextLine();
exit = input.nextLine();
b = true;
}
while(!"quit".equals(exit));
}
}

最佳答案

问题是这个条件总是为真:

while (exit != "quit");

因为 != 表示“不相同”,exit 变量和 "quit" 不相同。使用 equals 方法检查逻辑相等性

在此示例中,将循环条件改为:

while (!"quit".equals(exit));

关于你没有正确开始第二场比赛的其他问题,您需要重新初始化状态变量,例如重置 b = true

最后,请注意 input.nextInt() 不会读取您在输入数字时按下的换行符。因此,当 exit = input.nextLine() 运行时,它会读取换行符,实际上不会让您有机会键入“退出”。要解决此问题,请在 exit = input.nextLine();

之前添加 input.nextLine();

关于java - Java 中的 "Stick Game"程序无法正常运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33425750/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com