我将如何在控制台应用程序中实现一个简单的基于文本的菜单

本文关键字:简单 一个 菜单 文本 于文本 控制台 实现 应用程序 | 更新日期: 2023-09-27 18:20:31

我想在我为好玩而写的折扣价格计算器中有一个选项来重新启动程序,并想知道如何最好地执行它。我想让它说类似于:"你想输入另一个价格吗?"如果用户说"是"、"y"或"否"等,请重新启动程序或退出。我可能应该使用if循环,对吧?有人能告诉我如何实施吗?或者给我指一个正确的方向?我也觉得我应该重写程序以获得方法,但我不熟悉C#。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Figure_the_Discount
{
class Program
{
    static void Main(string[] args)
    {
        string price, discount;
        decimal discountedPrice, savedAmount;
        //Receiving the price as input
        Console.WriteLine("Please enter the price of the item.");
        price = Console.ReadLine();
        decimal numPrice = decimal.Parse(price);
        //Receiving the discount as input
        Console.WriteLine("Please enter the discount that you wish to apply");
        discount = Console.ReadLine();
        //Receiving discount from input, divide by 100 to convert to percentile
        decimal numDiscount = decimal.Parse(discount) / 100;
        //Calculate the discounted price with price - (price * discount)
        discountedPrice = numPrice - (numPrice * numDiscount);
        //Calculate the amount of money they saved
        savedAmount = numPrice - discountedPrice;
        Console.WriteLine("The discounted price of this item is: ${0}'nYou saved: ${1}", discountedPrice, savedAmount);
        Console.ReadLine();
    }
}
}

我将如何在控制台应用程序中实现一个简单的基于文本的菜单

只要用户在程序结束时指定yyes(不区分大小写),以下循环就会循环进行。

static void Main(string[] args)
{
    string price, discount;
    decimal discountedPrice, savedAmount;
    bool startAgain = true;
    string line;
    // Loop again every time the startAgain flag is true.
    while (startAgain)
    {
        //Receiving the price as input
        Console.WriteLine("Please enter the price of the item.");
        price = Console.ReadLine();
        decimal numPrice = decimal.Parse(price);
        //Receiving the discount as input
        Console.WriteLine("Please enter the discount that you wish to apply");
        discount = Console.ReadLine();
        //Receiving discount from input, divide by 100 to convert to percentile
        decimal numDiscount = decimal.Parse(discount) / 100;
        //Calculate the discounted price with price - (price * discount)
        discountedPrice = numPrice - (numPrice * numDiscount);
        //Calculate the amount of money they saved
        savedAmount = numPrice - discountedPrice;
        Console.WriteLine("The discounted price of this item is: ${0}'nYou saved: ${1}", discountedPrice, savedAmount);
        Console.ReadLine();
        // Ask if the user wants to submit another price.
        Console.WriteLine("Would you like to enter another price?");
        // Record the spaceless, lower-case answer.
        line = Console.ReadLine().ToLowerCase().Trim();
        // Set the startAgain flag to true only if the line was "y" or "yes".
        startAgain = line == "y" || line == "yes";
    }
}

Just s粗略样本

string input = null;
do {

    //your code 
    Console.WriteLine("Would you like to insert another price?");
    input = Console.ReadLine();
    if(input.ToLower() != "y" ||  //if responce to the question is not 'y' or 'yes', break the loop
         input.ToLower() != "yes")
    break;
}while(true);

编辑

这个程序插入一个价格,直到用户确认他想插入一个新的价格,否则就退出。

您可以这样做:

static void Main(string[] args)
{
    string resp = "";
    string price, discount;
    decimal discountedPrice, savedAmount;
    do {
        .... // your previous code here
        ....
        Console.WriteLine("The discounted price of this item is: ${0}'nYou saved: ${1}", discountedPrice, savedAmount);
        Console.WriteLine("Another item?");
        string resp = Console.ReadLine().ToLower();
    }
    while (resp == "y" || resp == "yes");
    Console.ReadLine();
}