C# 再次玩数字游戏

C# 再次玩数字游戏,c#,string,C#,String,我试图实现用户输入Y或N以再次玩游戏或退出游戏的功能,但我很难在不大规模重写代码逻辑的情况下让自己的头脑清醒过来。。。我今天刚刚回到C#中,我是一名初学者,所以请对我放松:)我已经准备好userContinue字符串,只想输入一种简单的方法来重复游戏并添加保持分数的方法(慢慢改进游戏) 使用系统; 命名空间猜测\u编号\u V2 { 班级计划 { 静态void Main(字符串[]参数) { 做 { 游戏(); Console.WriteLine(“你想再玩一次吗?Y还是N”); }而(Cons

我试图实现用户输入Y或N以再次玩游戏或退出游戏的功能,但我很难在不大规模重写代码逻辑的情况下让自己的头脑清醒过来。。。我今天刚刚回到C#中,我是一名初学者,所以请对我放松:)我已经准备好userContinue字符串,只想输入一种简单的方法来重复游戏并添加保持分数的方法(慢慢改进游戏)

使用系统;
命名空间猜测\u编号\u V2
{
班级计划
{
静态void Main(字符串[]参数)
{
做
{
游戏();
Console.WriteLine(“你想再玩一次吗?Y还是N”);
}而(Console.ReadLine().ToLower()=“y”);
}
公共静态voic游戏()
{
Random rand=新的Random();
int randNum=rand.Next(1,11);
int不正确猜测=0;
int userScore=10;
int-userGuess;
int-perGuess=1;
字符串userContinue;
bool-correctGuess=false;
Console.WriteLine(“输入一个介于1和10之间的数字\n核心从10开始,每猜一次错误将扣一分。”);
userGuess=int.Parse(Console.ReadLine());
while(correctGuess==false)
{
if(userGuess==randNum)
{
Console.ForegroundColor=ConsoleColor.Green;
WriteLine(“你的猜测是对的,数字是{0}!总分是{1},你有{2}个错误的猜测。”,randNum,userScore,不正确的猜测);
正确猜测=正确;
}
if(userGuess>randNum)
{
userScore-=perGuess;
不正确的猜测++;
Console.ForegroundColor=ConsoleColor.Red;
控制台。WriteLine(“又猜错了,高了!”);
正确猜测=错误;
}
else if(userGuess
如果您将游戏逻辑放在自己的方法中,这会有所帮助。说
PlayGame()
。然后你可以简单地写:

static void Main(string[] args)
{
    do {
        PlayGame();
        Console.ForegroundColor = ConsoleColor.White;
        Console.WriteLine("Would you play to play again? Y or N");
    } while (Console.ReadLine().ToLower() == "y");
}
此外,如果您创建一个“无限”循环并使用
break打破它,您可以简化您的逻辑当猜测正确时

您可以在每个循环的开头读取和解析用户输入

设置用户分数错误猜测次数和红色控制台颜色也只能进行一次

测试和工作代码:

using System;

namespace Guess_Number_V2
{
    class Program
    {
        static void Main(string[] args)
        {
            do {
                PlayGame();
                Console.ForegroundColor = ConsoleColor.White;
                Console.WriteLine("Would you play to play again? Y or N");
            } while (Console.ReadLine().ToLower() == "y");
        }

        private static void PlayGame()
        {
            Random rand = new Random();
            int randNum = rand.Next(1, 11);
            int incorrectGuesses = 0;
            int userScore = 10;
            int userGuess;
            int perGuess = 1;

            Console.WriteLine("Enter a number between 1 and 10\nScore starts at 10, one point will be deducted with each incorrect guess.");

            while (true) {
                userGuess = int.Parse(Console.ReadLine());
                if (userGuess == randNum) {
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.WriteLine("Your guess was right, the number was {0}! Total score is {1} and you had {2} incorrect guesses.", randNum, userScore, incorrectGuesses);
                    break;
                }
                userScore -= perGuess;
                incorrectGuesses++;
                Console.ForegroundColor = ConsoleColor.Red;
                if (userGuess > randNum) {
                    Console.WriteLine("Wrong guess again, to high!");
                } else { // Must be userGuess < randNum
                    Console.WriteLine("Wrong guess again, to low!");
                }
            }
        }
    }
}
使用系统;
命名空间猜测\u编号\u V2
{
班级计划
{
静态void Main(字符串[]参数)
{
做{
游戏();
Console.ForegroundColor=ConsoleColor.White;
Console.WriteLine(“你想再玩一次吗?Y还是N”);
}而(Console.ReadLine().ToLower()=“y”);
}
私有静态void游戏()
{
Random rand=新的Random();
int randNum=rand.Next(1,11);
int不正确猜测=0;
int userScore=10;
int-userGuess;
int-perGuess=1;
Console.WriteLine(“输入一个介于1和10之间的数字\n核心从10开始,每猜一次错误将扣一分。”);
while(true){
userGuess=int.Parse(Console.ReadLine());
if(userGuess==randNum){
Console.ForegroundColor=ConsoleColor.Green;
WriteLine(“你的猜测是对的,数字是{0}!总分是{1},你有{2}个错误的猜测。”,randNum,userScore,不正确的猜测);
打破
}
userScore-=perGuess;
不正确的猜测++;
Console.ForegroundColor=ConsoleColor.Red;
if(userGuess>randNum){
控制台。WriteLine(“又猜错了,高了!”);
}else{//必须是userGuess
这是否回答了您的问题?只有一点,逻辑对我不起作用。这段代码在循环之前只读取用户输入一次,而不是作为循环中的第一条语句。因此,当第一次猜测错误时,循环从不停止,也从不读取用户输入。尝试过这样做,但不幸的是,当我猜到正确的数字时,它只会说按任意键继续,控制台应用程序退出。我更新了我的代码,使其在自己的方法中。编辑:哦,我必须说,非常好,如果我想把用户的得分结转,并在每次重复的游戏中加起来,该怎么办。我会简单地将分数变量设置为全局变量并进行更新?因为我们使用的是do while循环,在每次游戏结束后读取控制台,应用程序不能在游戏结束时退出。修复了它编译的问题,但应用了相同的问题,我甚至在do-while循环上设置了一个断点,它似乎没有到达程序的while-console-readline位…遗憾的是,代码中的同一个问题不允许我按Y继续,只是说按任意键继续。。。当我按下一个键,它就退出了,我知道出了什么问题,但我无法理解我的代码从未打印出“按任意键继续…”,所以你一定是在运行错误的项目。每个项目必须只包含一个
Main
方法。如果您的解决方案包含多个项目,请确保在visual studio中选择正确的项目。右键单击项目并选择“设置为启动项目”
using System;

namespace Guess_Number_V2
{
    class Program
    {
        static void Main(string[] args)
        {
            do {
                PlayGame();
                Console.ForegroundColor = ConsoleColor.White;
                Console.WriteLine("Would you play to play again? Y or N");
            } while (Console.ReadLine().ToLower() == "y");
        }

        private static void PlayGame()
        {
            Random rand = new Random();
            int randNum = rand.Next(1, 11);
            int incorrectGuesses = 0;
            int userScore = 10;
            int userGuess;
            int perGuess = 1;

            Console.WriteLine("Enter a number between 1 and 10\nScore starts at 10, one point will be deducted with each incorrect guess.");

            while (true) {
                userGuess = int.Parse(Console.ReadLine());
                if (userGuess == randNum) {
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.WriteLine("Your guess was right, the number was {0}! Total score is {1} and you had {2} incorrect guesses.", randNum, userScore, incorrectGuesses);
                    break;
                }
                userScore -= perGuess;
                incorrectGuesses++;
                Console.ForegroundColor = ConsoleColor.Red;
                if (userGuess > randNum) {
                    Console.WriteLine("Wrong guess again, to high!");
                } else { // Must be userGuess < randNum
                    Console.WriteLine("Wrong guess again, to low!");
                }
            }
        }
    }
}