• Post Reply Bookmark Topic Watch Topic
  • New Topic

Calculating averageage women and men  RSS feed

 
Birgitta Ols�n
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!
I have just started with Java and found this forum before I have been doing a lot of reading here but now I need some help since I�m stuck.
I want to make a program that will calculate the averageage for women and and men one group for women and one for men.At the end the program should say buy buy and then show the averageage for women and men.
The program looks like this.
import javax.swing.*;

public class Person {

public static void main(String [] arg) {

String kvinna;

String man;

String txt= "";

int start= JOptionPane.showConfirmDialog(null,"V�lkommen!"+

"\nVill du r�kna medel�ldern?", "Fr�ga", JOptionPane.YES_NO_OPTION);

if (start == 0){

for (int i=1 ; i>= 0; i++) {

for(int j=1 ;j <= i; j++){

int knapp= JOptionPane.showConfirmDialog(null,"En kvinna?");

if(knapp == 0) {

kvinna= JOptionPane.showInputDialog("Hur gammal �r kvinnan?");

if (false)

break;

else {

int k1= Integer.parseInt(kvinna);

int medel1= k1 / j;

String txt1= txt + medel1;
}
}

else if(knapp == 1) {

man= JOptionPane.showInputDialog("Hur gammal �r mannen?");

if (false)

break;

else {

int m1= Integer.parseInt(man);

int medel2= m1 / j;

String txt2= txt +medel2;

}
}

else

break;

} // for1 slut

} // for2 slut

} // if1 slut

else




JOptionPane.showMessageDialog(null, "Hej d�!"); (Hej d� is Buy Buy in swedish)

System.exit(0);


//JOptionPane.showMessageDialog(null,"Medel�ldern f�r kvinnor �r: " + txt1 + "\nMedel�ldern f�r m�n �r: " + txt2);( this is the calculation for women and men)

//System.exit(0);
} //main slut
} //class slut

Thansk for input and help in advance.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so what exactly is your question? The more specific you can be, the easier it is to help. Tell us where you are stuck, what actually IS happening and what you think SHOULD be happening.
 
Campbell Ritchie
Marshal
Posts: 55786
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Please put "code" tags round any code; it makes it easier to read.

Don't just use 0 and 1. Use JOptionPane.YES_OPTION and JOptionPane.NO_OPTION. That way you can tell what the numbers mean.

You have an infinite loop with no means of escape. Enter a test somewhere in the loop for continuation. Suggest starting the loop something like this:-

Try again, then come back. And my Swedish spelling will have become even worse in the meantime.
 
Birgitta Ols�n
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks�s both for your help I think I have managed to get the program to work now.Next time I try to explain better what i�m doing.
My program looks like this now.
mport javax.swing.JOptionPane;

public class Person4 {

public static void main(String [] arg) {

String kvinna;

String man;

String txt= "";
int i=0,j=0;
int sumk1=0,summ1 =0;

int start= JOptionPane.showConfirmDialog(null,"V�lkommen!"+

"\nVill du r�kna medel�ldern?", "Fr�ga", JOptionPane.YES_NO_OPTION);

while(start == 0){


int knapp= JOptionPane.showConfirmDialog(null,"En kvinna?");

if(knapp == 0) {

kvinna= JOptionPane.showInputDialog("Hur gammal �r kvinnan?");

if(kvinna != null ){

int k1= Integer.parseInt(kvinna);
sumk1 = sumk1 + k1;
i = i +1;
}
}

else if(knapp == 1) {

man= JOptionPane.showInputDialog("Hur gammal �r mannen?");
if(man != null){
int m1= Integer.parseInt(man);
summ1 = summ1 + m1;
j = j +1;
}

}

else

break;


} // if1 slut





JOptionPane.showMessageDialog(null, "Hej d�!");



JOptionPane.showMessageDialog(null,"Medel�ldern f�r kvinnor �r: " + sumk1/i + "\nMedel�ldern f�r m�n �r: " + summ1/j);
System.exit(0);


// System.exit(0);
} //main slut
} //class slut
 
Campbell Ritchie
Marshal
Posts: 55786
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, it is working. Well done.

Please use "code" tags around programming; it makes it easier to read.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!