473,386 Members | 1,694 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

program in C# for a console app

4
Hi im writing a very "simple" program in C# for a console app..
I'm supposed to make a program that asks a user how many dice to roll, rolls a six sided die that many times, and returns the total and average roll.(this is all coming out of a book "C# programming for absolute beginners")
I cant figure how to get the value of the dice roll outside of the for statement please help.

Expand|Select|Wrap|Line Numbers
  1. {
  2.     class Program
  3.     {
  4.         static void Main(string[] args)
  5.         {
  6.             Random generator = new Random();
  7.  
  8.             int NumberOfRolls;
  9.  
  10.             Console.WriteLine("how many dice should i roll?");
  11.             NumberOfRolls = int.Parse(Console.ReadLine());
  12.  
  13.             for (int i = 0; i < NumberOfRolls; i++) 
  14.             {
  15.                 double mydouble = (int)(generator.NextDouble() * 6) + 1;
  16.                 int total = +(int)mydouble;
  17.                 double average = total / NumberOfRolls;
  18.                 Console.WriteLine(average);
  19.  
  20.             }
  21.  
  22.         }
  23.     }
  24. }
Jun 9 '07 #1
2 1912
chinu
36
Hi im writing a very "simple" program in C# for a console app..
I'm supposed to make a program that asks a user how many dice to roll, rolls a six sided die that many times, and returns the total and average roll.(this is all coming out of a book "C# programming for absolute beginners")
I cant figure how to get the value of the dice roll outside of the for statement please help.

Expand|Select|Wrap|Line Numbers
  1. {
  2.     class Program
  3.     {
  4.         static void Main(string[] args)
  5.         {
  6.             Random generator = new Random();
  7.  
  8.             int NumberOfRolls;
  9.  
  10.             Console.WriteLine("how many dice should i roll?");
  11.             NumberOfRolls = int.Parse(Console.ReadLine());
  12.  
  13.             for (int i = 0; i < NumberOfRolls; i++) 
  14.             {
  15.                 double mydouble = (int)(generator.NextDouble() * 6) + 1;
  16.                 int total = +(int)mydouble;
  17.                 double average = total / NumberOfRolls;
  18.                 Console.WriteLine(average);
  19.  
  20.             }
  21.  
  22.         }
  23.     }
  24. }
What do you mean by "value of the dice roll". Do you mean "average"?
Jun 11 '07 #2
Plater
7,872 Expert 4TB
When you decalare a varriable inside a set of { } brackets it is only available inside those (and nested) brackets. If you want the variable to exist outside the brackets, you must declare it outside the brackets
Expand|Select|Wrap|Line Numbers
  1. class Program
  2.     {
  3.         static void Main(string[] args)
  4.         {
  5.             Random generator = new Random();
  6.  
  7.             int NumberOfRolls;
  8.  
  9.             Console.WriteLine("how many dice should i roll?");
  10.             NumberOfRolls = int.Parse(Console.ReadLine());
  11.             int total=0;
  12.             for (int i = 0; i < NumberOfRolls; i++) 
  13.             {
  14.                 double mydouble = (int)(generator.NextDouble() * 6) + 1;
  15.                 total = +(int)mydouble;
  16.             }
  17.             double average = total / NumberOfRolls;
  18.             Console.WriteLine(average);
  19.         }
  20.     }
  21.  
Jun 11 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Will Stuyvesant | last post by:
I never used the popen or popen2 libraries but it is my understanding that they can capture the output of console based programs. Is it also possible to send keystrokes to console base programs? ...
1
by: Eric Whittaker | last post by:
hi all, im trying to write my first c++ program. a success, but i can't get the window to stay open after user enters input. it just automatically closes. right now the end of my program looks...
11
by: Brett | last post by:
Hi. I wrote a program in C that spends most of its time doing integer arithmetic (on a data set loaded at run time), with a negligible amount of I/O. I compiled it with lcc-win32 as a console...
5
by: Stephan Steiner | last post by:
Hi I'm trying to write a program that interacts with an interactive cli program, that is a program that does some processing but every now and then requires some user input. My problem is that...
9
by: tsung-yu | last post by:
Hello any expert, I has a vb.net program. For user requirement, I need to run and communicate an old DOS execution file. But, how to run and communicate a DOS program in a VB.NET program?...
3
by: Trev | last post by:
Hi, I'm learning C# and trying to write the program below, everything works fine until the last section: Console.Write("Would you like to play again y/n: "); again = Console.ReadLine(); if...
4
by: pms | last post by:
Need suggestions to improve this program. using System; namespace CalculateWage { class GetEmployeeDetails {
1
by: Tony Freixas | last post by:
Hello, I'm trying to create a wrapper for a program. I want to execute program 'X' by running program 'Y', such that 'Y' appears to function pretty much like 'X' both in the way command line...
4
by: Peter Nimmo | last post by:
Hi, I am writting a windows application that I want to be able to act as if it where a Console application in certain circumstances, such as error logging. Whilst I have nearly got it, it...
14
by: masaniparesh | last post by:
Hi Friends, The basic issue is "To read stdin and stdout in the same program" The following program is i have wrote using thread but i figured out that stdout is working but it is not working when...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.