1.a++与++a的区别
public class AddDemo01 { public static void main(String[] args) { int a = 3; int b = a++; System.out.println("b = "+b); int c = ++a; System.out.println("c = "+c); } }
2.试验计算功能
public class ArithmaticDemo01 { public static void main(String[] args){ int a = 3 ; int b = 5 ; System.out.println(a + b);//8 System.out.println(b - a);//2 System.out.println(a * b);//15 System.out.println(b / a);//1 System.out.println(b % a);//2 } }
3.比较两个数中的最大值(If语句初应用)
import java.util.Scanner; public class IfDemo { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("请输入第一个数"); int num1 = scanner.nextInt(); System.out.println("请输入第二个数"); int num2 = scanner.nextInt(); int max = num1; if (num1 <= num2) { max = num2; } System.out.println("最大值为: " + max); } }
4.判断一个数是否为整数(If,Else语句应用)
import java.util.Scanner; public class IfElseDemo { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("请输入一个数"); int num = sc.nextInt(); if (num >= 0){ System.out.println("该数为非负数"); }else{ System.out.println("该数为负数"); } } }
5.根据成绩分析学校(IfElse语句应用)
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("恭喜你,可以上211"); } else { if (grade >= 450){ System.out.println("恭喜你,可以上一本"); }else{ if(grade >= 350){ System.out.println("恭喜你,可以上二本"); }else{ System.out.println("考不上大学"); } } } } }
6.三目运算符(倒序输出输入的数字)
import java.util.Scanner; public class ThreeEyesDemo { public static void main(String[] args) { System.out.println("请输入一个数"); Scanner scanner = new Scanner(System.in); // int number = scanner.nextInt(); // String s = number > 0 ? "你输入的是一个整数" : number == 0 ? "你输入的是0" :"你输入的是一个负数"; // System.out.println(s); int a = scanner.nextInt(); System.out.print(a % 10); System.out.print((a % 100) / 10); System.out.print(a / 100); } }
7.双For循环
* @description:打印九九乘法表 */ public class DoubleForDemo { public static void main(String[] args) { for(int i = 1;i <= 9;i++){ for(int j = 1;j <= i;j++ ){ System.out.print(i + "*" + j +"="+ i * j +" "); } System.out.println(); } } }
8.
* @description:练习For循环语句 */ public class ForDemo01 { public static void main(String[] args) { //顺序循环打印1——10数字 /*for(int i = 1;i <= 10;i++){ System.out.print(i + " "); }*/ /*打印1到100之间的偶数 for(int i = 1;i <= 100;i++){ if(i % 2 == 0){ System.out.print(i + " "); } }*/ //测试break与continue for(int i = 1;i < 10;i++){ if(i == 5){ //break; continue; } System.out.print(i + " "); } } }
9.
// 练习1 //提示用户输入姓名和年龄,并打印 public class IdentityDemo01 { // public static void main(String[] args){ // //提示用户输入姓名和年龄 // //键盘扫描器 表示创建一个Scanner类型的引用,指向Scanner类型的对象,其中System。in可以看成一个键盘 // Scanner scanner = new Scanner(System.in); // //提示用户输入姓名和年龄 // System.out.println("请输入你的姓名"); // String name = scanner.next(); // System.out.println("请输入你的年龄"); // int age = scanner.nextInt(); // System.out.println("我叫"+name+" 我今年"+age+"岁了"); // } //练习2 //打印学号,班级,座右铭 // public static void main(String[] args){ // Scanner scanner = new Scanner(System.in); // System.out.println("请输入你的座右铭"); // String name = scanner.next(); // System.out.println("请输入你的学号"); // String sid = scanner.next(); // System.out.println("请输入你的班级"); // String classNumber = scanner.next(); // System.out.println("我的学号是"+sid+" 我的班级是"+classNumber+"我的座右铭是:"+name); // } //编程题:要求用户输入下落时间t并按照公式0.5*9.8*t*t public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("请输入时间"); int time = scanner.nextInt(); System.out.println("路程为"+0.5*9.8*time*time); } }
10.SwitchCase语句
根据输入月份输出有几天(不考虑闰年)
public class SwitchCaseDemo { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("请输入月份"); int month = sc.nextInt(); switch(month){ case 1: case 3: case 5: case 7: case 8: case 10: case 12: System.out.println("31天"); break; case 2: System.out.println("28天"); break; case 4: case 6: case 9: case 11: System.out.println("30天"); break; default: System.out.println("你输入的不是月份"); } } }
11.While语句的应用(跑圈)
public class WhileDemo { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int i = 1; System.out.println("你已经跑了1圈,是否要继续"); while (true){ String str = scanner.next(); if("否".equals(str)){ System.out.println("不行了,跑不动了"); break; }else if("是".equals(str)){ i++; System.out.println("你已经跑了"+ i + "圈,是否要继续"); } } } }