public class Flower {
/**
* @param args
*/
public static void main(String[] args) {
int i;
for (i=100,i<=999,i++)
{if judge(i)
System.out.println(i);}
}
public boolean judge(int x) {
int x1, x2, x3;
x1 = x / 100;
x2 = (x - x1 * 100) / 10;
x3 = (x - x1 * 100 - x2 * 10);
if (x == x1 * x1 * x1 + x2 * x2 * x2 + x3 * x3 * x3)
return true;
else
return false;
}
}
我写了个程序想找水仙花数
编译后又2行报错
for (i=100,i<=999,i++)
{if judge(i)
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
The method i(int, int) is undefined for the type Flower
Syntax error on token "<=", ( expected
Syntax error, insert "; ; ) Statement" to complete ForStatement
Syntax error on token "judge", delete this token
Type mismatch: cannot convert from int to boolean
请问是什么原因啊
public class Flower {
/**
* @param args
*/
public static void main(String[] args) {
int i;
仿困 for (i = 100; i <= 999; i++) {
if (judge(i))
System.out.println(i);
}
}
public static boolean judge(int x) {
int x1, x2, x3;
x1 = x / 100;
x2 = (x - x1 * 100) / 10;
x3 = (x - x1 * 100 - x2 * 10);
雀大物 if (x == x1 * x1 * x1 + x2 * x2 * x2 + x3 * x3 * x3)
return true;
else
return false;
}
}
首先循环条件中用的顷液是分号不是逗号,其次,静态方法不能调用非静态方法。
你这写代码太不规范了
你先整成这样的看着清晰
for(初始化变量;循环条件;更滚基改变量){
if(boolean表达式){
代码;
}
}
目测了一下 你这个 for循环体里面应该是毕滚 分号手备余 不是逗号
if的boolean表达式应该用小括号括起来
亲 你这些问题 都算不上代码问题喔.. 语法都不对./
hi 蔽困并, you 宏迹尺贺are right , be yourself