01键盘扫描仪
案例及练习:
import java.util.Scanner;
/**
* 提示用户输入姓名,年龄
* @author liyuqi
* @version 1.0
* @date 2024/1/23
*/
public class IdentifyDemo01 {
public static void main(String[] args) {
//键盘扫描器
//创建一个Scanner类的引用Scanner,System代表可以看成一个键盘
Scanner sc = new Scanner(System.in);
System.out.println("请输入你的姓名:");
String name = sc.next();
System.out.println("请输入你的年龄:");
int age = sc.nextInt();
System.out.println("我叫" + name + ",我今年" + age + "岁了。");
}
}
class Practice{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入您的学号:");
long sno = sc.nextLong();
System.out.println("请输入您的班级:");
int clas = sc.nextInt();
System.out.println("请输入您的座右铭:");
String slogan = sc.next();
System.out.println("我的学号是" + sno + ",我来自于" + clas + "班,我的座右铭是" + slogan);
}
}
class Practice1{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println("请输入下落时间");
int t = sc.nextInt();
double x;
x = 0.5 * 9.8 * t * t;
System.out.println("下落位移为" + x);
}
}
02数字的加减乘除取余运算
案例及练习:
package com.operator;
/**
* @author liyuqi
* @version 1.0
* @date 2024/1/23
*/
public class ArithmaticDemo01 {
public static void main(String[] args) {
//加减乘除取余
int a = 12;
int b = 5;
System.out.println(a + b);
System.out.println(a - b);
System.out.println(a * b);
System.out.println(a / b);
System.out.println(a % b);
}
}
package com.operator;
/**
* @author liyuqi
* @version 1.0
* @date 2024/1/23
*/
public class AddDemo01 {
public static void main(String[] args) {
int a = 3;
int b = a++;
System.out.println("b=" + b);// 3
int c = ++a;
System.out.println("c=" + c);//5
}
}
03三目运算符
案例及练习
package com.operator;
import java.util.Scanner;
/**
* @author liyuqi
* @version 1.0
* @date 2024/1/23
*/
public class ThreeEyesDemo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("属于一个数:");
int number = sc.nextInt();
//三目运算符判断数
String s = number > 0 ? "该数为正数" : number == 0 ? "该数为0" : "该数为负数";
System.out.println(s);
}
}
class Practice{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("输入一个三位数");
int number = scanner.nextInt();
int a = number % 10;
int b = (number % 100) / 10;
int c = number / 100;
System.out.println(a * 100 + b * 10 + c);
}
}
04循环
1. if循环
package com.operator;
import java.util.Scanner;
/**
* @author liyuqi
* @version 1.0
* @date 2024/1/23
*/
public class IfDemo01 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入年龄:");
int age = sc.nextInt();
if (age >= 18) {
System.out.println("好!!!!!!!");
} else {
System.out.println("马上就好!!!!");
}
}
}
package com.operator;
import java.util.Scanner;
/**
* @author liyuqi
* @version 1.0
* @date 2024/1/23
*/
public class IfDemo02 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("输入第一个数:");
int num1 = sc.nextInt();
System.out.println("输入第二个数:");
int num2 = sc.nextInt();
int max = num1;
if (num1 < num2) {
max = num2;
}
System.out.println("最大数为" + max);
}
}
package com.operator;
import java.util.Scanner;
/**
* @author liyuqi
* @version 1.0
* @date 2024/1/23
*/
public class IfElseDemo {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入你的成绩:");
double score = scanner.nextDouble();
if (score >= 60) {
System.out.println("恭喜你,及格了");
} else {
System.out.println("恭喜你,补考一下");
}
}
}
package com.operator;
import java.util.Scanner;
/**
* @author liyuqi
* @version 1.0
* @date 2024/1/23
*/
public class IfElseIfElseDemo01 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入你的身份信息:(军人,学生,还是普通人)");
String identify = scanner.next();
//对用户输入的身份信息进行判断
if ("军人".equals(identify)) {
System.out.println("由于您是军人,享受免票");
} else if ("学生".equals(identify)) {
System.out.println("由于您是学生,享受半价优惠");
} else {
System.out.println("由于你啥也不是,请购买全价票");
}
}
}
class Practice1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("输入高考成绩:");
int grade = sc.nextInt();
if (grade >= 650) {
System.out.println("清北");
} else if (grade >= 550) {
System.out.println("985,211");
} else if (grade >= 450) {
System.out.println("一本");
} else if (grade >= 350) {
System.out.println("二本");
} else {
System.out.println("啊哈???");
}
}
}
2. switch循环
案例
import java.util.Scanner;
/**
* @author liyuqi
* @version 1.0
* @date 2024/1/23
*/
public class SwitchCaseDemo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入你的业绩名次:");
int num = sc.nextInt();
switch (num) {
case 1:
System.out.println("欧洲游");
break;
case 2:
System.out.println("亚洲游");
break;
case 3:
System.out.println("国内游");
break;
case 4:
System.out.println("省内游");
break;
default:
System.out.println("加班!!!");
}
}
}
练习
import java.util.Date;
import java.util.Scanner;
/**
* @author liyuqi
* @version 1.0
* @date 2024/1/23
*/
public class YearDemo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("输入月份:");
int year = sc.nextInt();
switch (year) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
System.out.println("31");
break;
case 4:
case 6:
case 9:
case 11:
System.out.println("30");
break;
case 2:
System.out.println("28");
break;
default:
System.out.println("你要起飞啊");
}
}
}
3. for循环
案例
/**
* @author liyuqi
* @version 1.0
* @date 2024/1/23
*/
public class ForDemo {
public static void main(String[] args) {
for (int i = 0; i < 100; i = i + 2) {
System.out.print(i + " ");
}
}
}
练习
/**
* @author liyuqi
* @version 1.0
* @date 2024/1/23
*/
public class DoubleForDemo01 {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= 5; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
class Practice2 {
public static void main(String[] args) {
for (int i = 1; i < 10; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(j + "*" + i + "=" + i * j + " ");
}
System.out.println("");
}
}
}
4. while循环
import java.util.Scanner;
/**
* 跑圈
* @author liyuqi
* @version 1.0
* @date 2024/1/23
*/
public class WhileDemo {
public static void main(String[] args) {
int i = 1;
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("你已经跑了第" + i + "圈,是否继续");
String str = sc.next();
if ("是".equals(str)) {
System.out.println("继续,正在跑第" + (i + 1) + "圈");
i++;
} else if ("否".equals(str)) {
System.out.println("不行了,跑不动了");
break;
}
}
}
}
do-whlie循环
/**
* @author liyuqi
* @version 1.0
* @date 2024/1/23
*/
public class DoWhileDemo {
public static void main(String[] args) {
int i = 1;
do {
System.out.println(i + " ");
i++;
} while (i <= 0);
}
}