简体   繁体   中英

function does not return number correctly

I'm doing a simple program that return the percentage of each number of a set. But for some reason I cannot make the number return correctly except for the last one...

I cannot figure why it always print 0% for each number except the last one.

Here is the code :

public class MainWindow extends JFrame implements ActionListener {
private JButton theButton = new JButton("Calculer sur 100");
private JTextField textField = new JTextField("");
private JTextArea text = new JTextArea("");
private JScrollPane scroller = new JScrollPane(text);

public MainWindow() {
    setLayout(new BorderLayout());
    setTitle("Calculateur de pourcentage");
    setSize(400, 500);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLocationRelativeTo(null);

    layoutManagement();

    setVisible(true);
}

private void layoutManagement() {

    text.setEditable(false);
    theButton.addActionListener(this);

    getContentPane().add(scroller, BorderLayout.CENTER);
    getContentPane().add(textField, BorderLayout.NORTH);
    getContentPane().add(theButton, BorderLayout.SOUTH);
}

private int checkForNumber()
{
    int numl;
    try{
        numl = Integer.parseInt(textField.getText());
    }catch(NumberFormatException e)
    { 
        text.setText("Please try with number...");
        System.out.println("Error in number format. Returning 0");
        return 0;
    }
    System.out.println("numl = " + numl);
    return numl;
}

private double doMath(int i, int num)
{
    System.out.println("printing result = " + (i / num) * 100);
    return (i / num) * 100;
}

private void print100(int num) {
    for (int i = 1; i < num + 1; i++)
    {
        text.setText(text.getText() + "\n" + i + " : " + doMath(i, checkForNumber()));
    }
}

public void actionPerformed(ActionEvent e) {
    if (e.getSource() == theButton) {
        text.setText("");
        print100(checkForNumber());
    }
}

Any idea?

This is a classic case of integer division. When dividing two integers, the result is an integer. This means that 2/3 is 0. Multiplied by 100, it's still 0.

Instead of (i / num) * 100 , use i * 100 / num .

That way, you divide 200 by 3 instead, getting the 66 you're looking for.

Alternatively, you can do the calculation with floating point:

return ((double) i / num) * 100;

By casting, i becomes a double, which means that i / num is calculated as a double rather than an int.

When you put / between integers you get integer division. 3/2 = 1 not 1.5

You could try using doubles in the first place or casting to double like this (double)(3)/2 and get 1.5 but that has it's own problems because double isn't in base 10. If you want perfect calculations of non whole numbers in base 10 check out BigDecimal. I go into why here

The problem lies in

private double doMath(int i, int num) {

 System.out.println("printing result = " + (i / num) * 100); return (i / num) * 100; 

}

which is integer division, when dividing two integers, the result is an integer (that truncates any decimal value), you might want to parse a value to a float in order to fix that problem as follows:

FROM:

private double doMath(int i, int num) {

 System.out.println("printing result = " + (i / num) * 100); return (i / num) * 100; 

}

TO:

private double doMath(int i, int num) {

 System.out.println("printing result = " + (i / (float) num) * 100); return (i / (float) num) * 100; 

}

That way it makes a floating point division, which returns a floating point number that doesn't truncates the decimals.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM