简体   繁体   English

函数仅返回偶数

[英]Function return only even number

My program is supposed to print all the percentage of a given number. 我的程序应该打印给定数字的所有百分比。 It was working fine until i add a function that color the result(red is low and green is high). 直到我添加了一个为结果加颜色的功能(红色为低,绿色为高),它的工作正常。 Now it only print odd number or even number, but not both. 现在,它仅打印奇数或偶数,但不能同时打印两者。 As for the coloring, it work backward, from green to red. 至于着色,它从绿色到红色向后工作。 I want all the results to print and to be colored according to their value. 我希望将所有结果打印出来并根据其值进行着色。

Here's the code 这是代码

public class Window extends JFrame implements ActionListener{

private JButton theButton = new JButton("Calculer sur 100");
private JEditorPane text = new JEditorPane();
private JTextField textField = new JTextField("Écrire un nombre");
private JScrollPane scroller = new JScrollPane(text);
private StringBuilder sb = new StringBuilder();
private Style style;

public Window() {
    setLayout(new BorderLayout());
    setTitle("Test");
    setSize(400, 500);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLocationRelativeTo(null);
    text.setContentType("text/html");

    theButton.addActionListener(this);

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

/**
 * Prints the result on text
 * @param num
 */
private void print100(int num) {
    for (int i = 1; i < num + 1; i++) {
        text.setText(appendString(i));
    }
}


/**
 * Color from red to green according to the result
 * @param a
 * @return a haxaecimal to color the answer
 */
private String colorOnDigit(double a) {
    double green, red;
    int g, r;
    double power = a;
    int blue = 0;

    green = 255 * Math.sqrt( Math.cos ( power * Math.PI / 200 ));
    red = 255 * Math.sqrt( Math.sin ( power * Math.PI / 200 ));

    int precision = 10; //Number of zero = number of digits
    green = Math.floor(green * precision + .5) / precision;
    red = Math.floor(red * precision + .5) / precision;

    r = (int) red;
    g = (int) green;

    String hex = String.format("#%02x%02x%02x", r, g, blue);

    System.out.println("blue " + blue);
    System.out.println("Green " + green);
    System.out.println("Red " + red);
    System.out.println("----------");
    return "<font color = \"" + hex + ">";
}

/**
 * convert the number to string 
 * @param i
 * @return a string that contains the information
 */
private String appendString(int i){
    double a = doMath(i, checkForNumber());

    String s = "<br>" + colorOnDigit(a) + i + " : " + a + "</font>";

    return sb.append(s).toString();
}

/**
* Check if the text in the text is numbers
* return numl
*/
private int checkForNumber() {
    int numl;
    try {
        numl = Integer.parseInt(textField.getText());
    } catch (NumberFormatException e) {
        text.setText("Essayer avec des nombres...");
        return 0;
    }
    return numl;
}

/**
* leave specific number of digit after the dot
* return myNum
*/
private double doMath(int i, int num) {
    double myNum = ((double) i / num) * 100;
    int precision = 100; //Number of zero = number of digits
    myNum = Math.floor(myNum * precision + .5) / precision;
    return myNum;
}

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

} }

When i call System.out.print() it has the exact number of what i entered in the JTextField . 当我调用System.out.print()它具有我在JTextField输入的确切数字。

I didn't found any answer on google nor on StackOverflow. 我没有在Google或StackOverflow上找到任何答案。 I can't figured it out, but i'm pretty sure the answer is simple. 我不知道,但是我很确定答案很简单。 Any idea ? 任何想法 ?


I got the color figured out. 我弄明白了颜色。 All i needed to do was multiply then divide instead of divide the multiply. 我要做的就是乘然后除而不是乘。 (ie) (即)

//Before
green = 255 * Math.sqrt( Math.cos ( power * Math.PI / 200 ));
red = 255 * Math.sqrt( Math.sin ( power * Math.PI / 200 ));

//After
green = 255 * Math.sqrt( Math.cos ( power / Math.PI * 200 ));
red = 255 * Math.sqrt( Math.sin ( power / Math.PI * 200 ));

For the test, I've changed your application slightly, into an simple Java Application. 为了进行测试,我将您的应用程序稍作更改,变成了一个简单的Java应用程序。 I tested this program by emulating the input from textField (which is modified to be given by the argument of print100 method). 我通过模拟textField的输入(已修改print100方法的参数指定)来测试该程序。

However, the program gives a normal output from 1 to 100, without skipping odd numbers, even 12 is given as an argument to print100 method. 但是,该程序将正常输出从1到100,而不会跳过奇数,甚至将12用作print100方法的参数。

Anyway, you should change the last line of the colorOnDigit into return "<font color = \\"" + hex + "\\">"; 无论如何,您应该将colorOnDigit的最后一行更改为return "<font color = \\"" + hex + "\\">"; . Closing double-quotation mark (which is should be included in the resultant HTML tag) is missing. 缺少双引号(应包含在结果HTML标记中)。 I think maybe it's the reason that odd tags are missing in your output. 我认为这可能是输出中缺少奇数标记的原因。

public class OneHundred {

    /**
    * leave specific number of digit after the dot
    * return myNum
    */
    private static double doMath(int i, int num) {
        double myNum = ((double) i / num) * 100;
        int precision = 100; //Number of zero = number of digits
        myNum = Math.floor(myNum * precision + .5) / precision;
        return myNum;
    }

    /**
     * Color from red to green according to the result
     * @param a
     * @return a haxaecimal to color the answer
     */
    private static String colorOnDigit(double a) {
        double green, red;
        int g, r;
        double power = a;
        int blue = 0;

        green = 255 * Math.sqrt( Math.cos ( power * Math.PI / 200 ));
        red = 255 * Math.sqrt( Math.sin ( power * Math.PI / 200 ));

        int precision = 10; //Number of zero = number of digits
        green = Math.floor(green * precision + .5) / precision;
        red = Math.floor(red * precision + .5) / precision;

        r = (int) red;
        g = (int) green;

        String hex = String.format("#%02x%02x%02x", r, g, blue);

        System.out.println("blue " + blue);
        System.out.println("Green " + green);
        System.out.println("Red " + red);
        System.out.println("----------");
        return "<font color = \"" + hex + "\">";
    }

    /**
     * convert the number to string 
     * @param i
     * @return a string that contains the information
     */
    private static String appendString(StringBuilder b, int i, int input){
        double a = doMath(i, input /* assumed an arbitrary input*/ );

        String s = "<br>" + colorOnDigit(a) + i + " : " + a + "</font>\n";

        return b.append(s).toString();
    }

    private static String print100(int num) {
        StringBuilder text = new StringBuilder();

        for (int i = 1; i < 100 + 1; i++) {
            appendString(text, i, num);
        }

        return text.toString();
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        String l = print100(7);
        System.err.println(l);
    }

}

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

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