QUOTE(kvarnerexpress @ Sep 22 2005, 07:27 AM)
i'm trying to have a user input a number between 1 to 5, and that number will be the number of times the program will print out random 10 digit integers. Example, if user inputs 3, then the program will print out 3 random 10 digit intergers.
Here's what i have so far
Code:
CODE
import java.util.Scanner;
import java.util.Random;
public class test3
{
public static void main (String[] args)
{
Scanner scan = new Scanner(System.in);
Random generator = new Random();
int n=0, times;
String number = "";
System.out.print ("Number of times: ");
times = scan.nextInt();
for (int i=0; i<10; i++)
{
n = generator.nextInt(10);
number += n * times;
}
System.out.println(number);
}
}
I tried multiplying it by "times" but it just multiplies that random 10 digit interger by whatever the number the user inputs for "time".
Anyone know what i'm doing wrong?
Thanks,kvarnerexpress
I haven't coded Java in a while. But programming logic tells me your error is in here:
CODE
for (int i=0; i<10; i++)
{
n = generator.nextInt(10);
number += n * times;
}
Why are you doing "n*times"?
What you'll want to do is use variable times in your for loop...
CODE
for (int i=0; i< times; i++)
I seem to recall not being able to use a variable as the control counter in the loop, so you may be better off using a different loop.
CODE
do
{
//your code goes here
value++;
} while(value < times);
Then simply concatenate the 10 digits with a newline. Similar to this:
CODE
number += '\n' + n;
This code probably won't work as written, I'm at work and do not have a compiler here...but it should give you enough to get started.
Reply