本文目录一览:
java if用法
if语法:
if(条件){
代码块;
} else {
代码块;
}
用法举例:
public class simple {
public static void main(String[] args) {
int score = 91; // 张浩的成绩
if (score > 95) {
System.out.println("老师说:不错,奖励一个MP4!");
}
if (score == 95) {
System.out.println("老师说:惩罚进行编码!");
}
}
}
按照需要,画出流程图来分析下问题,如下图所示:
java中的if语句
原因:没有加 else
修改后代码:
public class Test {
public static void main(String[] args) {
/* if语句的第一种格式: if(条件表达式) { 执行语句; } */
int x = 1;
if (x > 1) {
System.out.println("Yes"); // 如果为真,则输出“Yes”
} else {
System.out.println("over");
}
}
}
运行结果:
java 中if的用法
有图吗?怎么看不到。
try {
int num = Integer.parseInt(a);
if (num > 7) {
System.out.println("hello java");
} else if (num < 7) {
System.out.println("hello world");
}
} catch (Exception e) {
System.out.println("字符串a的格式错误");
}
在java中if有什么用?
public class IfDemo {
public static void main(String[] args) throws Exception {
// 用法一:if(条件){.执行代码块.}
// 符合条件,就执行代码块
int index = 0;
while (true) {
// 如果 index等于10,那么跳出循环
if (index == 10) {
break;
}
index++;
}
System.out.println(index);
// 用法二:if(条件){.执行代码块1.}else{.执行代码块2.}
// 符合条件就执行代码块1.不符合条件就执行代码块2
if (index < 0) {
System.out.println("小于0");
} else {
System.out.println("大于0");
}
// 用法三:if(条件1){.执行代码块1.}else if(条件2){.执行代码块2.}else{.执行代码块3.}
// 符合条件1就执行代码块1,符合条件2 就执行代码块2,不符合条件1和条件2,就执行代码块3
int score = 85;
if (score >= 90) {
System.out.println("优秀");
} else if (score < 90 && score >= 80) {
System.out.println("良好");
} else if (score < 80 && score >= 60) {
System.out.println("及格");
} else {
System.out.println("不及格");
}
}
}
谁能告诉我java中的if用法
单独一个 if
就是 if
的判断,即:
if (判断) {
// 上面的判断成立,则执行此大括号包住的语句
}
而第二个 if-else
,就是 if-else
的配对。一个 if
最多只有一个 else
配对:
if (判断) {
// 上面的判断成立,则执行此大括号包住的语句。不成立则直接跳过此括号见的语句
} else {
// 上面的判断不成立,则执行此大括号包住的语句
}
第三个是嵌套:
if (判断1) {
// 上面的判断1成立,则执行此大括号包住的语句。不成立则直接跳过此括号见的语句
} else if (判断2) {
// 判断1不成立 判断2成立,则执行此大括号包住的语句
}
JAVA的If语句?
原因:没有加 else
修改后代码:
public class Test {
public static void main(String[] args) {
/* if语句的第一种格式: if(条件表达式) { 执行语句; } */
int x = 1;
if (x > 1) {
System.out.println("Yes"); // 如果为真,则输出“Yes”
} else {
System.out.println("over");
}
}
}
运行结果: