我可以改变这段代码以允许用户输入 [英] Hw can I alter this code to allow user input

查看:78
本文介绍了我可以改变这段代码以允许用户输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码创建一个表格,晚餐价格为10到100美元,小费率为0.05到0.25。我必须改变它,以便用户可以输入晚餐价格和小费百分比。我完全迷失了。



 使用系统; 
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Text;
使用 System.Threading.Tasks;

命名空间 TippingTable.cs
{
class TippingTable
{
static void Main()
{
double dinnerPrice = 10 00 ;
double tipRate;
double tip;
const double LOWRATE = 0 10 ;
const double MAXRATE = 0 25 ;
const double TIPSTEP = 0 05 ;
const double MAXDINNER = 100 00 ;
const double DINNERSTEP = 10 00 ;

Console.Write( Price);
for (tipRate = LOWRATE; tipRate < = MAXRATE; tipRate + = TIPSTEP)
Console.Write( {0,8},tipRate.ToString( F));
Console.WriteLine();
Console.WriteLine( ----------------- -------------------------);

tipRate = LOWRATE;
while (dinnerPrice < = MAXDINNER)
{

Console.Write( {0,8},dinnerPrice.ToString( C));
while (tipRate < = MAXRATE)
{
tip = dinnerPrice * tipRate;
Console.Write( {0,8},tip.ToString( F));
tipRate + = 0 05 ;
}
dinnerPrice + = DINNERSTEP;
tipRate = LOWRATE;
Console.WriteLine();
}

dinnerPrice + = DINNERSTEP;
tipRate = LOWRATE;
Console.WriteLine();


}
}
}

解决方案

我建​​议您熟悉MSDN文档。它充满了有趣的项目,包括 https:// msdn.microsoft.com/en-us/library/system.double.tryparse%28v=vs.110%29.aspx [ ^ ]。



输入请求的位置在您使用这些值之前的某个位置。您可以通过 Charles Petzold的.NET Book Zero [ ^ ],它以非常清晰的细节解释了许多这些内容,并包含许多有用的源代码示例。


除了答案1:



是的,您可以使用方法 System.Console.Read ReadLine ReadKey 方法(https://msdn.microsoft.com/en-us/library/system.console%28v=vs.110%29.aspx [ ^ ]),但我建议你看看专业编写的仅限控制台的实用程序。你会发现很少使用交互式输入,它作为一个完全成熟的命令解释器,CMD.EXE只是其中之一。大多数实用程序或任何仅限控制台的应用程序从不使用交互式输入。这是有一个很好的理由:如果确实需要交互式输入,对于常规应用程序,图形UI更合适。因此,仅仅不需要控制台应用程序中的交互式输入,并且通常对用户来说是不方便的。您的应用程序是此类应用程序的典型示例。



相反,此类应用程序提供命令行中的所有输入。如果某些数据量对于命令行参数来说太大,则使用文件名参数,因此这部分详细信息在文件中提供。这对用户来说更方便,因为一个小错误不会破坏长时间的输入工作。命令行传递给入口点参数( Main 方法),或者可以从 System.Environment 获取:

https:// msdn .microsoft.com / zh-CN / library / system.environment.commandline%28v = vs.110%29.aspx [ ^ ],

https://msdn.microsoft.com/en-us/library/system。 environment.getcommandlineargs(v = vs.110).aspx [ ^ ]。



对于命令行解析,请参阅我的文章或其他CodeProject我在本文中推荐的工作:基于枚举的命令行实用程序 [ ^ ]。



-SA

This code creates a table with dinner prices form 10 to 100 dollars and tip rates from 0.05 to 0.25. I have to alter it so users can type in the price of dinner and percentage of tip. I'm totally lost.

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

namespace TippingTable.cs
{
    class TippingTable
    {
        static void Main()
        {
            double dinnerPrice = 10.00;
            double tipRate;
            double tip;
            const double LOWRATE = 0.10;
            const double MAXRATE = 0.25;
            const double TIPSTEP = 0.05;
            const double MAXDINNER = 100.00;
            const double DINNERSTEP = 10.00;

            Console.Write("   Price");
            for (tipRate = LOWRATE; tipRate <= MAXRATE; tipRate += TIPSTEP)
                Console.Write("{0, 8}", tipRate.ToString("F"));
            Console.WriteLine();
            Console.WriteLine("------------------------------------------");

            tipRate = LOWRATE;
            while (dinnerPrice <= MAXDINNER)
            {
               
                Console.Write("{0, 8}", dinnerPrice.ToString("C"));
                while (tipRate <= MAXRATE)
                {
                    tip = dinnerPrice * tipRate;
                    Console.Write("{0, 8}", tip.ToString("F"));
                    tipRate += 0.05;
                }
                dinnerPrice += DINNERSTEP;
                tipRate = LOWRATE;
                Console.WriteLine();
            }

            dinnerPrice += DINNERSTEP;
            tipRate = LOWRATE;
            Console.WriteLine();


        }
    }
}

解决方案

I would suggest you familiarise yourself with the MSDN documentation. It is full of interesting items, including https://msdn.microsoft.com/en-us/library/system.double.tryparse%28v=vs.110%29.aspx[^].

And the place to put your input requests is somewhere before the point at which you are using those values. You could work through .NET Book Zero by Charles Petzold[^], which explains many of these things in excellent clear detail, and includes lots of useful source code samples.


In addition to Answer 1:

Yes, you can use the methods System.Console.Read, ReadLine and ReadKey methods (https://msdn.microsoft.com/en-us/library/system.console%28v=vs.110%29.aspx[^]), but I would advise you to look at the professionally-written console-only utilities. You will find very few using interactive input, which work as a fully-fledged command interpreters, CMD.EXE is just one of them. The majority of the utilities, or any console-only applications, never use interactive input. This is done for a very good reason: if interactive input is really needed, for a "regular" application, a graphical UI is more suitable. So, interactive input in console-only application is just not needed and is generally inconvenient for a user. Your application is a typical example of such application.

Instead, such application provides all the input in a command line. If some volume of data is too big for a command-line parameter, the file name parameter is used, so this portion of detail is supplied in a file. This is much more convenient for a user, because one little mistake won't spoil long input work. The command line is passed to the entry point arguments (Main method) or can be obtained from System.Environment:
https://msdn.microsoft.com/en-us/library/system.environment.commandline%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.environment.getcommandlineargs(v=vs.110).aspx[^].

For command line parsing, please see my article or another CodeProject work I recommended in this article: Enumeration-based Command Line Utility[^].

—SA


这篇关于我可以改变这段代码以允许用户输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆