分支语句-if
目标 : 今后的需求, 只要是涉及到判断, 就要联想到 if 语句
package com.wanxi.test;
import java.util.Scanner;
//键盘录入用户名字、密码,如果名字为陈明、密码为111111,
// 程序输出正确,否则输出错误。
public class UsernamePassword {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入你的用户名: ");
String name = scanner.next();
System.out.print("请输入你的6位数密码: ");
int password = scanner.nextInt();
String userName = "陈明";
int userPassword = 111111;
if(name.equals(userName)&& password==userPassword){
System.out.println("正确,请进入");
}else {
System.out.println("输入错误");
}
scanner.close();
}
}
package com.wanxi.test;
//考试奖励
import java.util.Scanner;
public class ExamRewards {
public static void main(String[] args) {
int scores1 = 100;
int scores2 = 95;
int scores3 = 90;
int scores4 = 80;
Scanner scanner = new Scanner(System.in);
System.out.println("请输入成绩: ");
int scores = scanner.nextInt();
if (scores >= scores2 && scores <= scores1) {
System.out.println("自行车");
return;
}
if (scores >= scores3 && scores < scores2) {
System.out.println("城堡玩具");
return;
}
if (scores >= scores4 && scores < scores3) {
System.out.println("机器人玩具");
return;
}
if (scores < scores4 || scores > scores1) {
System.out.println("打一顿");
return;
}
scanner.close();
}
}
switch语句
注意事项 :
case 后面的值不允许重复
case 后面的值只能是常量, 不能是变量
switch () 中可以接收的类型
基本数据类型 : byte short char int
引用数据类型 : jdk5版本开始可以是枚举, jdk7版本开始可以是String字符串
如果switch语句, 省略了break, 会开启case穿透现象
使用选择 :
if : 适用于范围性的判断查找
switch : 适用于固定值的匹配
package com.wanxi.test;
//计算器
import java.util.Scanner;
public class Calculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入操作(+-*/): ");
String operate = scanner.next();
System.out.print("请输入第一个数字: ");
int num1 = scanner.nextInt();
System.out.print("请输入第二个数字: ");
int num2 = scanner.nextInt();
switch (operate){
case "+":
System.out.println("num1 + num2 = " + (num1 + num2));
break;
case "-":
System.out.println("num1 - num2 = " + (num1 - num2));
break;
case "*":
System.out.println("num1 * num2 = " + (num1 * num2));
break;
case "/":
System.out.println("num1 / num2 = " + (num1 / num2));
break;
default:
System.out.println("输入有误");
}
scanner.close();
}
}
for循环
* 目标 : 为什么要学习循环语句
* 回答 : 程序中有些逻辑代码, 需要重复的执行很多次, 就应该使用循环
package com.wanxi.test;
//10~1的倒计时
public class Countdown {
public static void main(String[] args) throws InterruptedException {
for (int i=10; i >= 0 ; i--){
Thread.sleep(1000);
System.out.println(i);
}
}
}
package com.wanxi.test;
//找出水仙花数
public class NumberOfDaffodils {
public static void main(String[] args) {
int num = 0;
for (int i = 100; i < 1000; i++) {
int ge = i % 10;
int shi = i / 10 % 10;
int bai = i / 100 % 10;
if (ge * ge * ge + shi * shi * shi + bai * bai * bai == i) {
System.out.println(i);
num++;
}
}
System.out.println("水仙花数个数为: " + num);
}
}
package com.wanxi.test;
//使用双循环输出正方形
public class Square {
public static void main(String[] args) {
for (int i = 1; i <= 4; i++) {
for (int j = 1; j <= 5; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
while循环
package com.wanxi.test;
//1~100的奇数,及奇数和
public class SumAdd {
public static void main(String[] args) {
int i = 1;
int sum = 0;
while (i <= 100) {
if (i % 2 != 0) {
System.out.println(i);
sum += i;
}
i++;
}
System.out.println(sum);
}
}
Random产生随机数
package com.wanxi.test;
import java.util.Random;
//random.nextInt(100)随机生成【0 100)的数,不包含100
//random.nextInt(100)+1随机生成【1 101)的数,包含100
//random.nextInt(6)随机生成【0 6)的数,不包含6
//random.nextInt(6)+1随机生成【1 7)的数,包含6
//随机生产【a b)之间的数
//random.nextInt(b-a)+a
public class MyRandom {
public static void main(String[] args) {
Random random = new Random();
for (int i=1; i<=4;i++){
System.out.println(random.nextInt(5-3)+3);//【3 5)
}