0

一开始,我想让用户在程序中输入“公式”这个词才能继续。如果输入任何其他内容,它将显示“名称错误。再试一次。” 它会要求用户再次输入名称。

最后,当程序询问用户是否要重试时,如果用户输入“是”,则程序将重新从头开始。如果用户输入“否”,那么它将退出程序。

这是代码。

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

namespace ConsoleApplication_program
{
    class formula
    {
        internal double numberOne, numberTwo, numberThree;
        internal double formulaResult()
        {
            return (6 * numberOne * numberTwo) + (6 * numberTwo * numberThree) + (6 * numberThree * numberOne);
        }
        internal double formulaResultTwo()
        {
            return formulaResult() * 0.9;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            formula integer = new formula();

            string name;

            Console.WriteLine("Enter name");
            name = Console.ReadLine();
            //Enter the word "Formula" to start calculating.
            //If anything else is entered then it will say "Wrong name. Try again."

            Console.WriteLine("Enter the first number");
            integer.numberOne = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("Enter the second number");
            integer.numberTwo = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("Enter the third number");
            integer.numberThree = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("The first result is {0}", integer.formulaResult());
            Console.WriteLine("The second result is {0}", integer.formulaResultTwo());

            Console.WriteLine("\nDo you want to try again?");
            //Enter "Yes" to try again or enter "No" to exit. 

            Console.ReadLine();
        }
    }
}
4

2 回答 2

1

你只需要使用一个循环。我会推荐一个 do ... while 循环。

do
{
    Console.WriteLine("Enter name");
    name = Console.ReadLine();
    //Rest of your code here.
    Console.WriteLine("\nDo you want to try again?");
}
while(!Console.ReadLine().Equals("No"));

只要输入的值不是“否”,循环就会重新开始,并要求用户再次输入他们的姓名。

于 2013-10-13T15:55:50.300 回答
0

我建议您查看while 循环if 语句

您可能还想查看break 语句以中断循环

于 2013-10-13T15:54:07.070 回答