如何重新开始游戏并提出一些问题

时间:2016-11-27 11:23:46

标签: c#

我制作了一个以不同方式计算温度的程序。

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

namespace Excercise_7
{
    class Program
    {
        static void Main(string[] args)
        {
            showBegin();
            decide();   
            secondPlay();
            showEnd();

        }
    // Decide code
    static void decide() {
        int choice;
        string input;
        input = Console.ReadLine();
        int.TryParse(input, out choice);
        if (choice == 1)
        {
            Console.WriteLine("");
            celciusSystem();
        }
        else if (choice == 2)
        {
            Console.WriteLine("");
            farenheitSystem();
        }
        else if (choice == 3)
        {
            Console.WriteLine("");
            kelvinSystem();
        }
        else {
            Console.WriteLine("");
            Console.WriteLine("Make sure u write a number between 1 and 3! No text!");
            Console.WriteLine("");
            showBegin();
            decide();
        }
    }
    static void secondPlay()
    {
        int choice2;
        string input;
        Console.WriteLine("");
        Console.WriteLine("Type 1 to play again, type 2 to close the application");
        input = Console.ReadLine();
        int.TryParse(input, out choice2);

    }

    // Begin code
    static void showBegin()
    {
        Console.WriteLine("Welcom at the temperature calculator, pick the mode u prefer!");
        Console.WriteLine("Press 1 for Celcius mode");
        Console.WriteLine("Press 2 for Fahrenheit mode");
        Console.WriteLine("Press 3 for Kelvin mode");
        Console.WriteLine("Press the correct number and then hit the enter button!");
        Console.WriteLine("");
    }
    // Temperature Calculators
    static void celciusSystem()
    {
        Console.WriteLine("This is the Celsius calculator");
        Console.Write("Enter the amount of celsius: ");

        int celsius = Convert.ToInt32(Console.ReadLine());

        Console.WriteLine("");
        Console.WriteLine("The temperature in Kelvin = {0}", celsius + 273);
        Console.WriteLine("The temperature in Fahrenheit = {0}", celsius * 18 / 10 + 32);

    }
    static void farenheitSystem()
    {
        Console.WriteLine("This is the Fahrenheit calculator");
        Console.Write("Enter the amount of Fahrenheit: ");

        int Fahrenheit = Convert.ToInt32(Console.ReadLine());

        Console.WriteLine("");
        Console.WriteLine("The temperature in Kelvin = {0}", (Fahrenheit + 459.67) / 1.8);
        Console.WriteLine("The temperature in Celsius = {0}", (Fahrenheit - 32) / 1.8);

    }

    static void kelvinSystem()
    {
        Console.WriteLine("This is the Kelvin calculator");
        Console.Write("Enter the amount of Kelvin: ");

        int Kelvin = Convert.ToInt32(Console.ReadLine());

        Console.WriteLine("");
        Console.WriteLine("The temperature in Fahrenheit = {0}", Kelvin * 1.8 - 459.67);
        Console.WriteLine("The temperature in Celsius = {0}", Kelvin - 273.15);

    }
    // End code
    static void showEnd()
    {
        Console.WriteLine("");
        Console.WriteLine("We hoped u enjoyed our application!");
        Console.WriteLine("Press enter to exit...");
        Console.ReadLine();
    }


}
}

所以现在我的问题是,我做了一个叫做第二部剧的课。在那个课程中,我试图做一个声明,询问用户他或她是否想再次“玩”。因此,如果他们写1,他应该再次循环游戏。

我已经尝试过这个

  if (choice2 == 1)
        {
            while (choice2 == 1) {
                Console.WriteLine("");
                showBegin();
                decide();
            }

        }
        else {
            Console.WriteLine("");
            Console.WriteLine("Cheers!!");
        }
    }

然后他实际上工作但不是我想要的方式,因为如果用户写1,它将重新循环第一件事。所以用户不能再退出游戏了。

我希望应用程序在每次跟踪后询问是否要通过键入1或2再次播放。

3 个答案:

答案 0 :(得分:0)

当你想要至少执行一次循环时,你可以使用一个do while循环,在你的情况下很适合你:

do {
    Console.WriteLine("");
    showBegin();
    decide();
    //Ask for choice2 here
}
while(choice2 == 1);

Console.WriteLine("");
Console.WriteLine("Cheers!!");

答案 1 :(得分:0)

要进入游戏循环,从简单的whiledo-while开始最为有用。

while( conditionToRun )
{
   //run the program
   if( userWantToExit )
     conditionToRun = false;
}

或者

do
{
    // run the program
} while ( !userWantToExit )

此代码显示了处理此问题的最基本内容。

要申请您的游戏/计划,请使用:

 static void Main(string[] args)
 {
     bool userWantToEnd = false;

     showBegin();  

     while( !userWantToEnd)
     {
         decide();   
         userWantToEnd = IsUserWillingToContinue();
     }

     showEnd();

 }

并将SecondPlay()方法调整为IsUserWillingToContinue()

public bool IsUserWillingToContinue()
{
    int choice2;
    string input;
    Console.WriteLine("");
    Console.WriteLine("Type 1 to play again, type 2 to close the application");
    input = Console.ReadLine();
    int.TryParse(input, out choice2);

    return choice2 == 1;
}

根据@devRicher的评论添加更多信息 - 您应该将代码拆分一下,以便它以这种方式工作

bool runAgain = true;

Console.WriteLine("Begin info: (Hello this is XY game.)");
while(runAgain)
{
     Console.WriteLine("You have 3 options: 1, 2, 3");
     PickOptionAndRunTheCode();

     bool runAgain = DoYouWantToContinue();
     Console.Clear(); // clear the Console screen (next time You will see only options, not the text on first run).
}

我建议您了解有关游戏的更多信息,请访问此页面 - > http://gameprogrammingpatterns.com/(并阅读本书)

检查此模式:Game Loop

答案 2 :(得分:0)

你已经知道如何从控制台读取行,你也知道如何使用if来比较布尔值,所以为什么不用它来检查它们是否写了1或2?

private bool playAgain()
{
    int number = 2; //Invalid option: close game
    Int32.TryParse(Console.ReadLine(), out number);
    if (number == 1)
    {
        return true;
    }
    else if (number == 2)
    {
        return false;
    }
    else
    {
        Console.WriteLine("Unknown option, quitting!");
        return false;
    }
}

然后你只需要使用该功能来确定他们是否想要在do-loop中再次播放。

static void Main(string[] args)
{
    showBegin(); //Show begin sequence
    do
    {
    decide();   
    secondPlay();
    } while(playAgain()); //Play again if they want to
    showEnd(); //Show end sequence when they decided not to play

}

专业提示:使用switch-case语句而不是多个if-else语句。