Jump to content

am stuck and new at this - trying to help nephew

- - - - -

  • Please log in to reply
5 replies to this topic

#1
steveosh

steveosh

    Newbie

  • Members
  • Pip
  • 4 posts
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();
}
}
}

Edited by steveosh, 14 October 2010 - 04:23 PM.
it was jumbled


#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Your for loop stops too soon. The input should also be in the for-loop + As it currently is, it will loop 4 times, so either start i at 0 or make the condition i<=5
for (int i = 1; i < 5; i++) 

{

numbers += i; 

numbers += " "; 

if (i < 4) 

continue;

[COLOR="red"]}[/COLOR] //loop ends here, too soon

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); 

}
Even if the score is wrong, Nothing will happen really.

Console.Write("Please enter score for Game {0}: ", game); 

int temp = Convert.ToInt32(Console.ReadLine()); 


if (temp >= 301){ 

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

  i--;

} else if (temp <= -1){

  Console.Write("Please enter a number apove 0 for Game {0}: ", game); 

  i--;

} else {

  score += temp;

}

don't forget i--; so i lowers by 1 to make sure the loop runs an extra time to get 5 correct inputs.

#3
steveosh

steveosh

    Newbie

  • Members
  • Pip
  • 4 posts
we figured it out, only problem is trying to get it to display the average score of the 5 games.

we can enter 5 game score now, but it displays the score of game 5 as the average score.

#4
steveosh

steveosh

    Newbie

  • Members
  • Pip
  • 4 posts
here si what we got so far


/* 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();
}
}
}

#5
steveosh

steveosh

    Newbie

  • Members
  • Pip
  • 4 posts
it says that the loop that was used would store the 5 scores, but we have no idea what names would be used so in the formula (processing) part ....

#6
AIGuy

AIGuy

    Learning Programmer

  • Members
  • PipPipPip
  • 64 posts
hi steveosh,
I'm sorry but I didn't follow that last post. However if I understand your program idea, this should halp. Here is a short program I just wrote that I think is like what you are trying to make.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int scoreSum = 0;
            for (int gameNumber = 1; gameNumber <= 5; gameNumber++)
                //Run every thing in the brackets 5 times with gameNumber going up by 1 every time through
            {
                Console.Write("Please enter a score for game number "+gameNumber+":");
                string input = Console.ReadLine();
                int inputNumber = int.Parse(input); //A string is just a line of letters. You can't add or
                    //subratct them. This line of code make the string into a number you can work with.
                scoreSum += inputNumber;
            }
            Console.WriteLine("The average score of the five games is "+scoreSum/5);
            Console.WriteLine("press any key to end...");
            Console.Read();
        }
    }
}
Hope this helps.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users