我的程序应该打印给定数字的所有百分比。它工作正常,直到我添加一个颜色结果的功能(红色低,绿色高)。现在它只打印奇数或偶数,但不能同时打印两者。至于着色,它从绿色到红色向后工作。我希望打印所有结果,并根据它们的值进行着色。
这是代码
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
中输入的确切数字。
我没有在谷歌和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 ));
答案 0 :(得分: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);
}
}