Jump to content

Quick help with a program

- - - - -

  • Please log in to reply
1 reply to this topic

#1
vernacular26

vernacular26

    Newbie

  • Members
  • Pip
  • 2 posts
Hello,

I'm new to C# and I'm working on a guessing game program. I let the user decide how many games they want to play and after the games are over I show them a report which include their average number of guesses per game.
The main problem is that I want the average number of guesses to be displayed as a decimal with 2 decimal places, but it always displays a whole number.
I have tried several things that I know of and I've looked online.

In the code, I have a loop that lets the user guess a number and displays "too high", "too low" or "hit, you win". The user is only allowed 6 guesses. Every time the user makes a guess, the variable "g" is incremented, until g=6. After all of the games have been played, g = "the sum of the guesses from all of the games". Then I divide by the number of games played to get the average number of guesses per game.

I just want to be able to display the average number of guesses (avgGuess) as a decimal with 2 decimal places.

Here's my code:


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

namespace LA3_1
{
class Program
{
static void Main(string[] args)
{
string result = " ";
Random num1 = new Random();
int target = num1.Next(1, 21);

int userGuess;
int upperBound = 20;
int lowerBound = 1;
int g = 0;
//int avgGuess;


int numberOfGames = getNumberOfGames();
Console.WriteLine(target);
for (int i = 1; i < (numberOfGames + 1); i++)
{
Console.WriteLine("Game {0}", i);
for (int j = 1; j < 7; j++)
{
userGuess = getUserGuess( lowerBound, upperBound);


if (userGuess == target)
{
Console.Write("Hit! You Win!You guessed the number in {0} tries! ", j);
j = 7;
g++;
} // if
else if (userGuess < target)
{
Console.Write("Too low! ");
lowerBound = userGuess + 1;
if (j == 6)
Console.WriteLine("\nYou Lose! MACHINE RULE! The number was {0}", target);
g++;
} // if (userGuess < target)
else
{
Console.Write("Too high! ");
upperBound = userGuess - 1;
if (j == 6)
Console.WriteLine("\nYou Lose! MACHINE RULE! The number was {0}", target);
g++;
} // else (userGuess > target)


} // for loop to repeat 6 tries at answering

target = num1.Next(1, 21); // get a new target
upperBound = 20;
lowerBound = 1;

} // for looping the number of games

double avgGuess;
avgGuess = g / numberOfGames;

Console.Write(result);
Console.WriteLine(g);
Console.WriteLine(numberOfGames);
Console.WriteLine(avgGuess);
Console.WriteLine("Games Played: {0}", numberOfGames);
Console.WriteLine("Average Guesses per game: {0:d2} guesses.", avgGuess);
Console.WriteLine("W/L record: {0}-{1}");
//Console.WriteLine(getResult());

Console.ReadLine(); // hold screen

}

// method to ask user and return number of games they want to
// play
public static int getNumberOfGames()
{
int gameNum;
string inValue;

Console.Write("How many games would you like to play? ");
inValue = Console.ReadLine();
gameNum = int.Parse(inValue);

return gameNum;

// X MORE NEEDED HERE

} // getNumberOfGames

public static int getUserGuess(int lBound, int uBound)
{
int guess;
string inValue;

Console.Write("Guess a number between " + lBound
+ " and " + uBound + ": " );
inValue = Console.ReadLine();
guess = Convert.ToInt32(inValue);
return guess;
} // getUserGuess

/*public static string getResult()
{

}*/
}
}



Any help is much appreciated.

#2
Momerath

Momerath

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 242 posts
Both g and numberOfGames are integers so when it reaches the line avgGuess = g / numberOfGames; it does an integer division, then converts the value to a double for storage in avgGuess. You need to make it do a floating point divide: avgGuess = ((double)g) / numberOfGames;

If you want 2 decimal places displayed, you need to tell it that: Console.WriteLine("{0:0.00}", avgGuess);

Also, your line Console.WriteLine("W/L record: {0}-{1}"); needs two parameters.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users