我最近决定制作一个程序,玩一个名为" Nim,"这是一个游戏,你开始预定数量的"棍棒"并且每个玩家轮流移除1到3支。除去最后一根棍子的人都输了。
无论如何,我已经编写了我的程序,它编译并运行几乎完美无缺。这只是一个小问题。比赛结束后,它会显示出好的比赛"屏幕两次,游戏的第一行显示在中间(我将在此处的屏幕截图中显示)。这很奇怪,我只是想知道你们是否可以试一试。
我把一大块节目剪掉了(只有一个类,名为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编辑的代码
有点晚了,我知道,但对于任何想玩的人来说,这里都是完整的游戏!随意复制并粘贴到你的记事本中并使用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));
}
}
答案 0 :(得分:2)
问题是这个条件总是是真的:
while (exit != "quit");
因为!=
表示&#34;不相同&#34;,
并且exit
变量和"quit"
不相同。
使用equals
方法检查逻辑相等。
在此示例中,将循环条件更改为:
while (!"quit".equals(exit));
对于你没有正确开始第二场比赛的其他问题,
你需要重新初始化状态变量,
例如重置b = true
。
最后,请注意input.nextInt()
无法读取您在输入数字时按下的换行符。因此,当exit = input.nextLine()
运行时,它会读取该换行符,并且实际上并没有给您输入&#34;退出&#34;的机会。要解决此问题,请在input.nextLine();
exit = input.nextLine();
答案 1 :(得分:1)
意外的重试是因为使用了input.nextLine();程序假设您已经按[enter]。
从以前的工作中,两个选项是再插入一个input.nextline();
input.nextLine();
exit = input.nextLine();
或者使用input.next();相反,虽然输入不适用于此方法,因此您可能需要输入任意键或“退出”退出;
exit = input.next();