我该如何解决这个练习? [英] How do I solve this exercise?

查看:74
本文介绍了我该如何解决这个练习?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编写一个程序,要求用户一次输入一系列非零整数。

程序应该一直询问用户输入一个数字,直到输入值为0 。

程序应显示已输入的最大数字。

请注意,用户可以输入所有负整数,并且您的程序必须是

能够处理这个。



我尝试过:



好​​的,所以这就是我最终做的事情,而且完全没问题。我只是需要睡一觉,当我再次尝试时它很顺利。我知道怎么做只是不知道如何把它放入代码中。感谢您发布的解决方案,但我没有看到您发布的代码,所以我确定我不应该使用它。干杯!













Write a program that asks the user to enter a series of non-zero integers, one at a time.
The program should keep asking the user for a number until they enter a value of 0.
The program should then display the largest of the numbers that have been entered.
Be aware that the user could enter all negative integers and your program has to be
able to handle this.

What I have tried:

Ok, so this is what i ended up doing and it's completly fine. I just needed to get some sleep, when I tried again it went pretty smoothly. I knew how to do it just didn't know how to put it into code. Thanks for the solution posted but I haven't seen the code you posted so I'm sure I shouldn't be using it. Cheers!






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

namespace bigestNumber
{
    class ProgramNumber
    {
        static void Main(string[] args)
        {
            int num2 ;  //declaring the required variables.
            int numberEntered;
            string inputNumber, inputNumber1;
            
            
            Console.Write("Please enter a series of non-zero integer numbers, once at a time. Enter 0 to stop: ");

            inputNumber = Console.ReadLine();  //waiting for the user imput
            numberEntered = int.Parse(inputNumber);
            do
            {

                Console.Write("Please enter another number. Enter 0 to stop: ");
                inputNumber1 = Console.ReadLine();
                num2 = int.Parse(inputNumber1);
                if (numberEntered < num2 && num2 != 0) //if statment
                {
                    numberEntered = num2; //assigns the value of num2 to the numberEntered variable
                }

            }
            while ( num2 != 0);  //loop statment

            Console.Write("The largest of the numbers you have entered is: ");
            Console.WriteLine(numberEntered); //displays the value of the variable num

            Console.ReadKey();  //waits for the user to press a key
        }
    }
}

推荐答案

这是作业,所以我们不会给你任何代码。

但是要说实话,这很简单,诚实!



将其分解为较小的赌注并让那些先行动。



所以从最重要的开始:

This is homework, so we'll give you no code.
But to be honest, this is pretty simple, honest!

Break it down into smaller bets and get those working first.

So start with the most important:
Ask the user for a number, and read it in.

你知道怎么做那样做吧?它只是一个Console.WriteLine,后跟一个Console.ReadLine和一个整数的转换(你知道怎么做,我敢肯定)



测试那:打印你输入的号码!当它读取数字时,(并抱怨非数字输入而不是崩溃)继续下一位:



You know how to do that, right? It's just a Console.WriteLine, followed by a Console.ReadLine and a conversion to an integer (And you know how to do that, I'm sure)

Test that: print the number you entered! When it reads numbers fine, (and complains about non-numeric inputs instead of crashing) move on to the next bit:

Continue to read in numbers until he enters zero

你也知道如何做到这一点:一个简单的循环,检查他读取的数字以及它是否为零退出。

测试它。再试一次。



下一位:

You know how to do that as well: a simple loop that checks the number he read and if it's zero exits.
Test it. Test it again.

Next bit:

Display the largest

略有更复杂,但不是很多。添加最大到目前为止变量,在您进入循环之前将其设置为可能的最小值 - 我甚至会为您提供代码:

Slightly more complicated, but not a lot. Add a "max so far" variable and before you enter the loop set it to the smallest possible value - I'll even give you the code for that:

int maxSoFar = int.MinValue;

将其设置为可能的最小值意味着无论用户输入什么都会更大!

在循环中,检查用户输入的值与该值的对比。如果新值更大,请将maxSoFar设置为新值。

循环后,打印maxSoFar,因为它是输入的最大值!



测试一下。用不同的数据测试它。而且,还有不同的数据。

当你开心时,一切都完成了!

setting it to the smallest possible value means that whatever the user enters will be bigger!
Inside the loop, check what the user entered against that value. If the new value is bigger, set maxSoFar to the new value.
After the loop, print maxSoFar because it is the biggest entered value!

Test it. Test it with different data. And again, with yet different data.
When you are happy, all done!


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

namespace bigestNumber
{
    class ProgramNumber
    {
        static void Main(string[] args)
        {
            int num2 ;  //declaring the required variables.
            int numberEntered;
            string inputNumber, inputNumber1;
            
            
            Console.Write("Please enter a series of non-zero integer numbers, once at a time. Enter 0 to stop: ");

            inputNumber = Console.ReadLine();  //waiting for the user imput
            numberEntered = int.Parse(inputNumber);
            do
            {

                Console.Write("Please enter another number. Enter 0 to stop: ");
                inputNumber1 = Console.ReadLine();
                num2 = int.Parse(inputNumber1);
                if (numberEntered < num2 && num2 != 0) //if statment
                {
                    numberEntered = num2; //assigns the value of num2 to the numberEntered variable
                }

            }
            while ( num2 != 0);  //loop statment

            Console.Write("The largest of the numbers you have entered is: ");
            Console.WriteLine(numberEntered); //displays the value of the variable num

            Console.ReadKey();  //waits for the user to press a key
        }
    }
}


这篇关于我该如何解决这个练习?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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