我创建了一个用户可以玩石头剪刀的程序。
我希望能够做的事情之一就是询问用户是否想再次玩游戏,如果他们重新开始游戏,如果不重新开始,那么它就会结束。
我有所有设置功能,我在类#34; MainClass"(playAgainn是函数名称)中的while循环之后调用该函数但由于某种原因它不被调用并且程序最后列出CPU和播放器的分数,然后按Enter键不做任何事情,这样程序就会保持打开状态。
有谁知道这是为什么或如何更改我的代码,以便我能够根据" y"的用户输入循环程序。或" n" - 非常感谢帮助。如果有帮助,我使用Microsoft Visual Studio社区2015在C#中编码。该守则列于下方。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProgramV3
{
class RPS
{
public static int scorePlayer = 0;
public static int scoreCPU = 0;
public static int playerWon = 0;
public static int cpuWon = 0;
static string inputPlayer, inputCPU;
public static int randomInt;
public static bool playAgain = true;
Random rnd = new Random();
public static void userInput()
{
Console.Write("Choose between ROCK, PAPER and SCISSORS: ");
inputPlayer = Console.ReadLine();
inputPlayer = inputPlayer.ToUpper();
Random rnd = new Random();
randomInt = rnd.Next(1, 4);
}
public static void CheckWhoWinsEach()
{
switch (randomInt)
{
case 1:
inputCPU = "ROCK";
Console.WriteLine("Computer chose ROCK");
if (inputPlayer == "ROCK")
{
Console.WriteLine("DRAW THIS ROUND!!\n\n");
}
else if (inputPlayer == "PAPER")
{
Console.WriteLine("PLAYER WINS THIS ROUND!!\n\n");
scorePlayer++;
}
else if (inputPlayer == "SCISSORS")
{
Console.WriteLine("CPU WINS THIS ROUND!!\n\n");
scoreCPU++;
}
break;
case 2:
inputCPU = "PAPER";
Console.WriteLine("Computer chose PAPER");
if (inputPlayer == "PAPER")
{
Console.WriteLine("DRAW THIS ROUND!!\n\n");
}
else if (inputPlayer == "ROCK")
{
Console.WriteLine("CPU WINS THIS ROUND!!\n\n");
scoreCPU++;
}
else if (inputPlayer == "SCISSORS")
{
Console.WriteLine("PLAYER WINS THIS ROUND!!\n\n");
scorePlayer++;
}
break;
case 3:
inputCPU = "SCISSORS";
Console.WriteLine("Computer chose SCISSORS");
if (inputPlayer == "SCISSORS")
{
Console.WriteLine("DRAW THIS ROUND!!\n\n");
}
else if (inputPlayer == "ROCK")
{
Console.WriteLine("PLAYER WINS THIS ROUND!!\n\n");
scorePlayer++;
}
else if (inputPlayer == "PAPER")
{
Console.WriteLine("CPU WINS!!\n\n");
scoreCPU++;
}
break;
default:
Console.WriteLine("Invalid entry!");
break;
}
Console.WriteLine("\n\nSCORES:\tPLAYER:\t{0}\tCPU:\t{1}", scorePlayer, scoreCPU);
}
public static void CheckWhoWon()
{
if (scorePlayer == 3)
{
Console.WriteLine("Player OVERALL!");
playerWon++;
Console.WriteLine("CPU HAS WON: {0} TIMES AND PLAYER HAS WON {1} TIMES", cpuWon, playerWon);
}
else if (scoreCPU == 3)
{
Console.WriteLine("CPU WON OVERALL!");
cpuWon++;
Console.WriteLine("CPU HAS WON: {0} TIMES AND PLAYER HAS WON: {1} TIMES",cpuWon, playerWon);
}
else
{
}
}
public static void playAgainn()
{
Console.WriteLine("Do you want to play again?(y/n)");
string loop = Console.ReadLine();
if (loop == "y")
{
playAgain = true;
Console.Clear();
}
else if (loop == "n")
{
playAgain = false;
}
else
{
}
}
}
class MainClass
{
public static void Main(string[] args)
{
while (RPS.playAgain)
{
while (RPS.scorePlayer < 3 && RPS.scoreCPU < 3)
{
RPS.userInput();
RPS.CheckWhoWinsEach();
RPS.CheckWhoWon();
}
}
RPS.playAgainn();
}
}
}
答案 0 :(得分:1)
您需要将RPS.playAgainn();
向上移动进入其上方的循环。
然后mainClass
将如下所示:
class MainClass
{
public static void Main(string[] args)
{
while (RPS.playAgain)
{
while (RPS.scorePlayer < 3 && RPS.scoreCPU < 3)
{
RPS.userInput();
RPS.CheckWhoWinsEach();
RPS.CheckWhoWon();
}
RPS.playAgainn();
}
}
}
答案 1 :(得分:0)
你的循环中有几个问题。
playAgain
。y
时,您应该重置分数。再次播放
public static void playAgainn()
{
Console.WriteLine("Do you want to play again?(y/n)");
string loop = Console.ReadLine();
if (loop == "y")
{
playAgain = true;
// reset scores here
Console.Clear();
}
else if (loop == "n")
{
playAgain = false;
}
else
{
}
}
主要班级
class MainClass
{
public static void Main(string[] args)
{
while (RPS.playAgain)
{
while (RPS.scorePlayer < 3 && RPS.scoreCPU < 3)
{
RPS.userInput();
RPS.CheckWhoWinsEach();
RPS.CheckWhoWon();
}
RPS.playAgainn();
}
}
}
答案 2 :(得分:0)
您是否尝试过放置RPS.playAgainn();恰好在前面的右括号之前?我在手机上阅读它,我认为它超出了RPS.playAgain = true区域,所以它会在完成后执行。
答案 3 :(得分:0)
playAgainn()函数的问题是它不是一个函数。我认为它不会编译。
public static bool playAgainn()
{
Console.WriteLine("Do you want to play again?(y/n)");
string loop = Console.ReadLine();
if (loop == "y")
{
Console.Clear();
return true;
}
else
{
return false;
}
}