java-if选择结构
2022/2/24 12:51:34
本文主要是介绍java-if选择结构,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
1 package com.xl.struct; 2 3 import java.util.Scanner; 4 5 public class IfDemo01 { 6 public static void main(String[] args){ 7 Scanner scanner = new Scanner(System.in); 8 9 System.out.println("请输入内容: "); 10 String s = scanner.nextLine(); 11 12 13 //equals:判断字符串是否相等 14 if (s.equals("Hello")){//如果输入的字符串等于Hello 就输出 s 15 System.out.println(s); 16 } 17 System.out.println("End"); 18 19 20 scanner.close(); 21 } 22 }
1 package com.xl.struct; 2 import java.util.Scanner; 3 public class IfDemo02 { 4 public static void main(String[] args){ 5 Scanner scanner = new Scanner(System.in); 6 7 System.out.println("请输入成绩: "); 8 9 int score = scanner.nextInt(); 10 11 if(score>60){ 12 System.out.println("及格");//真为 True 13 }else{ 14 System.out.println("不及格");// 假 为 false 15 } 16 17 18 19 20 scanner.close(); 21 22 } 23 }
1 package com.xl.struct; 2 3 import java.util.Scanner; 4 5 public class IfDemo03 { 6 public static void main(String[] args) { 7 Scanner scanner = new Scanner(System.in); 8 System.out.println("请输入成绩: "); 9 int score = scanner.nextInt();//int或double 10 11 /** 12 * if语句至多有一个 else 语句,else 语句在所有的else if 语句之后。 13 * if语句可以有若干个 else 语句,他们必须在 else 语句之前。 14 * 一单其中一个 else if 语句检测为 true,其他的else if 以及 else 语句都将跳过执行 15 */ 16 17 if (score==100){ 18 System.out.println("恭喜满分"); 19 }else if (score<100 && score>=90){ 20 System.out.println("优秀"); 21 }else if (score<90 && score>=80) { 22 System.out.println("良好"); 23 }else if (score<80 && score>=70){ 24 System.out.println("一般"); 25 }else if (score<70 && score>=60){ 26 System.out.println("及格"); 27 }else if (score<60 && score>=0){ 28 System.out.println("不合格"); 29 30 }else { 31 System.out.println("成绩不合法"); 32 } 33 34 scanner.close(); 35 } 36 }
1 package com.xl.struct; 2 3 import java.util.Scanner; 4 5 public class Demo033 { 6 public static void main(String[] args) { 7 int num1,num2;//变量 8 Scanner scanner = new Scanner(System.in); 9 System.out.println("请输入账号"); 10 num1 = scanner.nextInt();//获取第一个的输入 11 System.out.println("请输入密码"); 12 num2 = scanner.nextInt();//获取第二个的输入 13 if (num1==123456){//判断num1是否等于123456 14 System.out.println("账号正确"); 15 if (num2==888888){//判断num2是否等于888888 16 System.out.println("登陆成功"); 17 }else { 18 System.out.println("密码错误"); 19 } 20 }else{ 21 System.out.println("用户名错误"); 22 } 23 24 25 } 26 27 }
这篇关于java-if选择结构的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-09-28微服务架构中API版本控制的实践
- 2024-09-28AI给的和自己写的Python代码,都无法改变输入框的内容,替换也不行
- 2024-09-27Sentinel配置限流资料:新手入门教程
- 2024-09-27Sentinel配置限流资料详解
- 2024-09-27Sentinel限流资料:新手入门教程
- 2024-09-26Sentinel限流资料入门详解
- 2024-09-26Springboot框架资料:初学者入门教程
- 2024-09-26Springboot框架资料详解:新手入门教程
- 2024-09-26Springboot企业级开发资料:新手入门指南
- 2024-09-26SpringBoot企业级开发资料新手指南