API(application programming interface)应用程序编程接口
API文档:https://www.oracle.com/java/technologies/downloads/#jdk18-windows

键盘录入技术

导包过程,IDEA可以自动帮你导入
System.in
A.nextInt:表示代码会导致用户输入数据,知道用户输入完整数据并按回车,就会把数据拿到。

自动类型转换


小范围可以赋值给大范围。
注意:char ch=’a’;int b=a;是可以的

1.表达式的自动类型转换:小范围的变量在计算过程中会转换为大范围的变量。比如:byte short char
下面是错误的
1.byte a=10;
2.byte b=20;
3.byte c=a+b;
重点看:P38

死循环
写死循环:用while(true){};

案例:确认密码界面

  1. 1.public class DeadLoop {
  2. 2. public static void main(String[] args)
  3. 3. {
  4. 4. int rightCode=314;
  5. 5. Scanner code=new Scanner(System.in);
  6. 6. while(true)
  7. 7. {
  8. 8. System.out.println("please enter your password");
  9. 9. int number=code.nextInt();
  10. 10. if(number==rightCode)
  11. 11. {
  12. 12. System.out.println("welcome");
  13. 13. break;
  14. 14. }
  15. 15. else
  16. 16. {
  17. 17. System.out.println("password error,please re-enter your password");
  18. 18. }
  19. 19. }
  20. 20.
  21. 21. }
  22. 22.}

Continue用于跳出当前循环,直接进入下一次循环

  1. 1.public class Continue {
  2. 2. public static void main(String[] args) {
  3. 3. //continue跳出当前循环,直接进入下一次循环
  4. 4. for (int i = 0; i < 5; i++) {
  5. 5. if(i==3)
  6. 6. {
  7. 7. continue;
  8. 8. }
  9. 9. System.out.println(i);
  10. 10. }
  11. 11. }
  12. 12.}

猜数字随机数

步骤:1、导包;2、创建对象;3、接受数据。

猜数字代码

  1. 1.public class SuiJiShu {
  2. 2. public static void main(String[] args) {
  3. 3. Random a=new Random();
  4. 4. int number=a.nextInt(10);
  5. 5. Scanner c=new Scanner(System.in);
  6. 6. while(true)
  7. 7. {
  8. 8. System.out.println("please enter a number");
  9. 9. int d=c.nextInt();
  10. 10. if(d==number)
  11. 11. {
  12. 12. System.out.println("you are right.");
  13. 13. break;
  14. 14. }
  15. 15. else if(d>number)
  16. 16. {
  17. 17. System.out.println("your number is bigger than my number.");
  18. 18. }
  19. 19. else
  20. 20. {
  21. 21. System.out.println("your number is smaller than my number.");
  22. 22. }
  23. 23. }
  24. 24. }
  25. 25.}

实际上r.nextInt()中的数,代表生成随机数的区间,而后面+number,代表这个区间的起始位置。
例如r.nextInt(3)+1,代表从1开始区间为3的区间。r.nextInt(10),代表从0开始,区间为10的区间。

Switch穿透性: