Random number and generating an output to command prompt hel
Page 1 of 1
Rayne01




Posts: 39

PostPosted: Tue, 5th Nov 2013 20:05    Post subject: Random number and generating an output to command prompt hel
I have this code:

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

namespace Switch_Test
{
class Program
{
static void singleDigit(ref int firstRandom, ref int secondRandom)
{
Random rnd = new Random();
firstRandom = rnd.Next(0, 9);
secondRandom = rnd.Next(0, 9);
}
static void Main(string[] args)
{
int firstRandom = 0;
int secondRandom = 0;

Console.Write("{0} + {1} = : ", singleDigit(ref firstRandom, ref secondRandom));
}
}
}

I know its wrong, I just basically want to know how to output the random numbers to screen, I'm using Visual Studios 2010.

Thanks.
Back to top
LeoNatan
Banned



Posts: 73193
Location: Ramat Gan, Israel 🇮🇱
PostPosted: Tue, 5th Nov 2013 21:25    Post subject:
Console.Write("{0} + {1} = : ", singleDigit(ref firstRandom, ref secondRandom));

is incorrect because singleDigit does not return a value.

You can either change singleDigit to return firstRandom+secondRandom, or, preferably:

nt firstRandom = 0;
int secondRandom = 0;

singleDigit(ref firstRandom, ref secondRandom);

Console.Write("{0} + {1} = : ", firstRandom, secondRandom);
Back to top
Rayne01




Posts: 39

PostPosted: Tue, 5th Nov 2013 22:11    Post subject:
I will give the preferred method a try, thank you for your quick response, I love to program, however my lecturer hasn't run me through the code to output it to command prompt properly.

Thank you so much.

I'll test it now.

EDIT: The preferred method worked, you have no idea how grateful I am, I've been trying to figure that out for 30 minutes straight.

EDIT 2: how would the code differ if it was done via the first method if you don't mind me asking?


Last edited by Rayne01 on Tue, 5th Nov 2013 22:23; edited 1 time in total
Back to top
LeoNatan
Banned



Posts: 73193
Location: Ramat Gan, Israel 🇮🇱
PostPosted: Tue, 5th Nov 2013 22:21    Post subject:
What happens in this example is there is a method, singleDigit, which receives two integers by reference, and sets them to two random numbers. The method itself does not return a value. When you attempt to print, you have a formatted strings with {0}, {1}, which tell the Write method to print the first, then the second parameters following the format string. Remember that 0 means first, 1 means second, etc.

Edit: Cheers!
Back to top
Rayne01




Posts: 39

PostPosted: Wed, 6th Nov 2013 00:09    Post subject:
Ok, so I have all my random generators talking to the modules I need, however I'd like to ask, I know it'll probably involve an if statement which is absolutely fine, But I would like to ask you what the code would look like to validate the answer of the equation.

Thank you.
Back to top
LeoNatan
Banned



Posts: 73193
Location: Ramat Gan, Israel 🇮🇱
PostPosted: Wed, 6th Nov 2013 00:20    Post subject:
For the first method,

static int singleDigit(ref int firstRandom, ref int secondRandom)
{
Random rnd = new Random();
firstRandom = rnd.Next(0, 9);
secondRandom = rnd.Next(0, 9);

return firstRandom + secondRandom;
}

I don't understand what "validate" means in relation to the code shown. Please elaborate.
Back to top
Rayne01




Posts: 39

PostPosted: Wed, 6th Nov 2013 00:39    Post subject:
By validate, I mean, if the user answers correctly or incorrectly, the code that basically states:

if (answer == correct)
{
Console.Write("Correct!");
}

if (answer == incorrect)
{
Console.Write("Incorrect! Please try again");
}

Basically I need to be able to implement code that will check if the answer is right to the randomly generated numbers then if correct, i can use an if statement to tell them its correct or incorrect.

Thanks for any light you can shed on this Leon.
Back to top
Rayne01




Posts: 39

PostPosted: Wed, 6th Nov 2013 01:31    Post subject:
int input;
int sum;

singleDigit(ref firstRandom, ref secondRandom);

Console.Write("{0} / {1} = : ", firstRandom, secondRandom);
input = Convert.ToInt16(Console.ReadLine());

sum = firstRandom / secondRandom;

if (firstRandom / secondRandom == sum)
{
Console.Write("\nCorrect\n");
}
if (firstRandom / secondRandom < sum)
{
Console.Write("Incorrect");
}

That's what I have, but doesn't work unforunately, if only it was that simple.
Back to top
LeoNatan
Banned



Posts: 73193
Location: Ramat Gan, Israel 🇮🇱
PostPosted: Wed, 6th Nov 2013 18:17    Post subject:
But you take the input from the user. Why are not comparing sum to input in the if statement?

Also, sum = firstRandom / secondRandom; seems like it doesn't calculate sum. Very Happy You need sum = firstRandom + secondRandom;
Back to top
speedgear




Posts: 2697
Location: portugal
PostPosted: Wed, 6th Nov 2013 18:46    Post subject:


Sin317 wrote:
while you can't "turn gay", you can cut off your balls. believe me, you'll never think of women again.


zmed wrote:
Or just a defense mechanism. If you fart, you'll most definitely smell it so your brain tells you it ain't bad as strangerfarts.
Back to top
Rayne01




Posts: 39

PostPosted: Thu, 7th Nov 2013 00:28    Post subject:
LeoNatan wrote:
But you take the input from the user. Why are not comparing sum to input in the if statement?

Also, sum = firstRandom / secondRandom; seems like it doesn't calculate sum. Very Happy You need sum = firstRandom + secondRandom;


But surely if it was sum = firstrandom + secondRandom; will just add those two together, I need sum to store the answer to firstRandom divided by secondRandom.
Back to top
BearishSun




Posts: 4484

PostPosted: Thu, 7th Nov 2013 08:23    Post subject:
"Sum - An amount obtained as a result of adding numbers.". That's the confusing bit.
Back to top
Rayne01




Posts: 39

PostPosted: Thu, 7th Nov 2013 17:14    Post subject:
basically I have four local variable sum values per module:

Division.
Addition.
Multiplication.
Subtraction.

Each sum stores the resulting value from input for example, Multiplication, the syntax is:

sum = firstRandom * secondRandom;

Then as its been pointed out, the if statement is, if (input == sum) then jumped to the case statement module and spits out a randomly selected congratulatory statement.

However, at present, I seem to have run into a small problem. the input will intake decimals, however the sum value doesn't recognize the fact I've used a decimal so I have to use a number that is closest to the whole number rounded up or down to the closest whole number. I've been trying to figure out how to correct this so it will register decimal place answers.

Just a quick piece of information, I have only been programming for 2 months, since my college course started on September 2nd.

TL;DR?

New student, still have a minor coding problem I'm trying to solve.

Thanks.
Back to top
Page 1 of 1 All times are GMT + 1 Hour
NFOHump.com Forum Index - Programmers Corner
Signature/Avatar nuking: none (can be changed in your profile)  


Display posts from previous:   

Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB 2.0.8 © 2001, 2002 phpBB Group