2014-11-03 88 views
0

我的程序应该打印给定数字的所有百分比。它工作正常,直到我添加一个函数,结果颜色(红色低和绿色高)。现在它只打印奇数或偶数,但不是两个。至于着色,它从绿色变为红色。我希望所有结果都能够根据其价值进行打印和着色。函数只返回偶数

下面的代码

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()); 
    } 
} 

}

当我打电话System.out.print()它有我在JTextField输入的确切数量。

我没有在Google上找到任何答案,也没有在StackOverflow上找到答案。我无法弄清楚,但我确信答案很简单。任何想法 ?


我弄明白了颜色。我所需要做的就是繁殖然后分而不是分乘。 (即)

//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)); 
+1

如果事情在以前有效,为什么不直接比较两个版本,看看在添加颜色更改时无意中引入了哪些更改? – MarsAtomic 2014-11-03 04:10:30

+0

@mars我只添加了函数'colorOnDigit',然后它被窃听。 – Chax 2014-11-03 04:12:51

+0

当人们不知道代码应该做什么时,很难告诉别人什么是代码中断。你没有真正解释该计划的目的是什么。它试图解决什么问题。业务规则是什么?等请澄清。 – hfontanez 2014-11-03 04:17:27

回答

1

为了试验,我已经略有改变您的应用程序,到一个简单的Java应用程序。我通过模拟来自textField(它被修改为由print100方法的参数给出)的输入来测试该程序。

然而,该程序给出了从1到100的正常输出,没有跳过奇数,即使将12作为print100方法的参数给出。

无论如何,您应该将colorOnDigit的最后一行更改为return "<font color = \"" + hex + "\">";。关闭双引号(应包含在生成的HTML标记中)缺失。我想也许这是你的输出中缺少奇数标签的原因。

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); 
    } 

} 
+0

你是对的,这只是一个“缺失,我现在感觉被推迟了:P。谢谢很多的答案。 – Chax 2014-11-03 05:07:46

+0

不客气,我非常高兴我的帮助。 – 2014-11-03 05:15:44