Results 1 to 5 of 5

Thread: Help me please!

  1. #1
    Manikyr is offline Member
    Join Date
    Jan 2009
    Posts
    17
    Rep Power
    0

    Default Help me please!

    I canīt understand how to fix my code! Please help me with this!


    import javax.swing.*;
    public class algoritm1 {
    public stativ void main(String[] arg) {
    String kvinna;
    String man;
    String txt= "";
    int start= JOptionPane.showConfirmDialog(null,"Welcome!"+
    "\nDo you like to count the middleage?", "Question", 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,"Women?");
    if(knapp == 0) {
    kvinna= JOptionPane.showInputDialog("Womanīs age?");
    if (false)
    break;
    else {
    int k1= Integer.parseInt(kvinna);
    int medel1= k1 / j;
    String txt1= txt + medel1;
    }
    }
    else if(knapp == 1) {
    man= JOptionPane.showInputDialog("Manīs age??");
    if (false)
    break;
    else {
    int m1= Integer.parseInt(man);
    int medel2= m1 / j;
    String txt2= txt +medel2;
    }
    }
    else
    break;
    } // for1 slut
    } // for2 slsut
    } // if1 slut
    else if (knapp == 2) {
    JOptionPane.showMessageDialog(null, "Good bye!");
    JOptionPane.showMessageDialog(null, "Age for wimen" txt1 + "\n Age for men" + txt2);
    System.exit(0);
    }
    }
    }

    }
    }

  2. #2
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default What's the problem?

    Manikyr... it would help if you could tell the forum what the problem is:
    • Do you get compile errors or runtime errors (if any)?
    • What error code do you get (if any)?
    • What output do you get vs what you expect?

    Something that I do see in your code is that you have the following:
    Java Code:
    if (false)
    break;
    If you are expecting your program to break at that point, it never will. The flow of the code will always drop into the following "else" statement.

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  3. #3
    Manikyr is offline Member
    Join Date
    Jan 2009
    Posts
    17
    Rep Power
    0

    Default

    This code works, but doesnt work like I want i to.

    The school want me to write a program that asks about the age of a man or a woman, and after you have wrote about some people the program will count the average age.

    import javax.swing.*;
    public class algoritm1 {
    private static int knapp;
    private static String txt1;
    private static String txt2;
    public static void main(String[] arg) {
    String kvinna;
    String man;
    String txt= "";
    int start= JOptionPane.showConfirmDialog(null,"Welcome!"+
    "\nDo you like to count the middleage?", "Question", JOptionPane.YES_NO_OPTION);
    if (start == 0){
    for (int i=1 ; i>= 0; i++) {
    for(int j=1 ;j <= i; j++){
    int knapp1= JOptionPane.showConfirmDialog(null,"Women?");
    if(knapp1 == 0) {
    kvinna= JOptionPane.showInputDialog("Womanīs age?");
    if (true)
    break;
    else {
    int k1= Integer.parseInt(kvinna);
    int medel1= k1 / j;
    String txt3= txt + medel1;
    }
    }
    else if(knapp1 == 0) {
    man= JOptionPane.showInputDialog("Manīs age??");
    if (true)
    break;
    else {
    int m1= Integer.parseInt(man);
    int medel2= m1 / j;
    String txt4 = txt +medel2;
    }
    }
    else
    break;
    } // for1 slut
    } // for2 slsut
    } // if1 slut
    else if (knapp == 1) {
    JOptionPane.showMessageDialog(null, "Good bye!");
    JOptionPane.showMessageDialog(null, "Age for wimen" + txt1 + "\n Age for men" + txt2);
    System.exit(0);
    }

    }

    }

  4. #4
    tghn2b is offline Member
    Join Date
    Jan 2008
    Posts
    31
    Rep Power
    0

    Default

    umm I am just trying to save CJSLMAN do another reply here!

    WHAT is the point of if (true)you have it twice, it is the stranges code I have seen, unless you have done it that way to help you debug the program, in which case I am sorry I stuck my nose into it. because it will never do the "else" statements you may as well wrote

    Java Code:
    if (true)
    break;
    else {
    // have a nice day 
    // someone has too
    // dont forget to put the rubbish out on rubbish day
    }
    it would do the same thing when you run the code.





    One other thing I learnt recently, you have String kvinna; what does kvinna mean, what is the point of having top secret code words in java
    you could have used
    String thisVariableIsForTheAverageAgeOfAWoman;

    obviously you would use something more suitable like

    String aveageFemaleAge;

    it make your code a lot easier to read and at the end of the day your javac complier will compile it down the the exact same *.class file it would of done if you used
    String x2yz;
    Last edited by tghn2b; 02-08-2009 at 07:09 PM. Reason: made it clearer

  5. #5
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default Another language

    I imagine that the variables names are based on a another language.... Swedish if I remember correctly.
    Manikyr... I think these are the requirements:
    • Ask the age of men and women
    • When finished asking the ages then display the average ages of both.


    Here's some pseudo code to help you (I'm not even going to try to unravel the code you posted). Believe me... it will be easier to start over with a plan than to try to change the code you've already posted:

    Java Code:
    woman age = 0
    man age = 0
    women counter = 0
    men counter = 0
    
    loop
    ask if user is a women
    if yes start counter for women 
    ask woman her age 
    total women age = total women age + woman age  
    if not then user is a man 
    start men counter
    ask man his age
    total men age = total men age + man age
    end loop
    
    average women age = total women age / women counter
    average men age = total men age / men counter
    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •