帮助C#作业,无法弄明白 [英] Help with C# homework, cant figure it out

查看:70
本文介绍了帮助C#作业,无法弄明白的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要帮助解决这个问题,在Visual C#如何由Paul和Harvey Deitel编程。



(查找最大数字)找到最大值的过程值经常用于计算机应用程序中。例如,确定销售竞赛获胜者的应用程序将输入每个销售人员销售的单位数量。销售最多单位的销售人员赢得了比赛。写入伪代码,然后输入一系列10个整数的C#应用​​程序,然后确定并显示最大的整数。你的应用程序应该至少使用以下三个变量:counter,number和largest。



这是我到目前为止所获得的

< pre lang =c#> int highestnumber;
int number;
int counter;


Console.WriteLine( 输入销售数量:);
number = Convert.ToInt32(Console.ReadLine());
highestnumber = number;
counter = 1 ;
while (counter < 10
{
counter = counter + 1 ;

if (number > highestnumber)
number = highestnumber ;

Console.ReadLine();

}

Console.WriteLine( 最高数量销售额为:{0},数字);
Console.ReadLine();



我的老师已经展示了如何做循环的东西,但是我无法理解的最多的东西。每次执行最高销售数量是......时,它会输出我输入的第一个值。

解决方案

首先我认为您选择使用循环错误。我会使用进行循环,因为您事先知道需要多少次迭代。如果迭代次数取决于用户输入,我会使用循环 - 例如,如果提示类型X停止输入



最重要的是你只捕获第一个数字的输入...你有一个

 Console.ReadLine(); 

但你不是将数字分配给任何东西。 number仅在第一个输入上设置

 number = Convert.ToInt32(Console.ReadLine()); 

需要在每次迭代时在循环内捕获。< br $> b $ b

我真的很讨厌 Convert.ToInt32 ,特别是如果它与用户输入一起使用的话。 int.TryParse (和类似的函数)要好得多(抛出异常的几率较小)



For例如:



 静态  void  Main( string  [] args)
{
int highestnumber = 0 ;

const int NoOfSalesToCompare = 10 ;

for int counter = 1 ; counter < = NoOfSalesToCompare; counter ++)
{
int 数字;
Console.WriteLine( 输入销售人员{0}的销售数量:,counter );
if int .TryParse(Console.ReadLine(), out 数字))
如果(数字> 最高数字)最高数字=数字;
}

Console.WriteLine( 最高销售数量为:{ 0},最高数字);
Console.ReadLine();

}





最后,如果你学会使用调试器,你可能已经发现了这个问题。单步执行不起作用的代码行是提高知识水平的绝佳方法。实际上,逐步执行你在某处找到工作的代码行也是提高知识水平的好方法。



我强烈建议你迈一步通过我给你的代码并彻底理解它为什么有用,但你的代码没有。别忘了......你的老师也可能会使用这个网站。


你的代码中的问题是:

  if (number >  highestnumber)
number = highestnumber;





这一点。



你需要写这个:



 如果(数字> 最高数字)
最高数字=数字;





翻译成英文:



如果我现在处理的当前数字大于highestNumber,那么将higestNumber设置为此当前数字。



示例:

第一循环:

数= 3

最高 - 0;

结果:最高为3



第二循环:

数= 1

最高= 3

结果:最高= 3



thisrd loop

number = 5

最高= 3

结果:最高= 5



等.....


您的代码中有一些错误,请参阅注释更正

  int 最高数字; 
int number;
int counter;

Console.WriteLine( 输入销售数量:);
number = Convert.ToInt32(Console.ReadLine());
highestnumber = number;
counter = 1 ;
while (counter< 10
{
counter = counter + 1 ;

if (number> highestnumber)
// < span class =code-comment> number = highestnumber;
// 你做到了错误的方式
highestnumber = number;

// Console.ReadLine();
// 您忘记将新读数存储在变量中
number = Convert.ToInt32(控制台。的ReadLine());

}

// Console.WriteLine(最高销售数量为:{0},数量);
// 您寻找的最高编号
Console.WriteLine( 最高销售数为:{0} ,最高数字);
Console.ReadLine();





要了解您的代码是什么,我建议使用调试器,它允许您可以看到代码逐步执行,并在代码执行时检查变量。这是非常有教育意义的。


I need help with this problem, in Visual C# How to Program by Paul and Harvey Deitel.

(Find the largest number) The process of finding the maximum value is used frequently in computer applications. For example, an app that determines the winner of a sales contest would input the number of units sold by each salesperson. The salesperson who sells the most units wins the contest. Write pseudocode, then a C# app that inputs a series of 10 integers, then determines and displays the largest integer. Your app should use at least the following three variables: counter, number and largest.

Here's what I've got so far

int highestnumber;
int number;
int counter;


Console.WriteLine("Enter number of sales: ");
number = Convert.ToInt32(Console.ReadLine());
highestnumber = number;
counter = 1;
while (counter < 10)
{
    counter = counter + 1;

    if (number > highestnumber)
        number = highestnumber;

    Console.ReadLine();

}

Console.WriteLine("The highest number of sales is: {0}", number);
Console.ReadLine();


My teacher has shown how to do the loop stuff, but the largest number thing I cannot understand. Every time it executes the "highest number of sales is..." it outputs the first value that I entered.

解决方案

Firstly I think your choice of using a while loop is wrong. I would use a for loop because you know up front how many iterations you need. I would use a while loop if the number of iterations depended on the user input - for example if you prompted "Type X to stop the input"

Most importantly you are only capturing the input of the first number ... you have a

Console.ReadLine();

but you are not assigning the number to anything. number is only ever set on the first input

number = Convert.ToInt32(Console.ReadLine());

it needs to be captured within the loop on each iteration.

I really hate Convert.ToInt32, especially if it is used with user-input. int.TryParse (and similar functions) is far better (less chance to have an exception thrown)

For example:

static void Main(string[] args)
{
    int highestnumber = 0;

    const int NoOfSalesToCompare = 10;

    for (int counter = 1; counter <= NoOfSalesToCompare; counter++)
    {
        int number;
        Console.WriteLine("Enter number of sales for salesperson {0}:", counter);
        if(int.TryParse(Console.ReadLine(), out number))
            if(number > highestnumber) highestnumber = number;
    }

    Console.WriteLine("The highest number of sales is: {0}", highestnumber);
    Console.ReadLine();

}



Lastly, if you learn to use the debugger you would have probably spotted this for yourself. Stepping through lines of code that don't work is an excellent way of improving your knowledge. Actually, stepping through lines of code that *do* work that you've found somewhere is also a good way of improving your knowledge.

I strongly recommend that you step through the code that I've given you and thoroughly understand why it works, but yours didn't. Don't forget ... your teacher probably uses this site too.


Well the problem in your code is:

if (number > highestnumber)
       number = highestnumber;



this bit.

You need to write this:

if (number > highestnumber)
       highestnumber= number;



translated into english:

If current number that I'm processing is bigger than highestNumber until now than set higestNumber to this currentnumber.

Example:
First loop:
number = 3
hisghest - 0;
result: highest is 3

Second loop:
number = 1
highest = 3
result: highest = 3

thisrd loop
number = 5
highest = 3
result: highest = 5

etc.....


There is a few bugs in your code, see comments for corrections

int highestnumber;
int number;
int counter;

Console.WriteLine("Enter number of sales: ");
number = Convert.ToInt32(Console.ReadLine());
highestnumber = number;
counter = 1;
while (counter < 10)
{
    counter = counter + 1;
 
    if (number > highestnumber)
        //  number = highestnumber;
        // you did it the wrong way
        highestnumber = number;
 
    //  Console.ReadLine();
    // you forgot to store the new reading in the variable
    number = Convert.ToInt32(Console.ReadLine());
 
}
 
//  Console.WriteLine("The highest number of sales is: {0}", number);
// you look for highest number
Console.WriteLine("The highest number of sales is: {0}", highestnumber);
Console.ReadLine();



To learn what is doing your code, I recommend to use the debugger, it permit you to see your code executing step by step and inspect variables as your code execute. It is very educative.


这篇关于帮助C#作业,无法弄明白的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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