我的if / else if陈述使我在运行之前多次输入

时间:2018-08-22 15:05:01

标签: c#

因此,当我运行代码时,可以按1并运行良好,但是当我尝试输入2或3时,计算机会在运行该行代码之前多次输入2或3。

我不知道这是什么问题。 我是新手,所以如果有更好的方法来运行类似if的语句,请让我知道。

using System;


namespace PracticeScript
{
    class Program
    {

        static void Main()
        {
            Random rnd = new Random();
            int playerHp = 100;
            int enemyHp = 100;
            int playerD = rnd.Next(10, 15);
            int enemyD = rnd.Next(10, 15);
            int potion = 25;


            Console.Write("Get Ready! It's time to battle! ");
            Console.WriteLine("You stare into the eyes of the ugly goblin and are ready to slay it with your sword \n");

            do
            {
                Console.WriteLine(" what will you do now?");
                Console.WriteLine("\n 1. Attack    2. Defend    3. Use potion");

                if (Console.ReadLine() == "1")
                {
                    enemyHp -= playerD;
                    Console.WriteLine("you swung your sword and struck the goblin in the body leaving it " + enemyHp + "HP left over");
                }
                else if (Console.ReadLine() == "2")
                {
                    Console.WriteLine("You prepaired your sheild for the incoming attack");
                }
                else if (Console.ReadLine() == "3")
                {
                    playerHp += potion;
                    Console.WriteLine("You chugged down the potion as quick as you could and now have " + playerHp + " left!" );
                }


                Console.WriteLine("\nEnemy Turn!");
                playerHp -= enemyD;
                Console.WriteLine("The goblin struck you with his mace and left you with " + playerHp + "HP left!");


            } while (playerHp > 0 || enemyHp > 0);

4 个答案:

答案 0 :(得分:4)

您需要先存储Console.ReadLine的结果,然后再测试其值:

string line = Console.ReadLine();

if (line == "1")
{
    enemyHp -= playerD;
    Console.WriteLine("you swung your sword and struck the goblin in the body leaving it " + enemyHp + "HP left over");
}
else if (line == "2")
{
    Console.WriteLine("You prepaired your sheild for the incoming attack");
}
else if (line == "3")
{
    playerHp += potion;
    Console.WriteLine("You chugged down the potion as quick as you could and now have " + playerHp + " left!" );
}

否则,您的程序将调用Console.ReadLine 3次,并在最坏的情况下提示用户3次。

答案 1 :(得分:2)

Console.ReadLine()每次您输入时都会提示输入。

您可以使用switch语句来请求一次输入,并将其与您要处理的每个值进行比较:

switch (Console.ReadLine())
 {
    case "1":
       // ...
       break;

    case "2":
       // ...
       break;
   // Add rest of cases here
}

答案 2 :(得分:0)

基本上,每次用户输入值时,您都在评估每个Console.Readline()。因此,用户输入2if (Console.ReadLine() == "1")的值为false。再次输入2,else if (Console.ReadLine() == "2")的值为true。正如其他人所述,每个循环一次使用Console.ReadLine(),存储值,并在您的if语句中检查它:

string userinput = Console.ReadLine();
if(userinput == "1")
    // do stuff
else if(userinput == "2")
    // do other stuff
//etc.

答案 3 :(得分:0)

我建议您使用switch

问题是您重复了Console.ReadLine() 3次,而不是存储和使用其值!

此外,我添加了对用户输入的检查,您没有想到可能存在无效输入

class Program
{
    static void Main(string[] args)
    {
        Random rnd = new Random();
        int playerHp = 100;
        int enemyHp = 100;
        int playerD = rnd.Next(10, 15);
        int enemyD = rnd.Next(10, 15);
        int potion = 25;


        Console.Write("Get Ready! It's time to battle! ");
        Console.WriteLine("You stare into the eyes of the ugly goblin and are ready to slay it with your sword \n");

        do
        {
            Console.WriteLine(" what will you do now?");
            Console.WriteLine("\n 1. Attack    2. Defend    3. Use potion");


            bool isInputValid = false;
            while (isInputValid == false)
            {
                isInputValid = true;
                switch (Console.ReadLine())
                {
                    case "1":
                        enemyHp -= playerD;
                        Console.WriteLine("you swung your sword and struck the goblin in the body leaving it " + enemyHp + "HP left over");
                        break;
                    case "2":
                        Console.WriteLine("You prepaired your sheild for the incoming attack");
                        break;
                    case "3":
                        playerHp += potion;
                        Console.WriteLine("You chugged down the potion as quick as you could and now have " + playerHp + " left!");
                        break;
                    default:
                        Console.WriteLine("Invalid input, please choose a listed action!");
                        isInputValid = false;
                        break;
                }
            }

            Console.WriteLine("\nEnemy Turn!");
            playerHp -= enemyD;
            Console.WriteLine("The goblin struck you with his mace and left you with " + playerHp + "HP left!");


        } while (playerHp > 0 || enemyHp > 0);
    }
}