C#代码在循环中表现得很奇怪

时间:2016-06-07 19:06:41

标签: c# console-application

当我尝试向我的代码添加一个循环时,它开始承担决定并在某些writelines的开头添加“f”或“d”。 应用程序的前8行是正确的

Picture of console

代码循环“按任意键继续”

代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RockPaperScissors
{
    class Program
    {
    static void Main(string[] args)
    {
        {
            int userScore = 0;
            int computerScore = 0;
            for (int isRunning = 0; isRunning < 10;)
            {
                Random rnd = new Random();
                int computerChoice = rnd.Next(1, 150);
                Console.WriteLine("Welcome to rock, paper, scissors!");
                System.Threading.Thread.Sleep(400);
                Console.WriteLine("Press escape to exit");
                Console.Write("Rock, paper or scissors? ");
                string userChoice = Console.ReadLine();
                if (userChoice == "rock")
                {
                    if (computerChoice <= 50)
                    {
                        Console.WriteLine("The computer chooses rock.");
                        System.Threading.Thread.Sleep(1000);
                        if (userChoice == "rock")
                        {
                            Console.WriteLine("Tie!");
                            System.Threading.Thread.Sleep(500);
                            Console.WriteLine("Computer score is: " + computerScore + "!");
                            Console.WriteLine("Your score is: " + userScore + "!");
                            System.Threading.Thread.Sleep(500);
                            Console.WriteLine("Press any key to continue");
                            Console.ReadKey();
                        }
                        else if (userChoice == "paper");
                        {
                            Console.WriteLine("Paper beats rock! You win!");
                            userScore++;
                            System.Threading.Thread.Sleep(1000);
                            Console.WriteLine("Computer score is: " + computerScore + "!");
                            Console.WriteLine("Your score is: " + userScore + "!");
                            System.Threading.Thread.Sleep(500);
                            Console.WriteLine("Press any key to continue");
                            Console.ReadKey();
                        } }
                    else if (userChoice == "scissors")
                    {
                        Console.WriteLine("Rock beats scissors. You lose!");
                        computerScore++;
                        System.Threading.Thread.Sleep(1000);
                        Console.WriteLine("Computer score is: " + computerScore + "!");
                        Console.WriteLine("Your score is: " + userScore + "!");
                        System.Threading.Thread.Sleep(500);
                        Console.WriteLine("Press any key to continue");
                        Console.ReadKey();
                    }

                }
                if (computerChoice >= 51 && computerChoice <= 100)
                {
                    Console.WriteLine("The computer chooses scissors.");
                    System.Threading.Thread.Sleep(1000);
                    if (userChoice == "rock")
                    {
                        Console.WriteLine("Rock beats scissors! You win!");
                        userScore++;
                        System.Threading.Thread.Sleep(500);
                        Console.WriteLine("Computer score is: " + computerScore + "!");
                        Console.WriteLine("Your score is: " + userScore + "!");
                        System.Threading.Thread.Sleep(500);
                        Console.WriteLine("Press any key to continue");
                        Console.ReadKey();
                    }
                    else if (userChoice == "paper")
                    {
                        Console.WriteLine("Scissots cut paper! You lose.");
                        computerScore++;
                        System.Threading.Thread.Sleep(1000);
                        Console.WriteLine("Computer score is: " + computerScore + "!");
                        Console.WriteLine("Your score is: " + userScore + "!");
                        System.Threading.Thread.Sleep(500);
                        Console.WriteLine("Press any key to continue");
                        Console.ReadKey();
                    } }
                else if (userChoice == "scissors")
                {
                    Console.WriteLine("Tie!");
                    System.Threading.Thread.Sleep(1000);
                    Console.WriteLine("Computer score is: " + computerScore + "!");
                    Console.WriteLine("Your score is: " + userScore + "!");
                    System.Threading.Thread.Sleep(500);
                    Console.WriteLine("Press any key to continue");
                    Console.ReadKey();
                }
                if (computerChoice >= 101)
                {
                    Console.WriteLine("The computer chooses paper.");
                    System.Threading.Thread.Sleep(1000);
                    if (userChoice == "rock")
                    {
                        Console.WriteLine("Paper beats rock! You lose.");
                        computerScore++;
                        System.Threading.Thread.Sleep(500);
                        Console.WriteLine("Computer score is: " + computerScore + "!");
                        Console.WriteLine("Your score is: " + userScore + "!");
                        System.Threading.Thread.Sleep(500);
                        Console.WriteLine("Press any key to continue");
                        Console.ReadKey();
                    }
                    else if (userChoice == "paper")
                    {
                        Console.WriteLine("Tie!");
                        System.Threading.Thread.Sleep(1000);
                        Console.WriteLine("Computer score is: " + computerScore + "!");
                        Console.WriteLine("Your score is: " + userScore + "!");
                        System.Threading.Thread.Sleep(500);
                        Console.WriteLine("Press any key to continue");
                        Console.ReadKey();
                    }
                }
                else if (userChoice == "scissors")
                {
                    Console.WriteLine("Scissors cut paper! You win!");
                    userScore++;
                    System.Threading.Thread.Sleep(1000);
                    Console.WriteLine("Computer score is: " + computerScore + "!");
                    Console.WriteLine("Your score is: " + userScore + "!");
                    System.Threading.Thread.Sleep(500);
                    Console.WriteLine("Press any key to continue");
                    Console.ReadKey();
                }
            }
            if (computerScore > userScore)
            {
                Console.WriteLine("Game over!");
                Console.WriteLine("The computer won this round. Better luck next time.");
                Console.WriteLine("Press any key to continue.");
                Console.ReadKey();
            }
            else if (userScore < computerScore)
            {
                Console.WriteLine("You win!");
                Console.WriteLine("You beat the computer! Good job.");
                Console.WriteLine("Press any key to continue.");
                Console.ReadKey();
            }
        }
    }
}
}

感谢和抱歉长代码

2 个答案:

答案 0 :(得分:2)

在“按任意键继续”提示后,您的控制台似乎正在显示您的按键。

规避这种情况的一种可能方法是允许他们按任意键继续,然后添加额外的换行符:

Console.WriteLine();

您还可以考虑通过以下调用在每次回答后清除输出窗口:

Console.Clear();

答案 1 :(得分:0)

我可以看到几件事。首先你的for循环没有增量器,所以它将永远运行,永远不会达到退出条件。你应该 将其更改为

for (int isRunning = 0; isRunning < 10; isRunning++) 

第二,如果用户按下'Esc'键,我看不到你的退出命令在哪里。所以你的Console.ReadKey应该是

if (Console.ReadKey().Key == ConsoleKey.Escape) break;