从用户控制台应用程序C#获取输入 [英] Take input from user console application C#

查看:66
本文介绍了从用户控制台应用程序C#获取输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试当我在控制台上输入10个随机数字时,将显示消息

这些数字小于10



e.. $



如果我输入这些随机数

10,9,12,45,120,2,3,1,9, 14

然后

消息显示

9,2,3,1,9小于10



i试试这个



我尝试过:



I am trying that when i input 10 random numbers on console then message will be display
these numbers are less than 10

e..g

if i input these random numbers
10,9,12,45,120,2,3,1,9,14
then
message display
9,2,3,1,9 are less than 10

i try this

What I have tried:

public void p1()
{
   try
   {
       Console.WriteLine("Enter numbers");

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

       if (num < 10)
       {
           Console.WriteLine("less num",num);
           Console.ReadLine();

       }
       else
       {
           Console.WriteLine("Not less than num", num);
           Console.ReadLine();
       }
   }
   catch(Exception ex)
   {
       throw new Exception();
   }


}





但这个工作就像那样当我只输入1例如如果我输入9号码然后消息显示更少的号码,当我输入12然后消息显示不小于数量



我想要的地方

i输入10个数字并从该程序中识别哪些数字小于10



我是怎么做的



检查这个GIF

http://i.imgur.com/xmfiApI.gifv [< a href =http://i.imgur.com/xmfiapi.gifvtarget =_ blanktitle =New Window> ^ ]

推荐答案

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



这一行只会转换一个数字,因为这是你要求它做的。您应该通过以下方式处理多个数字:


This line will only convert a single number, since that is what you have asked it to do. You should deal with multiple numbers by something like:

string[] numbers = Console.ReadLine().split(" ");
foreach (string num in numbers)
{
    int num = Convert.ToInt32(num);
    // test for less or not
}



注意最好使用 TryParse 而不是转换以允许这种情况当用户键入无效字符时。


Note it is better to use TryParse rather than Convert to allow for the case when the user types invalid characters.


引用:

但是当我只输入1时这就像例如如果我输入9号码然后消息显示更少的号码,当我输入12然后消息显示不小于数量



我想要的地方

i输入10个数字并从该程序中识别哪些数字小于10

but this work like that when i enter only 1 e.g. if i enter 9 number then message display less number and when i enter 12 then message display Not less than num

where as i want
i input 10 numbers and from that program identifies which numbers are less than 10

您的代码完全按照您的要求执行。

您怎么可能不理解代码在做什么?这是你的代码吗?



你应该学会尽快使用调试器。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。



调试器允许你跟踪执行逐行检查变量,你会看到它有一个停止做你期望的点。

调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - A初学者指南 [ ^ ]



调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。

Your code is doing exactly what you requested.
How is it possible that you don't understand what the code is doing ? Is it your code ?

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.


我会解决用这种方式编辑:



I would have solved it this way:

namespace check_Numbers
{
    class Program
    {
        static void Main(string[] args)
        {    
            string[] seperators = {",", " ", ";", "."};
            int num2;
            Console.WriteLine("Input numbers over 10:");

            string[] splitArray = Console.ReadLine().Split(seperators, StringSplitOptions.RemoveEmptyEntries);
            foreach (string num in splitArray)
            {

            If(Int32.TryParse(num, out num2) == true)
            {
                if (num2 < 10)
                {
                    Console.WriteLine(num + " is smaller than 10");
                }
            }
            else
            {
                Console.WriteLine(num + " is not a f***ing number...");
            }
            }
        }
    }
}


这篇关于从用户控制台应用程序C#获取输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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