Results 1 to 3 of 3
- 12-01-2010, 09:47 AM #1
Member
- Join Date
- Dec 2010
- Location
- Chicago, IL
- Posts
- 10
- Rep Power
- 0
How to ask the user how many elements to print from an array?
Hello,
I have a really simple question, I'm feeling a little slow right now. The objective is to initialize the first n positions of a given array of 100 elements with n random integers using the Random class, n should be given by the user.
Basically, I just need help figuring out what to assign to this print statement: System.out.print("How many elements would you like to print? Must be between 0 and 100.");. I tried n[0] = keyboard.nextInt(); but that didnt work.
This is my code:
Java Code:import java.util.Scanner; import java.util.Random; public class PA6 { public static void main(String[] args) { int n[] = new int[100]; Scanner keyboard = new Scanner(System.in); Random randomNumbers = new Random(); System.out.print("How many elements would you like to print? Must be between 0 and 100."); //////// what to put here?? ////////////////// for (int i = 0; i < n.length; i++) { n[i] = (int) (randomNumbers.nextInt(1000) + 1); } for (int b = 0; b < n.length; b++) System.out.print(n[b] + " "); System.out.println(); } }
- 12-02-2010, 04:11 AM #2
Member
- Join Date
- Nov 2010
- Posts
- 44
- Rep Power
- 0
///what to post here
basically you need to declare a int variable
int num;
num = keyboard.nextInt();
but i have questions about your logic:
shouldn't the second expression in both for loops be i <num;?Java Code:int n[] = new int[100]; Scanner keyboard = new Scanner(System.in); Random randomNumbers = new Random(); System.out.print("How many elements would you like to print? Must be between 0 and 100."); //////// what to put here?? ////////////////// // shouldn't the second expression in for statment be i<num;i++;? for (int i = 0; i < n.length; i++) { n[i] = (int) (randomNumbers.nextInt(1000) + 1); } for (int b = 0; b < n.length; b++) System.out.print(n[b] + " "); System.out.println();
this is my sample output when i set both for loops middle expression with i<num, instead of array length
Java Code:How many elements would you like to print? Must be between 0 and 100: 20 481 309 430 173 262 502 462 602 929 972 299 890 902 405 738 18 783 197 526 547
- 12-02-2010, 04:18 AM #3
Member
- Join Date
- Dec 2010
- Location
- Chicago, IL
- Posts
- 10
- Rep Power
- 0
Similar Threads
-
How to print JTable elements using DefaultTableModel
By chyrl in forum Advanced JavaReplies: 7Last Post: 08-30-2010, 03:35 PM -
sum of elements in array
By myst in forum New To JavaReplies: 7Last Post: 07-17-2010, 08:36 AM -
Get value of a String into elements of an Array.
By mainy in forum New To JavaReplies: 1Last Post: 08-01-2009, 09:17 PM -
How to check whether two elements are available in an array?
By venkatteshb in forum New To JavaReplies: 8Last Post: 08-27-2008, 10:45 PM -
Help with array of elements
By zoe in forum New To JavaReplies: 1Last Post: 07-24-2007, 05:33 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks