Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/332.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
无法将Python代码转换为Java_Java_Python - Fatal编程技术网

无法将Python代码转换为Java

无法将Python代码转换为Java,java,python,Java,Python,任何了解Python和Java的人都可以在这里帮助我 我只是想把一个简单的程序从我知道如何用Python编写的东西转换成Java,但我显然做了一些简单的错误 以下是python代码: def correct(a, b, c): if a + b == 6 and a**2 + b**2 + c**2 == 18: return True else: return False def show(a, b, c): print("a is",

任何了解Python和Java的人都可以在这里帮助我

我只是想把一个简单的程序从我知道如何用Python编写的东西转换成Java,但我显然做了一些简单的错误

以下是python代码:

def correct(a, b, c):
    if a + b == 6 and a**2 + b**2 + c**2 == 18:
        return True
    else:
        return False

def show(a, b, c):
    print("a is", a, "\nb is", b, "\nc is", c)

def test(a=1, b=1, c=1):
    """Find the value of c, where:
    a + b = 6 and a*a + b*b + c*c = 18
    """
    for i in range (-10, 10):
        a = i
        if correct(a, b, c):
            show(a, b, c)
            return
        for j in range(-10, 10):
            b = j
            if correct(a, b, c):
                show(a, b, c)
                return
            for k in range(-10, 10):
                c = k
                if correct(a, b, c):
                    show(a, b, c)
                    return
    print("Nothing worked...")

test()
输出:

a is 3   
b is 3   
c is 0  
"Nothing worked..."
下面是我对Java版本的尝试:

private class test {

    /* Find the value of c, where:
     * a + b = 6 AND a*a + b*b + c*c = 18
     */

    public static void main(String[] args) {

        int a = 1;
        int b = 1;
        int c = 1;

        for(int i = -10; i == 10; i++) {
            a = i;
            if (correct(a, b, c)) {
                show(a, b, c);
            }
            for (int j = -10; j == 10; j++) {
                b = j;
                if (correct(a, b, c)) {
                    show(a, b, c);
                }
                for (int k = -10; k == 10; k++) {
                    c = k;
                    if (correct(a, b, c)) {
                        show(a, b, c);
                    }
                }
            }
            }       
        System.out.println("Nothing worked...");
        }


    public static void show(int a, int b, int c) {
        System.out.printf("a is: %s\nb is: %s\nc is: %s", a, b, c);
    }

    private static boolean correct(int a, int b, int c) {
        if (a + b == 6 && a*a + b*b + c*c == 18) {
            return true;
            }
        else {
            return false;
        }
    }
}
输出:

a is 3   
b is 3   
c is 0  
"Nothing worked..."
谁能帮我澄清一下吗


非常感谢。

看来你进步很大。跳出的一个问题是,如果这是真的,您将for循环中的第二个表达式视为stop。所以你在想:

for (int i = -10; i == 10; i++) {
将继续循环,直到i==10为真。但事实上,它一直在循环,而这是真的。所以它永远不会循环,因为我从-10开始,然后检查条件i==10,发现为false,循环永远不会运行

所以我们想用不同的方式来表述这个条件:

for (int i = -10; i < 10; i++) {
// ---------------^^^^^^
现在它在我<10时运行。当我10岁时,它停止了,因为我<10不再是真的


其他循环也是如此。

请将其表述为一个完整的循环。解释代码应该做什么。解释你期望它做什么。解释您试图编写的代码是如何失败的。你最终要求社区为你解决这个问题,这是一个不公平的要求;i==10;i++循环将运行吗?您只需要在最里面的循环中进行检查,该循环已经覆盖了所有的组合。你可以只使用i,j和k,而不需要a,b和c。非常感谢!这正是我需要的。非常感谢。学习还在继续。