简体   繁体   English

如果条件为真,如何在while循环中重复代码行c#

[英]How to repeat code line if a while loop if condition is true c#

I need help with an while loop for the following code:我需要有关以下代码的 while 循环的帮助:

using System;

namespace Sample
{
    class Program
    {
        static int Main(string[] args)
        {
            int num1;
            int num2;
            //This collects the price as input from the user
            Console.Write("Enter Price: ");
            num1 = Convert.ToInt32(Console.ReadLine());
            //This collects the amount paid as input from the user
            Console.Write("Paid: ");
            num2 = Convert.ToInt32(Console.ReadLine());
            decimal price = num2 - num1;
            Console.WriteLine("{0} - {1} = {2}", num2, num1, num2 - num1);
            while (num2 < num1)
            {
                Console.WriteLine("You have paid less than the price");
                Console.WriteLine("\nPlease pay an amount equals to/greater than "+num1);
                num2 = Convert.ToInt32(Console.ReadLine());
            }
            return num2 = Convert.ToInt32(Console.ReadLine());
        }
    }
}

I want the code to display the message AND ask the user again for the input 'Console.Write("Paid: ");'我希望代码显示消息并再次询问用户输入 'Console.Write("Paid:");' if the amount paid is less than the price.如果支付的金额低于价格。 The current code will only display the message but will not prompt user to enter the price again.当前代码只会显示消息,不会提示用户再次输入价格。 Can you please help with this你能帮忙解决这个问题吗

That's because you want the user to pay the price/above the price at once.那是因为您希望用户立即支付价格/高于价格。 Take a look at this code:看看这段代码:

static void Main(string[] args)
    {
        int price;
        int paid;
        //This collects the price as input from the user
        Console.Write("Enter Price: ");
        price = Convert.ToInt32(Console.ReadLine());
        //This collects the amount paid as input from the user
        Console.Write("Paid: ");
        paid = Convert.ToInt32(Console.ReadLine());
        decimal change = paid - price;
        Console.WriteLine("{0} - {1} = {2}", paid, price, change);
        while (paid < price)
        {
            Console.WriteLine("You have paid less than the price");
            Console.WriteLine("\nPlease pay an amount equals to/greater than " + price);
            paid += Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("You have paid:" +paid);
        }

        Console.WriteLine("You have paid enough, the price was {0} and you paid {1}", price, paid);
    }

I've changed the variable names to make it a bit more clear what's happening.我已经更改了变量名称,使其更清楚发生了什么。 With this code you add the new input to your last input, so eventually will the variable 'paid' be bigger than variable 'price'and thus end the program after the required amount is reached.使用此代码,您将新输入添加到上次输入中,因此最终变量 'paid' 将大于变量 'price',从而在达到所需金额后结束程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM