Jump to content
xisto Community
Sign in to follow this  
kvarnerexpress

Having Trouble Getting Program To Print Out Multip

Recommended Posts

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:

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

Share this post


Link to post
Share on other sites

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:

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

188322[/snapback]


I haven't coded Java in a while. But programming logic tells me your error is in here:

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...

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.

 do{  //your code goes here  value++;} while(value < times);
Then simply concatenate the 10 digits with a newline. Similar to this:

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.

Share this post


Link to post
Share on other sites

I haven't coded Java in a while.  But programming logic tells me your error is in here:

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...

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. 

 do{  //your code goes here  value++;} while(value < times);
Then simply concatenate the 10 digits with a newline.  Similar to this:

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.

188329[/snapback]


 

The above poster is right, but you can use a variable in a loop. The modification to make is as follows:

 

for(int i = 0; i < times; i++)  // Loops the number of times inputed.{    n = generator.nextInt(10);    number += n + " ";           // int number will automatically get converted to a string here with a space added on or replace the " " with a "\n" for a newline. }

The above code should compile fine

Share this post


Link to post
Share on other sites

hi,

There obviously is a logical error. I am not able to understand u clearly. But from what I understand:

If you want 10 random nos, each to be printed 'time' times in a line

for(int i=0;i<10;i++){n = generator.nextInt(10);for(int j=0;j<times;j++)       System.out.print(n);System.out.println("");}

If you want 'time' no of random nos. then probably:

for(int i=0;i<times;i++){n = generator.nextInt(10);System.out.println(n);}

Cheers.

Share this post


Link to post
Share on other sites

It is so simple....here is the 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 times; System.out.print ("Number of times: "); times = scan.nextInt(); for (int i=0; i<times; i++) { int n = generator.nextInt(10); System.out.println(times+1 +" : " +n); } }}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.