public class ifElse {
static int test(int testval, int target) {
int result = 0;
if(testval > target)
result = -1;
else if(testval < target)
result = 1;
else
return result;
}
public static void main(String [] args) {
System.out.println(test(5, 10));
System.out.println(test(5, 10));
System.out.println(test(5, 5));
}
}
static int test(int testval, int target) {
int result = 0;
if(testval > target)
result = -1;
else if(testval < target)
result = 1;
else
return result;
}
public static void main(String [] args) {
System.out.println(test(5, 10));
System.out.println(test(5, 10));
System.out.println(test(5, 5));
}
}
static int test(int testval, int target) {
int result = 0;
if(testval > target){
result = -1;
}else if(testval < target){
result = 1;
}
return result;
}
public static void main(String [] args) {
System.out.println(test(5, 10));
System.out.println(test(5, 10));
System.out.println(test(5, 5));
}
}
if (testval > target)
return -1;
else if (testval < target)
return 1;
else
return 0;
}在 test 中 result 好像没有什么必要的,如果真的要的话把你原来的 test() 中 return result; 前的那个else擦掉就可以了。
static int test(int testval, int target) {
if(testval > target)
return -1;
else if(testval < target)
return +1;
else
return 0;
}
public static void main(String [] args) {
System.out.println(test(10, 5));
System.out.println(test(5, 10));
System.out.println(test(5, 5));
}
}
在这个里面为什么没有用加的那几个{}可以运行呢?
static int test(int testval, int target) {
if(testval > target)
return -1;
else if(testval < target)
return +1;
else
return 0;
}
}
在这个里面为什么没有用加的那几个{}可以运行呢?==============================================因为这里的if和else语句里只有一句语句,所以可以不用加 { } 呀
如果其中有超过一条语句的话,就必须加上 { } 否则可能得到不对的结果。建议:无论里面有多少语句都加上 { } 这样有利于代码的可读性。* * * *另外顺便说一句,public class ifElse 中的“ifElse”改为“IfElse”(类名使用大写字母开头),虽然用小写没有错,但这样比较符合规范。
多么好的人啊 咯咯
int result;
if(testval > target)
result=-1;
else if(testval < target)
result=1;
else
result=0;
return result;
}
public static void main(String [] args) {
System.out.println(test(5, 10));
System.out.println(test(5, 10));
System.out.println(test(5, 5));
}
}