PDA

View Full Version : PLeaSE help me on this ForLoop Java Program.


kdreamx
04-05-2007, 08:19 PM
I have to write a program that prompts the user to input the gender and GPA of four students, and print the average GPA for both male and female students when the loop ends.
I was able to compile and run, but uh...it doesn't work the way it should.
The dialog box pops up more than 8 times (4 for asking gender and 4 for asking GPA). I can't figure out why...
Also, i have to change this to For Loop...

Can anyone help me on this???
======================================================
import javax.swing.*;

public class Loop1
{
public static void main(String [] args)
{
String gender, male, female;
double gpa;
double maleTotal;
int maleCounter;
double femaleTotal;
int femaleCounter;
double maleGpa;
double femaleGpa;
String input1, input2;
final int MAX = 4;
int i;


maleTotal = 0;
femaleTotal = 0;

maleCounter = 0;
femaleCounter = 0;
i = 0;

input1 = JOptionPane.showInputDialog(null, "Enter Gender: male or female");

while (i < MAX)
{
if(input1.equals( "male" ))
{
input2 = JOptionPane.showInputDialog(null, "Enter GPA:");
gpa = Double.parseDouble(input2);
maleTotal = maleTotal + gpa;
maleCounter++;
}
else if(input1.equals( "female" ))
{
input2 = JOptionPane.showInputDialog(null, "Enter GPA:");
gpa = Double.parseDouble(input2);
femaleTotal = femaleTotal + gpa;
femaleCounter++;
}
input1 = JOptionPane.showInputDialog(null, "Enter Gender: male or female");
i++;


}
if(maleCounter != 0 && femaleCounter != 0)
{
maleGpa = (double)maleTotal/maleCounter;
femaleGpa = (double)femaleTotal/femaleCounter;
JOptionPane.showMessageDialog(null, "Average Male GPA is:" + maleGpa+"\nAverage Female GPA is:" + femaleGpa);
}
System.exit(0);

}
}

Aradon
04-05-2007, 10:37 PM
Well let me explain what a for loop is.

A for loop is much like a while loop. In fact a for loop can be used as a while loop given a certain scenario, but we will get to that later.

All for loops have the following format


for( initial_condition ; boolean_expression ; ending_expression)
// body


So, we have an initial condition, where you can initialize a variable if you'd like.

Then a boolean expression that tests a condition if it's true or false (much like a while loop). If it's true then it goes into the body of the loop.

At the end of the body the ending_expression is then called in the for loop, and we evaluate the boolean expression all over again.

So for example, if I wanted to count to ten from 1



for(int i = 1; i <= 10; i++)
{
System.out.println(i);
}


This would output:

1
2
3
4
5
6
7
8
9
10


We have an initial condition where we initalize i to 1; (int i = 0;)

We have a boolean test (i <= 10)

And we have an ending condition to increment the number i (i++)

An equivalent while loop:


int i = 1;
while(i <= 10)
{
System.out.println(i)
i++;
}


So, given this information, look at the while loop that you have. What parts of the for loop do you see in the while loop?

********

As a side note, a for loop while:


for( ; boolean ; )


If you need more information let us know.

daniel_g
04-05-2007, 10:47 PM
I have to write a program that prompts the user to input the gender and GPA of four students, and print the average GPA for both male and female students when the loop ends.
I was able to compile and run, but uh...it doesn't work the way it should.
The dialog box pops up more than 8 times (4 for asking gender and 4 for asking GPA). I can't figure out why...

mmm, regarding this part of your question, I'm kind of confused about what the problem is. If you want to enter info for 4 students, then you will have to ask 4 genders, and 4 GPA's, therefore, the dialog box poping 8 times sounds good.

So this is what's going on(where the stuff in red is the user input):
gender: male - GPA: 2.1
gender: female - GPA: 3
gender: female - GPA: 4
gender: female - GPA: 3

And output:
Average GPA on males: 2.1
Average GPA on females: 3.33