编程很新,我有点卡住了。这是我用两个类(两个单独的类文件)编写的第一个程序。我将包含以下所有代码。这是一个玩Craps骰子游戏的程序。我的任务是在CrapsGame类中创建一个方法,允许我一遍又一遍地玩游戏,直到我决定停止。但是我在解决如何正确调用manyPlay()方法时遇到了一些麻烦。我不太确定我在做什么。该程序将通过调用myCraps.play()来玩游戏一次,但之后将不再进行任何操作。如果其他人注意到代码或任何不良做法的任何错误,那么请指出,因为我非常渴望学习。感谢任何花时间回答的人。
using System;
namespace Task4_7
{
public class CrapsGame
{
string replay;
private Craps myCraps;
private CrapsGame newGame;
public static void Main()
{
CrapsGame newGame = new CrapsGame();
Craps myCraps = new Craps ();
myCraps.play ();
newGame.manyPlay ();
}
public void manyPlay() {
string input; // declare local variable
do {
myCraps.play();
replay:
Console.Write("Would you like to play again? y/n");
input = Console.ReadLine();
if (input == "y") {
replay = input;
}
else if (input == "n") {
replay = "n";
}
else {
Console.WriteLine("\n Erroneous input. Please enter y (yes) or n (no)");
goto replay;
}
}
while(replay != "n");
}
}
}
using System;
namespace Task4_7
{
public class Craps
{
private Random randy; // define randy as a Random class
public Craps() {
this.randy = new Random ();
}
public int oneThrow() {
return randy.Next(6) + 1; // pick a number from 1 to 6 and return this
}
public int throw2Dice() {
int a, b, c;
a = oneThrow ();
b = oneThrow ();
c = a + b;
Console.WriteLine ("You threw a " + a + " and a " + b + " making " + c);
return c;
}
public void play() {
int result = throw2Dice ();
switch (result) {
case 2:
Console.WriteLine ("You lose! End of game!");
break;
case 3:
Console.WriteLine ("You lose! End of game!");
break;
case 12:
Console.WriteLine ("You lose! End of game!");
break;
case 7:
Console.WriteLine ("You win! End of game!");
break;
case 11:
Console.WriteLine ("You win! End of game!");
break;
case 4:
Console.WriteLine ("Your point! Rolling again!");
throwPoint (result);
break;
case 5:
Console.WriteLine ("Your point! Rolling again!");
throwPoint (result);
break;
case 6:
Console.WriteLine ("Your point! Rolling again!");
throwPoint (result);
break;
case 8:
Console.WriteLine ("Your point! Rolling again!");
throwPoint (result);
break;
case 9:
Console.WriteLine ("Your point! Rolling again!");
throwPoint (result);
break;
default:
Console.WriteLine ("Your point! Rolling again!");
throwPoint (result);
break;
}
}
public void throwPoint(int result) {
Throw:
int a = throw2Dice();
if (a == result) {
Console.WriteLine ("You rolled the same score! You win!");
} else if (a == 7) {
Console.WriteLine ("You rolled a 7! You loose!");
} else {
Console.WriteLine ("You rolled a " + a + ". Rolling again!");
goto Throw;
}
}
}
}
答案 0 :(得分:0)
这是你的问题:
Craps myCraps = new Craps();
您正在使用CrapsGame
方法的局部变量隐藏类Main
的变量。只需将其更改为
myCraps = new Craps();
它应该有用。
修改强>
当然,并将myCraps
声明更改为static
。
<强> EDIT2 强>
答案 1 :(得分:0)
我的第一个建议是将您的Main
方法放在单独的文件中。例如,您可以使用三个文件:Program.cs,
CrapsGame.cs
,Craps.cs
。为此,只需添加一个新的类文件,然后将main方法移动到新的Class文件中。第二,永远不要使用goto.Instead使用while循环,
public void throwPoint(int result) {
while(true)
{
int a = throw2Dice();
if (a == result) {
Console.WriteLine ("You rolled the same score! You win!");
break;
} else if (a == 7) {
Console.WriteLine ("You rolled a 7! You loose!");
break;
} else {
Console.WriteLine ("You rolled a " + a + ". Rolling again!");
}
}
}
许多游戏方法:
public void manyPlay() {
string input; // declare local variable
myCraps.play();
while(true) {
Console.Write("Would you like to play again? y/n");
input = Console.ReadLine();
if (input == "y") {
myCraps.play();
}
else if (input == "n") {
break;
}
else {
Console.WriteLine("\n Erroneous input. Please enter y (yes) or n (no)");
}
}
}
答案 2 :(得分:0)
问题看起来像是在创建游戏供游戏迭代器使用的地方,但它需要为每次迭代创建一个游戏。所以我删除了一些代码:所有类变量都必须去,变量应该尽可能本地。
using System;
namespace Task4_7
{
public class CrapsGame
{
public static void Main()
{
new Craps ().play ();
new CrapsGame().manyPlay ();
}
public void manyPlay() {
string replay;
string input; // declare local variable
do {
new Craps().play();
replay:
Console.Write("Would you like to play again? y/n");
input = Console.ReadLine();
if (input == "y") {
replay = input;
}
else if (input == "n") {
replay = "n";
}
else {
Console.WriteLine("\n Erroneous input. Please enter y (yes) or n (no)");
goto replay;
}
}
while(replay != "n");
}
}
}
现在修复那个循环:没有了。你需要循环循环,但不要构建自己的循环。
然后,如果Craps.play()返回了分数。您可以添加一个类变量来累积分数。
答案 3 :(得分:-2)
将重播更改为布尔值,如果输入为y,则将其设置为true;如果输入为n,则将其设置为false。
然后改变你的主要方法。
public static void Main()
{
CrapsGame newGame = new CrapsGame();
Craps myCraps = new Craps ();
while (replay == true) {
myCraps.play ();
newGame.manyPlay ();
}
}