需要C#2008的帮助,请考虑一个简单的问题-侄子需要帮助 [英] Need help for C# 2008 PLEASE simple question i think - Nephew needs help

查看:64
本文介绍了需要C#2008的帮助,请考虑一个简单的问题-侄子需要帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们需要帮助,我的侄子需要让该程序询问5个游戏的得分,一旦用户输入5个得分,它就应该给出5个游戏的平均得分....但是我们是某处缺少东西...我们只能让它得分1.以下是我们所拥有的...任何帮助都会...我一直在努力帮助他,但我不知道该怎么办....

Hi all we need help, my newphew needs to get the program to ask the scores of 5 games, once the user enters the 5 score, it suppose to give the average score of the 5 games.... BUT we are missing something somewhere... we can only get it to ask for 1 score. below is what we have... ANY HELP WOULD BE GREAT... I have been trying to help him but I have no idea what to do....

class Program
    {
        static void Main(string[] args)
        {

            // DECLARATIONS

            // variables
            int score = 0;              // integer to hold the current score inputted
            int game = 1;

            /* I know I have to do some kind of looping here for 5 games
             * but I'm not sure how to do that!
             */
            string numbers = null;
            for (int i = 1; i < 5; i++)
            {
                numbers += i;
                numbers += " ";
                if (i < 4)
                    continue;
                
            }

            // INPUT
            try
            {
                // Prompt for the score for the current bowler/game
                Console.Write("Please enter score for Game {0}: ", game);
                score = Convert.ToInt32(Console.ReadLine());

                /* Somehow I need to check to make sure the user entered a valid
                 * number for the score. It's impossible to score a negative
                 * amount or higher than 300... I need HELP!!!
                 */

                if (score >= 301)
                    Console.Write("Please enter a number under 300 for Game {0}: ", game);

                if (score <= -1)
                    Console.Write("Please enter a number apove 0 for Game {0}: ", game);

            }
            catch (Exception) // the user's input could not be converted to int
            {
                // invalid, try again
                Console.WriteLine("Error converting your input to a whole number. Please try again.");

            }


            // PROCESSING

            /* Once I get the looping right, I figure I'll be doing some calculations
             */



            // OUTPUT
            // Show the bowler score... it should be the average score though....
            Console.WriteLine("Average score for Bowler: {0}\n\n, score);

            // end of program
            Console.Write("\n\nPress any key...");

            Console.ReadKey();
        }
    }
}

推荐答案

是...侄子...

无论如何要循环播放五局游戏,您都需要将输入代码放入for循环中,我也不知道您对字符串numbers和语句
做些什么.
Yes... nephew...

Anyways to loop around for five games you need to put your input code inside the for loop, I''ve also got no idea what your doing with the string numbers and the statement
if (i < 4)
  continue;



实际上什么也没做.

因此要循环5次,您应该有一个for循环,如下所示:



Isn''t actually doing anything.

So to loop 5 times you should have a for loop like this:

for(int i=0; i<5; i++)
{
  //Get input from user here
}
//Calculate average score and whatnot after the loop


其中i从零开始(或者,如果需要,则从1开始,但是您需要检查i<6i<=5)

此外,可以使用函数bool Int32.TryParse(string str, out int val)代替使用try-catch块,如果成功则将返回true(并且结果将在val中),如果失败将返回false.

因此,要检查输入是否有效,您将需要另一种循环,do while循环将始终至少运行一次,然后继续处理直到最后的语句返回false.


where i starts at zero (or start at 1 if you want but then you need to check for i<6 or i<=5)

Also, instead of using a try-catch block you can use the function bool Int32.TryParse(string str, out int val) which will return true if it succeeds (and your result will be in val) and false if it fails.

So to check for valid input you''d need another kind of loop, the do while loop which will always run at least once and then keep going around until the statement at the end returns false.

bool validInput = false;
do
{
  int score = 0;
  validInput = Int32.TryParse(Console.ReadLine(), out score);
  
  //If the parse was successful (ie the user actually entered a number)
  // then you can check the actual value
  if(validInput)
  {
    if (score > 300)
    {
      Console.Writeline(...);
      validInput = false; //we need to try again
    }

    etc.
  }
}while(!validInput) //We will keep on looping until we have valid input



MSDN TryParse [ C#中的循环 [



MSDN TryParse[^]
Loops in C#[^]

Your or your nephew may also want to pick up a beginners book for C# too, as it will cover all of the basics as well as best practices and provide examples; it would also server as a handy reference.


他在大学里上初学者班,但是有问题...

我们要求5个不同的游戏,但无法显示平均得分.


he is in a beginners class at college, but have problems ...

we got it to ask for 5 different games, but can not get it to display the average score.


<pre lang="cs">/*  Lab02
 *  YOUR NAME HERE
 *  Lab 2 for PROG 1205
 *  Sep 2010
 *
 *  Description:
 *
 */



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

namespace Lab02
{
    class Program
    {
        static void Main(string[] args)
        {

            // DECLARATIONS

            // variables
            int score = 0;              // integer to hold the current score inputted
            int game = 1;
            int avgScore = 0;

            /* I know I have to do some kind of looping here for 5 games
             * but I''m not sure how to do that!
             */
            for (game = 1; game < 6; game++)
            {
                // INPUT
                try
                {
                    // Prompt for the score for the current bowler/game
                    Console.Write("Please enter score for Game {0}: ", game);
                    score = Convert.ToInt32(Console.ReadLine());
                    game--;

                    /* Somehow I need to check to make sure the user entered a valid
                     * number for the score. It''s impossible to score a negative
                     * amount or higher than 300... I need HELP!!!
                     */

                    if (score >= 301 || score <= -1)
                        Console.WriteLine("Score must be between 0 to 300. Please try agian.");
                    else
                        game++;
                }
                catch (Exception) // the user''s input could not be converted to int
                {
                    // invalid, try again
                    Console.WriteLine("Error converting your input to a whole number. Please try again.");
                    game--;
                }

            }
            // PROCESSING

            /* Once I get the looping right, I figure I''ll be doing some calculations
             */



            // OUTPUT
            // Show the bowler score... it should be the average score though....
            Console.WriteLine("Averge score for Bowler: {0}\n\n", avgScore);

            // end of program
            Console.Write("\n\nPress any key...");

            Console.ReadKey();
        }
    }
}



这篇关于需要C#2008的帮助,请考虑一个简单的问题-侄子需要帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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