程序使用循环,获取用户输入的最小和最大数字 [英] Program using loops that gets minimum and largest numbers entered by the user

查看:91
本文介绍了程序使用循环,获取用户输入的最小和最大数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!我正在努力理解循环,我正在使用这本书。有一个问题,我似乎无法弄明白。

它要求一个带有循环的程序,让用户输入一系列数字。输入所有数字后,程序应显示输入的最大和最小数字。用户应输入-99表示系列结束。



我无法找出能给出系列最小值和最大值的主循环。



这就是我现在所拥有的:



编辑1:我更改了代码现在我可以让用户输入数字直到他们输入-99。现在我只需要一个循环,它将给出系列的最大值和最小值。

希望这更容易阅读!



编辑2:我再次更改了代码。我想出了很多东西(感谢所有的建议!)并且现在就这样做了。代码正在运行,但是对于答案我出于某种原因只得到0,而不是系列中的任何数字。有没有理由发生这种情况?我认为它与参数有关,但我不确定。



Hi! I''m trying to understand loops and I''m using this book. There''s a question in it that I can''t seem to figure out.
It''s asking for a program with a loop that lets the user enter a series of numbers. After all the numbers have been entered, the program should display the largest and the smallest number entered. The user should enter -99 to signal the end of the series.

I can''t figure out the main loop that will give me the minimum and the maximum value of the series.

Here''s what I have right now:

EDIT 1: I changed the code and now I can get the user to enter numbers till they enter -99. Now I just need the loop that will give me the max and min values of the series.
Hope this is easier to read!

EDIT 2: I changed the code again. I figured out pretty much everything (thanks to all the suggestions!) and have this now. The code is running, but for the answer I get only 0 for some reason, not any number in the series. Is there a reason that this is happening? I think it has something to do with the parameters but I''m not sure.

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

namespace Project_1
	{
	class Program
		{
		static void Main(string[] args)
			{//start main
			
			int number = 0;
			int min = 0;
			int max = 0;
			
			//start calling modules
			calculateMinMax(ref min, ref max, ref number);
			getInput(ref number);
		  display (min, max, number);
			//end calling modules
			 
			
			Console.ReadKey(); //keep program running
			}//end main	
				
	
	//start CalculateMinMax
			static void calculateMinMax ( ref int min, ref int max, ref int number)
			{
			if (min == 0 && max == 0)
				{
				min = number;
				max = number;
				}

			if (number < min)
				{
				min = number;
				}

			if (number > max)
				{
				max = number;
				}

			}//end calculateMinMax

	//start getInput
			static void getInput(ref int number)
				{
				while (number != -99)
					{
					Console.WriteLine("Please enter the numbers in the series. Enter '-99' to end the series.");
					while (!int.TryParse(Console.ReadLine(), out number))
						Console.WriteLine("Error. Please enter a number.");
					}
				}//end getInput
			
			
	//start display
			static void display (int min, int max, int number)
			{
			Console.WriteLine("The largest number in the series you entered is {0}, and the smallest number in the series you entered is {1}.", max, min);
			}//end display

	

			}//end class
		}//end program







任何帮助将不胜感激!谢谢! :)




Any help will be appreciated! Thanks! :)

推荐答案

到目前为止,你离任何工作代码还很远。



首先,您的代码从用户获取字符串并解析为整数显示控制台中的数据并将其丢失。为什么添加使用System.Collections.Generic; ?创建一个 System.Collections.Generic.List< int> 的实例并在那里添加您的数据,这样您就可以在哪里找到最大值和最小值。



ref 参数毫无意义。阅读 ref out 参数,按值和参考传递参数,并了解每种情况的工作原理。 />


现在,有点棘手的部分。您将最大值和最小值初始化为0毫无意义。相反,将minimum最小化为 int.MaximumValue ,maximum - 到 int.MinimumValue 并在循环中更新其值。 br />


现在,这几乎是你所需要的。使用这些想法重写代码。



-SA
You are pretty far from any working code so far.

First of all, your code getting strings from the user and parsing as integer shows data in the console and looses it. Why did you added uses System.Collections.Generic;? Create an instance of System.Collections.Generic.List<int> and add your data there, so you could have where to find your maximum and minimum.

You ref parameters make no sense. Read about ref and out parameters, passing parameters by value and by reference, and learn how each case works.

Now, a bit more "tricky" part. Your initialization of maximum and minimum to 0 makes no sense at all. Instead, initialize minimum to int.MaximumValue, maximum — to int.MinimumValue and update its value in the loop.

Now, this is pretty much all you need. Rewrite the code using these ideas.

—SA


首先将数字存储在一个数组中。



然后将第一个数字设置为最大值和最小值。



然后使用任何循环技术循环遍历数组,如果数组中的数字大于最大值,则将其指定为最大值,如果小于最小值,则将其指定为最小值。



尝试它出来了。



:)
First Store the Numbers in an array.

Then set the first number as Maximum and as minimum.

Then use any looping technique to loop through the array and if the number in the array is greater than Maximum, assign it to maximum and if it is less than minimum then assign it to minimum.

Try it out.

:)


这篇关于程序使用循环,获取用户输入的最小和最大数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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