本文介绍了需要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();
        }
    }
}

推荐答案

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#中的循环 [ ^ ]

您或您的侄子可能也想为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.


<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的帮助,请考虑一个简单的问题-侄子需要帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 13:00