View Full Version : how do you get repeating variables for loops?
Doryin
11-21-2004, 11:50 PM
I need to create a loop that gives numbers to be added, but I can't for the life of me figure out how to get them to repeat the previous numbers if it's given an incorrect response... I would appreciate any help I could get with this :\
nevarmore
11-22-2004, 11:45 AM
What? Try being more descriptive.
I'm going to guess that you know how to make a loop, but are not to sure about how to properly store your variables.
Doryin
11-22-2004, 01:06 PM
Yea, thats it... to be more specific I can show u what i have so far:
int val1
val2,
answer,
userInput,
messageNum;
Random generator = new Random( unchecked( (int)DateTime.Now.Ticks ) );
string name;
Console.WriteLine( "Hi! I will help you learn arithmetic!" );
Console.Write( "But first, type your name and press the Enter key:" );
name = Console.ReadLine();
Console.WriteLine( "\nHello, {0}!", name );
Console.WriteLine( "Press -1 at any time to quit." );
for ( ;; )
{
val1 = (int)( generator.NextDouble() * 13 ) + 0;
val2 = (int)( generator.NextDouble() * 13 ) + 0;
answer = val1 + val2;
Console.Write( "\nWhat is {0} + {1}?: ", val1, val2 );
userInput = Convert.ToInt32( Console.ReadLine() );
if ( userInput == -1 )
break;
else
if ( userInput == answer )
{
messageNum = (int)( generator.NextDouble() * 3 ) + 0;
switch ( messageNum )
{
case 0:
Console.WriteLine( "Excellent!" );
break;
case 1:
Console.WriteLine( "Great!" );
break;
default:
Console.WriteLine( "Awesome!" );
break;
}
}
else
}
messageNum = (int)( generator.NextDouble() * 3 ) + 0;
switch ( messageNum )
{
case 0:
Console.WriteLine( "Keep trying." );
break;
case 1:
Console.WriteLine( "Not quite. Try again." );
break;
default:
Console.WriteLine( "Your mommy and daddy don't love you anymore." );
break;
}
}
}
Console.WriteLine( "\nGoodbye {0}! See you next time.", name );
Console.Write( "\nPlease press the Enter key" );
Console.ReadLine();
}
}
Virus
11-22-2004, 07:19 PM
Ok, I understand what your trying to do. Try something more along the lines of having a while loop, (keep in mind im a java programmer, not c#, but this is what i would do in java or c++, sorta like c#)
import java.io.*;
import java.util.*;
public class test
{
public static void main(String args[]) throws Exception
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
Random rand = new Random();
String name, temp;
int v1, v2, realAnswer, userAnswer, messageNum;
boolean done = false;
System.out.println("Hi! I will help you learn arithmetic!");
System.out.print("But first, type your name and press the Enter key: ");
name = in.readLine();
System.out.println("Hello " + name);
System.out.println("Enter -1 at any time to quit.");
while(!done)
{
v1 = rand.nextInt(13);
v2 = rand.nextInt(13);
realAnswer = v1 + v2;
System.out.print("What is " + v1 + " + " + v2 + "?: ");
temp = in.readLine(); //In java, reads in a String for what was entered
userAnswer = Integer.parseInt(temp); //Parsing our string to return our integer value
if(userAnswer == -1)
done = true;
while(userAnswer != realAnswer && !done)
{
messageNum = rand.nextInt(3);
switch(messageNum)
{
//Easy enough here, output all your Whoops you suck outputs
}
System.out.print("What is " + v1 + " + " + v2 + "?: ");
temp = in.readLine();
userAnswer = Integer.parseInt(temp);
}
if(userAnswer == realAnswer)
{
//Output all your Good job and thats it
}
}
}
}
Hope this helps some, good luck to you.
(most of the code should be self explanatory, but if ya dont know java, feel free to ask what any of it means)
Doryin
11-23-2004, 11:53 PM
thanks a lot, that worked perfectly :D
vBulletin® v3.7.0, Copyright ©2000-2009, Jelsoft Enterprises Ltd.