在数字数组中查找一个或多个数字 [英] Finding one or more numbers in number array

查看:73
本文介绍了在数字数组中查找一个或多个数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我要在控制台中键入一个数字,然后在 numb中显示小于它的所有数字数组。



Hi
I''m going to type in a number in the Console and then display all the numbers that are less than it in the numb array.

int[] numb = { -9, -8, -7, -6, -5, -4, -3, -2, -1, 1, 2, 3, 4, 5, 6, 8, 9 };

            Console.Write("pick a number: ");
            int v = int.Parse(Console.ReadLine());
            int t = 0;
            for (int x = 0; x < numb.Length; x++)
            {
                if (v > numb[x])
                    t = numb[x];
            }
            Console.Write("Your num under is: " + t);
            
            Console.ReadLine();





此代码仅显示一个数字。如何更改它以显示小于输入值的数字?



This code only only displays one number. How do I change it to display the numbers less than the entered value?

推荐答案

这是你的作业,所以我不会给你任何代码解决方案。



但是......你说的是正确的。

所有你需要做的就是弄清楚发生了什么,以及你做了什么想要发生。



目前,你有一个循环遍历数组中的每个数字并检查它是否小于你输入的数字。如果是,则将其存储在名为t的变量中。循环结束后,打印t的值。问题是,如果你想要打印多个值(你确实如此),那么打印是否应该在循环之外?



顺便说一下,我不是''我知道你是否已经遇到它(我会假设你有)但如果你使用 foreach 这会更容易阅读(和写)循环而不是用于 - 因为你不需要知道你在数组中的位置,为什么要有一个变量来看你在哪里?

不更改除循环之外的任何代码(即根本不解决问题):

This is your homework, so I won''t give you any code solution.

But...you are on the right lines.
All you have to do is work out what is going on, and what you want to happen.

At the moment, you have a loop which runs through each of the numbers in your array and checks if it is less than you entered number. If it is, it stores it in a variable, called "t". After the loop has finished, you print the value of "t". The problem is that if you want to print more than one value (and you do) should the print be outside the loop?

By the way, I don''t know if you have met it yet (I''m going to assume you have) but this would be a lot easier to read (and write) if you used a foreach loop instead of a for - since you don''t need to know where you are in teh array, why have a variable to look at where you are?
Without changing any code except the loop (i.e. not fixing your problem at all):
int[] numb = { -9, -8, -7, -6, -5, -4, -3, -2, -1, 1, 2, 3, 4, 5, 6, 8, 9 };

Console.Write("pick a number: ");
int v = int.Parse(Console.ReadLine());
int t = 0;
foreach (int n in numb)
{
    if (v > n)
    {
        t = n;
    }
}
Console.Write("Your num under is: " + t);

Console.ReadLine();

看看它读起来有多容易?

我做的另一个改变是在if语句周围加上大括号。当你开始使用时,总是使用括号是个好主意,即使你不需要它们 - 它可以让你以后在必须做出改变时更轻松。相信我:我花了好几个小时试图找出为什么我的代码不起作用才发现如果我把括号插入其中就显得太明显......:笑:



那么,你能看到你要做什么吗?

See how much easier it is to read?
The other change I made is to add curly brackets around the if statement. While you are getting started, it''s a good idea to always use brackets, even if you don''t need them - it can make your life a lot easier later when you have to make changes. Trust me on this: I spent hours trying to work out why my code didn''t work only to find that if I''d put brackets in it would have been sooooo obvious...:laugh:

So, can you see what you have to do?


t 必须是一个数组。

t must be an array.
int[] t;



在循环中你需要调整 t 数组...



如何在C#中调整数组大小? [ ^ ]



;)


Inside a loop you need to resize t array...

How to resize array in C#?[^]

;)


这篇关于在数字数组中查找一个或多个数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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