Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/373.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 函数只返回偶数_Java_For Loop_Colors - Fatal编程技术网

Java 函数只返回偶数

Java 函数只返回偶数,java,for-loop,colors,Java,For Loop,Colors,我的程序应该打印给定数字的所有百分比。在我添加一个为结果着色的函数(红色为低,绿色为高)之前,它工作得很好。现在它只打印奇数或偶数,但不能同时打印两者。至于颜色,它是反向工作的,从绿色到红色。我希望所有的结果打印出来,并根据它们的价值进行着色 这是密码 public class Window extends JFrame implements ActionListener{ private JButton theButton = new JButton("Calculer sur 100");

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

这是密码

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

为了测试,我将您的应用程序稍微更改为一个简单的Java应用程序。我通过模拟
textField
中的输入来测试这个程序(修改为
print100
方法的参数)

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

无论如何,您应该将colorOnDigit的最后一行更改为
return”“。缺少结束双引号(应该包含在结果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);
    }

}
公共类100{
/**
*在点后留下具体数字
*返回八哥
*/
私有静态双域(inti,intnum){
双myNum=((双)i/num)*100;
int precision=100;//零位数=位数
myNum=数学地板(myNum*精度+0.5)/精度;
返回myNum;
}
/**
*根据结果将颜色从红色变为绿色
*@param a
*@返回一个haxaecimal来着色答案
*/
专用静态字符串colorOnDigit(双a){
重瓣绿色,红色;
int g,r;
双功率=a;
int蓝色=0;
绿色=255*Math.sqrt(Math.cos(power*Math.PI/200));
红色=255*Math.sqrt(Math.sin(power*Math.PI/200));
int precision=10;//零位数=位数
绿色=数学地板(绿色*精度+0.5)/精度;
红色=数学地板(红色*精度+0.5)/精度;
r=(int)红色;
g=(int)绿色;
字符串十六进制=String.format(#%02x%02x%02x),r,g,蓝色);
系统输出打印项次(“蓝色”+蓝色);
System.out.println(“绿色”+绿色);
系统输出打印项次(“红色”+红色);
System.out.println(“--------------”;
返回“”;
}
/**
*将数字转换为字符串
*@param i
*@返回包含信息的字符串
*/
私有静态字符串appendString(StringBuilder b、int i、int input){
double a=doMath(i,input/*假设为任意输入*/);
字符串s=“
”+colorOnDigit(a)+i+:“+a+”\n”; 返回b.append.toString(); } 私有静态字符串print100(int num){ StringBuilder text=新的StringBuilder(); 对于(int i=1;i<100+1;i++){ 追加字符串(文本,i,num); } 返回text.toString(); } /** *@param args */ 公共静态void main(字符串[]args){ 字符串l=print100(7); 系统错误println(l); } }
如果以前一切正常,为什么不把这两个版本区别开来,看看你在添加颜色变化时无意中引入了什么变化呢?@mars我只添加了函数
colorOnDigit
,然后它就被窃听了。当一开始不知道代码应该做什么时,很难告诉别人代码中有什么问题。你没有真正解释这个计划的目的。它试图解决什么问题。商业规则是什么?等等。请澄清。@hfontanez我的程序应该打印一组的所有百分比。第一行。但更明确地说,它需要一个给定的数字,它给出了集合中所有的百分比(这有点难以解释,因为我的母语不是英语)。我把我的问题编辑得更具体一些。我找到了解决方案的一部分,但我不知道如何解决它。有时绿色或红色的值为NaN。根据它的意思,这不是一个数字。所以我可能在做一些我不应该做的算术。现在问题在哪里。你是对的,只是少了一个\。我现在觉得自己有点迟钝:P。非常感谢你的回答。不客气。我真的很高兴我能帮上忙。
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);
    }

}