简体   繁体   English

函数不能正确返回数字

[英]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. 我不知道为什么它总是为每个数字(除了最后一个数字)打印0%。

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. 这意味着2/3为0。乘以100,仍为0。

Instead of (i / num) * 100 , use i * 100 / num . 代替(i / num) * 100 ,使用i * 100 / num

That way, you divide 200 by 3 instead, getting the 66 you're looking for. 这样,您可以将200除以3,得到所需的66。

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. 通过强制转换, i成为双精度型,这意味着i / num计算为双精度型而不是整数。

When you put / between integers you get integer division. 当您将/放在整数之间时,会得到整数除法。 3/2 = 1 not 1.5 3/2 = 1不是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. 您可以尝试首先使用双精度数,或者像这样(double)(3)/ 2强制转换成双精度数并得到1.5,但这有其自身的问题,因为double不在10以内。如果您想对非整数进行完美的计算在以10为底的基础上,查看BigDecimal。 I go into why here 我进入为什么在这里

The problem lies in 问题出在

private double doMath(int i, int num) { 私人双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) { 私人双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) { 私人双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. 这样,它将进行浮点除法,该除法将返回不截断小数的浮点数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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