Java基础学习笔记-2
前言
在计算机编程领域,条件语句和控制流结构是构建程序逻辑的基本组成部分。它们允许程序员根据不同的条件执行不同的操作,从而使程序更加灵活和智能。本文将深入探讨Java编程语言中的条件语句和控制流,提供了一系列实用的示例和技巧,帮助读者更好地理解和运用这些概念。
Java基础学习笔记-1
1. if语句
if
语句是最基本的条件语句,它允许您根据给定条件执行不同的代码块。
import java.util.Scanner;public class Demo01 {public static void main(String[] args) {Scanner scanner = new Scanner(System.in);System.out.print("请输入成绩:");int score = scanner.nextInt();if (score >= 90) {System.out.println("优秀");} else {System.out.println("其它");}}
}
在上面的示例中,根据输入的成绩,程序将输出"优秀"或"其它"。
2. if-else语句
if-else
语句允许您在条件不满足时执行备选代码块。
import java.util.Scanner;public class Demo02 {public static void main(String[] args) {Scanner scanner = new Scanner(System.in);System.out.print("请输入成绩:");int score = scanner.nextInt();if (score >= 90) {System.out.println("奖励");}System.out.println("程序结束");}
}
在上面的示例中,如果成绩大于等于90分,将输出"奖励",否则程序将继续执行后续代码。
3. 多重条件语句
Java允许您使用多重条件语句来处理多个条件。
public class Demo03 {public static void main(String[] args) {int score = 88;if (score >= 90) {System.out.println("优秀");} else if (score >= 80) {System.out.println("良好");} else if (score >= 70) {System.out.println("中等");} else if (score >= 60) {System.out.println("及格");} else {System.out.println("差");}}
}
在上面的示例中,根据成绩的不同范围,程序将输出不同的评级。
4. 简化的多重条件语句
您还可以使用简化的多重条件语句,减少代码的复杂性。
public class Demo04 {public static void main(String[] args) {int score = 98;if (score >= 90) System.out.println("优秀");else if (score >= 80) System.out.println("良好");else if (score >= 70) System.out.println("中等");else if (score >= 60) System.out.println("及格");else System.out.println("差");}
}
在上面的示例中,我们使用了更简洁的语法来达到相同的效果。
5. 逻辑运算符
逻辑运算符允许您在条件中组合多个条件。
public class Demo05 {public static void main(String[] args) {int java = 89, html = 90;if (java >= 90 || html >= 90) System.out.println("去动物园游玩");else System.out.println("在家休息");}
}
在上面的示例中,我们使用逻辑或运算符 ||
来判断是否有一门课程达到了90分以上。
6. switch语句
switch
语句允许您根据不同的取值执行不同的代码块。
import java.util.Scanner;public class Demo06 {public static void main(String[] args) {Scanner scanner = new Scanner(System.in);System.out.println("> 幸运抽奖\n");System.out.print("请输入4位会员号:");int id = scanner.nextInt();int baiwei = id / 100 % 10;int luck = (int)(Math.random() * 10);System.out.println("幸运数字是:" + luck);if (baiwei == luck) System.out.println("奖励mp3一个。");else System.out.println("谢谢惠顾。");}
}
在上面的示例中,我们使用了switch
语句根据不同的条件执行不同的代码。
7. 字符串比较
在Java中,要比较字符串的内容,您应该使用equals()
方法。
import java.util.Scanner;public class Demo07 {public static void main(String[] args) {Scanner scanner = new Scanner(System.in);System.out.print("请输入是否是会员(y/n):");String yesno = scanner.next();if (yesno.equals("y")) {System.out.println("是会员");} else {System.out.println("不是会员");}}
}
在上面的示例中,我们使用equals()
方法来比较用户输入的字符串是否等于"y"。
8. 条件嵌套
条件语句可以嵌套,以处理更复杂的逻辑。
import java.util.Scanner;public class Demo08 {public static void main(String[] args) {Scanner scanner = new Scanner(System.in);System.out.println("请输入是否是会员(y/n):");String yesno = scanner.next();System.out.println("请输入购物金额:");int money = scanner.nextInt();double zk = 1.0; // 默认值,没有折扣if (yesno.equals("y")) {if (money >= 200) zk = 0.75;else zk = 0.8;} else {if (money >= 100) zk = 0.9;}System.out.println("实际支付:" + money * zk);}
}
在上面的示例中,我们展示了条件语句的嵌套用法,根据会员状态和购物金额来计算实际支付金额。
9. switch语句的字符串支持
Java 7及更高版本支持使用字符串作为switch
语句的条件。
public class Demo10 {public static void main(String[] args) {String mingci = "1";switch (mingci) {case "1":System.out.println("夏令营");break;case "2":System.out.println("笔记本一台");break;case "3":System.out.println("移动硬盘");break;default:System.out.println("无");break;}}
}
在上面的示例中,我们使用字符串作为switch
语句的条件来根据不同的字符串值执行不同的代码。
10. switch语句的透传现象
switch
语句中的透传现象允许多个case
值共享相同的代码块。
public class Demo11 {public static void main(String[] args) {String mingci = "星期一";switch (mingci) {case "星期一":case "星期三":System.out.println("画画");break;case "星期二":System.out.println("休息");break;case "星期四":System.out.println("休息");break;case "星期五":System.out.println("移动硬盘");break;case "星期六":case "星期天":System.out.println("街舞");break;default:System.out.println("错误值");break;}}
}
在上面的示例中,星期一和星期三共享相同的活动"画画",星期六和星期天共享"街舞"活动。
11. 条件语句与用户交互
条件语句通常与用户输入一起使用,以根据用户的选择执行不同的操作。
import java.util.Scanner;public class Demo12 {public static void main(String[] args) {Scanner scanner = new Scanner(System.in);System.out.print("请输入消费金额:");double money = scanner.nextDouble();System.out.println("是否换购:");System.out.println("1.满50元,加2元换购商品1");System.out.println("2.满100元,加3元换购商品2");System.out.println("3.满100元,加10元换购商品3");System.out.println("4.满200元,加10元换购商品4");System.out.println("5.满200元,加20元换购商品5");System.out.println("0.不换购");System.out.print("请选择:");int select = scanner.nextInt();String goods = "不换购"; // 默认值switch (select) {case 1:if (money > 50) {money += 2;goods = "商品1";}break;case 2:if (money > 100) {money += 3;goods = "商品2";}break;case 3:if (money > 100) {money += 10;goods = "商品3";}break;case 4:if (money > 200) {money += 10;goods = "商品4";}break;case 5:if (money > 200) {money += 20;goods = "商品5";}break;}System.out.println("消费金额:" + money);System.out.println("成功换购:" + goods);}
}
在上面的示例中,根据用户输入的消费金额和选择,程序计算出最终的支付金额和换购商品。
总结
通过本文的阅读,我们深入研究了Java编程语言中的条件语句和控制流。从最基本的if语句到更复杂的多重条件语句和字符串比较,我们探讨了如何根据不同的条件执行代码块。此外,我们还介绍了逻辑运算符的使用和switch语句的应用,以及如何将条件语句与用户交互相结合,使程序更加智能和适应性强。这些概念和示例将有助于读者更好地理解和运用条件语句和控制流,提高他们的编程技能和程序设计能力。