gpt4 book ai didi

java - 函数仅返回偶数

转载 作者:行者123 更新时间:2023-12-01 12:20:22 25 4
gpt4 key购买 nike

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

这是代码

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

最佳答案

为了测试,我将您的应用程序稍微更改为一个简单的 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);
}

}

关于java - 函数仅返回偶数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26707639/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com