import javax.swing.JOptionPane;
public class Program{
public static void main (String [] args) {
int DateofBirth,Month,Day,Year,Minutes,Seconds,Hundredths,fMinutes,fSeconds,fHundredths ;
String input,FirstName,FamilyName,Gender,Courses,equals,Number;
while loop
int = Number=>20
Number = JOptionPane.showInputDialog(null,"Enter Number of Competitors");
FirstName = JOptionPane.showInputDialog(null,"Enter FirstName");
FamilyName = JOptionPane.showInputDialog(null,"Enter FamilyName");
input = JOptionPane.showInputDialog(null, "Enter Day of Date of Birth ");
Day = Integer.parseInt (input);
while (Day<1||Day>31) {
input = JOptionPane.showInputDialog
(null, "Enter a Number Day Between 1 to 31");
Day = Integer.parseInt (input) ;
}
input = JOptionPane.showInputDialog(null, "Enter Month of Date of Birth");
Month= Integer.parseInt (input);
while (Month<1||Month>12) {
input = JOptionPane.showInputDialog
(null, "Enter a Month Between 1 to 12");
Month = Integer.parseInt (input) ;
}
input = JOptionPane.showInputDialog(null, "Enter Year of Birth ");
Year = Integer.parseInt (input);
while (Year<1990||Year>2012) {
input = JOptionPane.showInputDialog
(null, "Enter a Year Between 1990 to 2012 ");
Year = Integer.parseInt (input) ;
}
System.out.println("Number of Competitors = " + Number) ;
System.out.println("Firstname = " + FirstName) ;
System.out.println("FamilyName = " + FamilyName) ;
System.out.println("Dateofbirth = " + Day + "/" + Month + "/" + Year) ;
}
}
need help java loop
Page 1 of 19 Replies - 379 Views - Last Post: 10 April 2012 - 05:09 AM
#1
need help java loop
Posted 09 April 2012 - 03:00 PM
im given a assessment on inputs. iv got an input of number of competitors. if i answer 5, I'm a bit confused how to loop the program 5 times so i have 5 details of competitors of information
Replies To: need help java loop
#2
Re: need help java loop
Posted 09 April 2012 - 03:03 PM
If you know how many times you have to loop, you use a for loop:
for ( int i = 0 ; i < numberOfLoops ; i++ )
{
// do this thing
}
#3
Re: need help java loop
Posted 09 April 2012 - 03:16 PM
GregBrannon, on 09 April 2012 - 03:03 PM, said:
If you know how many times you have to loop, you use a for loop:
for ( int i = 0 ; i < numberOfLoops ; i++ )
{
// do this thing
}
it still does not work, I'm still confused. for example i can enter this input for one person but if i want to input two people. it don't work. I'm trying to loop program to the number of time i want it to loop of all my inputs of name an ect
#4
Re: need help java loop
Posted 09 April 2012 - 03:19 PM
What output did you get from that particular loop?
#5
Re: need help java loop
Posted 10 April 2012 - 03:59 AM
[code]
import javax.swing.JOptionPane;
public class Program2{
public static void main (String [] args) {
int DateofBirth,Month,Day,Year,Minutes,Seconds,Hundredths,fMinutes,fSeconds,fHundredths ;
String input,FirstName,FamilyName,Gender,Courses,equals,Number,numberOfLoops;
for ( int i = 0 ; i < numberOfLoops ; i++ )
{
}
FirstName = JOptionPane.showInputDialog(null,"Enter FirstName");
FamilyName = JOptionPane.showInputDialog(null,"Enter FamilyName");
input = JOptionPane.showInputDialog(null, "Enter Day of Date of Birth ");
Day = Integer.parseInt (input);
while (Day<1||Day>31) {
input = JOptionPane.showInputDialog
(null, "Enter a Number Day Between 1 to 31");
Day = Integer.parseInt (input) ;
}
input = JOptionPane.showInputDialog(null, "Enter Month of Date of Birth");
Month= Integer.parseInt (input);
while (Month<1||Month>12) {
input = JOptionPane.showInputDialog
(null, "Enter a Month Between 1 to 12");
Month = Integer.parseInt (input) ;
}
input = JOptionPane.showInputDialog(null, "Enter Year of Birth ");
Year = Integer.parseInt (input);
while (Year<1990||Year>2012) {
input = JOptionPane.showInputDialog
(null, "Enter a Year Between 1990 to 2012 ");
Year = Integer.parseInt (input) ;
}
System.out.println("Firstname = " + FirstName) ;
System.out.println("FamilyName = " + FamilyName) ;
System.out.println("Dateofbirth = " + Day + "/" + Month + "/" + Year) ;
}
}
[/code}
I'm trying to loop my program for example 5 time using the same information that the user will input. iv used a for loop but it seem there are no output and become a error in my code. I'm not very use how to do this which i need some help.
import javax.swing.JOptionPane;
public class Program2{
public static void main (String [] args) {
int DateofBirth,Month,Day,Year,Minutes,Seconds,Hundredths,fMinutes,fSeconds,fHundredths ;
String input,FirstName,FamilyName,Gender,Courses,equals,Number,numberOfLoops;
for ( int i = 0 ; i < numberOfLoops ; i++ )
{
}
FirstName = JOptionPane.showInputDialog(null,"Enter FirstName");
FamilyName = JOptionPane.showInputDialog(null,"Enter FamilyName");
input = JOptionPane.showInputDialog(null, "Enter Day of Date of Birth ");
Day = Integer.parseInt (input);
while (Day<1||Day>31) {
input = JOptionPane.showInputDialog
(null, "Enter a Number Day Between 1 to 31");
Day = Integer.parseInt (input) ;
}
input = JOptionPane.showInputDialog(null, "Enter Month of Date of Birth");
Month= Integer.parseInt (input);
while (Month<1||Month>12) {
input = JOptionPane.showInputDialog
(null, "Enter a Month Between 1 to 12");
Month = Integer.parseInt (input) ;
}
input = JOptionPane.showInputDialog(null, "Enter Year of Birth ");
Year = Integer.parseInt (input);
while (Year<1990||Year>2012) {
input = JOptionPane.showInputDialog
(null, "Enter a Year Between 1990 to 2012 ");
Year = Integer.parseInt (input) ;
}
System.out.println("Firstname = " + FirstName) ;
System.out.println("FamilyName = " + FamilyName) ;
System.out.println("Dateofbirth = " + Day + "/" + Month + "/" + Year) ;
}
}
[/code}
I'm trying to loop my program for example 5 time using the same information that the user will input. iv used a for loop but it seem there are no output and become a error in my code. I'm not very use how to do this which i need some help.
#6
Re: need help java loop
Posted 10 April 2012 - 04:03 AM
The question is...What output are you getting. what is the console giving you?
#7
Re: need help java loop
Posted 10 April 2012 - 04:07 AM
#8
Re: need help java loop
Posted 10 April 2012 - 04:08 AM
do a debug of your file or project and tells us what the console shows you!
#9
Re: need help java loop
Posted 10 April 2012 - 04:24 AM
rik2012,
You're not very clear, but I think you're trying to say that you want the same loop to execute for some number of people, 2 at least but maybe more. If that's what you mean, there are multiple ways to accomplish it. The two most common would be a nested loop or a method that gathers the data for each person in a loop. I would prefer the latter:
or if you know exactly how many people will be entering data:
Then
Now, you can't just copy and past the above into your code and expect it to work. It's essentially pseudo-code to give you an outline of the concepts involved. It's not meant to be THE ANSWER. Please don't copy and paste the above into your code and then come back saying it doesn't work.
Do come back with real changes to your code that incorporate the above concepts or any others that you think smell pretty but that you need help with because you don't understand them. Post your updated code -- in code tags -- and explain what you don't understand and need help with. As always, post any error messages you don't understand, copied and pasted.
You're not very clear, but I think you're trying to say that you want the same loop to execute for some number of people, 2 at least but maybe more. If that's what you mean, there are multiple ways to accomplish it. The two most common would be a nested loop or a method that gathers the data for each person in a loop. I would prefer the latter:
while ( thereArePeopleLeft )
{
getThePersonData();
// determine if there are people left to enter data
}
or if you know exactly how many people will be entering data:
for ( int i = 0 ; i < numberOfPeople ; i++ )
{
getThePersonData();
}
Then
protected void getThePersonData()
{
// gather the data for each person and store the data
// in a collection or an instance of a Person class (?)
}
Now, you can't just copy and past the above into your code and expect it to work. It's essentially pseudo-code to give you an outline of the concepts involved. It's not meant to be THE ANSWER. Please don't copy and paste the above into your code and then come back saying it doesn't work.
Do come back with real changes to your code that incorporate the above concepts or any others that you think smell pretty but that you need help with because you don't understand them. Post your updated code -- in code tags -- and explain what you don't understand and need help with. As always, post any error messages you don't understand, copied and pasted.
#10
Re: need help java loop
Posted 10 April 2012 - 05:09 AM
GregBrannon, on 10 April 2012 - 04:24 AM, said:
rik2012,
You're not very clear, but I think you're trying to say that you want the same loop to execute for some number of people, 2 at least but maybe more. If that's what you mean, there are multiple ways to accomplish it. The two most common would be a nested loop or a method that gathers the data for each person in a loop. I would prefer the latter:
or if you know exactly how many people will be entering data:
Then
Now, you can't just copy and past the above into your code and expect it to work. It's essentially pseudo-code to give you an outline of the concepts involved. It's not meant to be THE ANSWER. Please don't copy and paste the above into your code and then come back saying it doesn't work.
Do come back with real changes to your code that incorporate the above concepts or any others that you think smell pretty but that you need help with because you don't understand them. Post your updated code -- in code tags -- and explain what you don't understand and need help with. As always, post any error messages you don't understand, copied and pasted.
You're not very clear, but I think you're trying to say that you want the same loop to execute for some number of people, 2 at least but maybe more. If that's what you mean, there are multiple ways to accomplish it. The two most common would be a nested loop or a method that gathers the data for each person in a loop. I would prefer the latter:
while ( thereArePeopleLeft )
{
getThePersonData();
// determine if there are people left to enter data
}
or if you know exactly how many people will be entering data:
for ( int i = 0 ; i < numberOfPeople ; i++ )
{
getThePersonData();
}
Then
protected void getThePersonData()
{
// gather the data for each person and store the data
// in a collection or an instance of a Person class (?)
}
Now, you can't just copy and past the above into your code and expect it to work. It's essentially pseudo-code to give you an outline of the concepts involved. It's not meant to be THE ANSWER. Please don't copy and paste the above into your code and then come back saying it doesn't work.
Do come back with real changes to your code that incorporate the above concepts or any others that you think smell pretty but that you need help with because you don't understand them. Post your updated code -- in code tags -- and explain what you don't understand and need help with. As always, post any error messages you don't understand, copied and pasted.
my task is to to design and develop a Java program to input, process, and present the results from a biathlon skiing event. You should accept as input for each competitor, name,gender and gender. i have designed this code to allow one competitors in this program. my problem is that i want to add more competitors so i am trying to loop the program for example three times and which as an output off all inputs from all competitor.
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote




|