如何在单独的行中显示从用户获取的三个或三个以上的数字。 [英] How do I display three or more than three numbers taken from user in separate lines.

查看:131
本文介绍了如何在单独的行中显示从用户获取的三个或三个以上的数字。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因为我是Visual Studio的新手,所以我在C#中有点弱。请有人帮助我。

例如,如果用户输入三个整数,如

234

然后显示如下

2

3

4



我尝试了什么:



我试过这个

As I'm new to Visual studio so I'm little bit weak in C#.Please someone help me.
For Example if user input three integers like
234
It then display like
2
3
4

What I have tried:

I have tried this

static void Main(string[] args)
{
int a, b, c;
Console.WriteLine("Enter Three values ");
a = Convert.ToInt32(Console.Read());
b = Convert.ToInt32(Console.Read());
c = Convert.ToInt32(Console.Read());
Console.WriteLine(a)
Console.WriteLine(b);
Console.WriteLine(c);
Console.Read();
}

推荐答案

这并不像你想象的那么简单:你需要把它作为一个字符串读出来,把它拆分成三个单独的字符串,将它们转换为数字,然后打印它们。



假设您需要转换为整数 - 如果它是您作业的一部分,您可能会这样做 - 但是如果你不这样做则很简单:

That's not as simple as you think: you would need to read it as a string, split it into three separate strings, convert those to numbers, and then print them.

That's assuming you need to convert then to integers - you probably do if it's part of your homework - but if you don't then it's trivial:
string inp = Console.ReadLine();
foreach (char c in inp)
   {
   Console.WriteLine(c);
   }

由于字符串是一个 char 的数组,你可以使用字符串作为 foreach 中的来源,它将为您提供每个 char 一次价值。

但你的作业并不难:输入相同,你可以使用 foreach 以同样的方式分解输入。您所要做的就是将char转换为整数!

Since a string is an array of chars, you can use a string as the source in a foreach and it will give you each char value at a time.
But your homework isn't much harder: the input is the same, and you can use the foreach to "break up" the input in the same way. All you have to do is convert the char to an integer!


这篇关于如何在单独的行中显示从用户获取的三个或三个以上的数字。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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