一个包下的类不可同名,不同包可以有同名的类。
包的名字一般是公司域名反写如com.baidu
同个包下面的类可以直接使用,但是不同包的类则需要导包
如 import com.baidu.Student;
类的使用: Student s = new Student()
在IDEA中 输入类的时候回车,如果调用的类不在当前包下,会自动导包到该页面
常用的API
Scanner 简单的文本扫描程序,可以获取基本类型数据和字符串数据
Scanner scan = new Scanner(System.in)
int p = scan.nextInt(); int一个p来接收输入的值
String s= scan.nextLine() 接受字符串,结束标志是回车
String s1=scan.next() 接受字符串 结束标志是空格
当需要结合整数跟字符串的时候,建议使用nextInt 和nextLine配合使用
Random,随机数生成器
Random r = new Random()
int p = r.nextInt(9) 表示,随机出来的数在[0-9)范围内,不包括9
找随机数
Random r =new Random(); int num = r.nextInt(101); Scanner scanner=new Scanner(System.in); while (true) { System.out.println("请输入一个0到100的数"); int shuru = scanner.nextInt(); if (shuru > num) { System.out.println("你猜的数大了"); } else if (shuru < num) { System.out.println("你猜的数小了"); } else { System.out.println("恭喜你回答正确"); break; }
注意找的过程中需要不断输入,所以要用while做一个死循环,当找到的时候break就可以了