| | |
Help in writing a litle program for a friend
![]() |
•
•
Join Date: Apr 2009
Posts: 6
Reputation:
Solved Threads: 0
He wants to input in the program an undefinite number of (numbers like 1, 2 etc), all positive. With that done he wants the program to randomize these numbers and show them one at a time in a random distribution. i thought it was easier when he asked but i found out while writing it that its not that easy. i need help. anyone?
First you can have the user input the number of values to input into the data structure(arrays are a good choice). Then use a for loop to ask the user to put in each value.
Last edited by Grn Xtrm; Apr 15th, 2009 at 4:41 pm.
Check out my new band URL on facebook. I'm the bass player. :) Become a fan and leave comments if you like.
URL on facebook!
URL on facebook!
•
•
Join Date: Apr 2009
Posts: 6
Reputation:
Solved Threads: 0
thx for ut reply.. heres what ive done. could u kindly check it for me?
//allow for input
import javax.swing.*;
import java.util.*;
public class Sorteggio
{
public static void main (String[] args)
{
//to add the numbers
int numero;
ArrayList numero = new ArrayList();
//loop part
boolean cont = false;
do
{
cont = false;
//allows for input
int choice = Integer.parseInt(JOptionPane.showInputDialog("Type\n"+"1 to add new number\2"+"2 to show random number"));
if(choice==1)
{
//metti il numero
int scelta = Integer.parseIntJOptionPane.showInputDialog("what number would you like to add");
//add the number
numero.add(scelta);
}
if(choice==2)
{
String output = "your number is..\n";
output+=((int)randomnumber);
//we randomize the numbers
int randomnumber = Math.random(numero);
JOptionPane.showMessageDialog(null, output);
}
//input to repeat
String repeat = JOptionPane.showInputDialog("Again?");
if(repeat.equals("si"))
cont = true;
}
while(cont);
}
}
//allow for input
import javax.swing.*;
import java.util.*;
public class Sorteggio
{
public static void main (String[] args)
{
//to add the numbers
int numero;
ArrayList numero = new ArrayList();
//loop part
boolean cont = false;
do
{
cont = false;
//allows for input
int choice = Integer.parseInt(JOptionPane.showInputDialog("Type\n"+"1 to add new number\2"+"2 to show random number"));
if(choice==1)
{
//metti il numero
int scelta = Integer.parseIntJOptionPane.showInputDialog("what number would you like to add");
//add the number
numero.add(scelta);
}
if(choice==2)
{
String output = "your number is..\n";
output+=((int)randomnumber);
//we randomize the numbers
int randomnumber = Math.random(numero);
JOptionPane.showMessageDialog(null, output);
}
//input to repeat
String repeat = JOptionPane.showInputDialog("Again?");
if(repeat.equals("si"))
cont = true;
}
while(cont);
}
}
•
•
Join Date: Apr 2008
Posts: 972
Reputation:
Solved Threads: 145
As the user enters numbers add them to a List ( http://java.sun.com/j2se/1.4.2/docs/...util/List.html ). Then in a loop generate a random integer between 0 and the size() of the list. Get(int index) the element at that index, print it, then remove(int index) that element. Loop until the List is empty.
I would get rid of this
I do input using the predefined scanner class
That's how I add user input numbers into the list.
By the way. Are you Italian?
Java Syntax (Toggle Plain Text)
int numero;
I do input using the predefined scanner class
Java Syntax (Toggle Plain Text)
Scanner scan = new Scanner(System.in); System.out.println("Enter how many numbers will be added"); int nums = scan.nextInt(); System.out.println("Input the numbers"); for(int index = 0; index<nums; ++index) { int input = scan.nextInt(); numero.add(input); }
By the way. Are you Italian?
Check out my new band URL on facebook. I'm the bass player. :) Become a fan and leave comments if you like.
URL on facebook!
URL on facebook!
•
•
Join Date: Apr 2009
Posts: 6
Reputation:
Solved Threads: 0
yeah i am from rome.
sry but i am very new to java and this is the first prog i get to do.. should i add that part like this?
how should i continue i think i was doing it wrong before
sry but i am very new to java and this is the first prog i get to do.. should i add that part like this?
•
•
•
•
import java.util.*;
public class Sorteggio
{
public static void main (String[] args)
{
//to add the numbers
ArrayList numero = new ArrayList();
//loop part
boolean cont = false;
do
{
cont = false;
//allows for input
Scanner scan = new Scanner(System.in);
System.out.println("Enter how many numbers will be added");
int nums = scan.nextInt();
System.out.println("Input the numbers");
for(int index = 0; index<nums; ++index)
{
int input = scan.nextInt();
numero.add(input);
}
The code segment I provided earlier is just to get the numbers into the list. Sorry, but I don't know that much about random numbers. Try searching on google.com.
Check out my new band URL on facebook. I'm the bass player. :) Become a fan and leave comments if you like.
URL on facebook!
URL on facebook!
And don't forget to acknowledge the help you received here to your instructor, ah, I mean friend, of course. ;-)
Java Programmer and Sun Systems Administrator
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
•
•
Join Date: Apr 2009
Posts: 6
Reputation:
Solved Threads: 0
thank you guys for the help however its a bit too complicated for me at this level. this is not a course im taking but personal interest in programming and i thought java is good intro to programming in general. ive been learning using pdf books for the past week and i let a few friends now about it. this particular friends needs this prog in less than two weeks but i might not be able to learn what i need to learn and then write it by then. it would help a lot if u could tell me the steps and procedure to make this work. also im a bit lost between the posts and im still unsure if my previous attempt worked or i should completely rewrite it. thx for ur time!
![]() |
Other Threads in the Java Forum
- Previous Thread: How to do Makeup of Java default GUI...??
- Next Thread: Found Bug but can't understand it
| Thread Tools | Search this Thread |
911 actionlistener addball addressbook android api append applet application array arrays automation binary bluetooth button character chat class client code component consumer css csv database desktop eclipse ee error fractal ftp game givemetehcodez graphics gui html ide image input integer j2me japplet java javaarraylist javac javaee javaprojects jni jpanel julia jvm linked linux list loan loop map method methods mobile netbeans newbie objects online oriented output panel phone printf problem program programming project projects properties recursion replaydirector reporting researchinmotion rotatetext rsa scanner se server service set sms software sort sql string swing test threads time transfer tree ubuntu update windows






