请在这件事上给予我帮助! [英] Please help me with this!

查看:67
本文介绍了请在这件事上给予我帮助!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨救世主!所以我正在编写Codeasy.net上的一个问题,即使我认为我的代码是正确的,也有一个我无法解决的问题。任何人都可以帮我解决我的错误吗?非常感谢你提前。这是问题所在。



编写一个读取整数计数的程序,然后从控制台读取计数整数并输出它们的最小值。例如:

Hi savior! So i'm working on a question on Codeasy.net and there's a question I can't solve even though I thought my code is right. Can anyone help me out to spook out my error? Thank you so much in advance. Here's the question.

Write a program that reads integer count and then reads count integer numbers from the console and outputs their minimum. Example:

>3
>14
>57
>-10
-10





我尝试过:



这是我的解决方案。然而答案显然是错误的,当机器投入14,57和-10时总是出现14.为什么?





What I have tried:

Here's my solution. However the answer is wrong apparently and when the machine put in 14, 57 and -10 it always come out with 14. Why??

using System;

namespace ForLoops
{
    class Minimum
    {
        static void Main(string[] args)
        {
            int count = int.Parse(Console.ReadLine());
            int min = 999999999;
            int number= int.Parse(Console.ReadLine());

            for( int i=1; i<=count; i++)
            {
                    if (number<min)
                    min = number;

            }
            Console.WriteLine(min);
        }
    }

推荐答案

因为你只能输入一个数字。您键入一个数字以允许输入那么多值,但是您只允许输入单个值。



使用调试器来执行您的代码,一行一行,您将看到问题,显而易见。
Because you only ever get to input a single number. You type a number to allow for the entry of that many values, but then you're only allow to enter a single value.

Use the debugger to follow the execution of your code, line by line, and you'll see the problem, plainly obvious.


尝试:

Try:
for( int i=1; i<=count; i++)
{
    int number= int.Parse(Console.ReadLine());
    if (number<min)
        min = number;

}
Console.WriteLine(min);



您的代码行为不符合您的预期,您不明白为什么!



有一个几乎通用的解决方案:逐步在调试器上运行代码,检查变量。

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

调试器中没有魔法,它不知道你应该做什么,它没有发现错误,它只是通过向你展示你要发生什么来帮助你上。当代码没有达到预期的效果时,你就接近了一个错误。

要查看你的代码在做什么:只需设置断点并查看代码是否正常运行,调试器允许你执行第1行第1行并在执行时检查变量。



此解决方案的缺点:

- 这是一个DIY,你是跟踪问题并找到根源的那个,这导致了解决方案。

这个解决方案的优点:

- 它也是一个很好的学习工具,因为它告诉你现实,你可以看到哪种期望与现实相符。



次要效果

- 你会为自己找到虫子感到自豪。

- 你的学习技巧会提高。



你应该很快就会发现什么是错的。



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



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

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]

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


Your code do not behave the way you expect, and you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

The downside of this solution:
- It is a DIY, you are the one tracking the problem and finding its roots, which lead to the solution.
The upside of this solution:
- It is also a great learning tool because it show you reality and you can see which expectation match reality.

secondary effects
- Your will be proud of finding bugs yourself.
- Your learning skills will improve.

You should find pretty quickly what is wrong.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
The debugger is here to only show you what your code is doing and your task is to compare with what it should do.


这篇关于请在这件事上给予我帮助!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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