昨天学习了循环结构,再补充一个知识点,关于Random随机数的使用
①导包:import java.util.Random;
②创建对象:Random r=new Random();
//上面这个格式里,r是变量名,可以变,其他的都不允许改变
③int number=r.nextInt(10); //获取数据的范围:[0,10]包括0但是不包括10
用Random生成一个随机数,进行猜数字的程序,直到猜中为止
//Sanner和Random都用的java.util包,所以可以直接用*表示
import java.util.*;
public class test {
public static void main(String[] args) {
Random ra = new Random();
int number = ra.nextInt(100) + 1;
//循环不能把随机数也包含在里面,while(ture)是个无限循环
while (true) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个1-100的数字:");
int i = sc.nextInt();
if (number > i) {
System.out.println(i + "比随机数字小");
} else if (number < i) {
System.out.println(i + "比随机数字大");
} else if (number == i) {
System.out.println(i + "跟随机数字一样");
//利用break结束整个循环
break;
}
}
}
}
在开始新一天的数组学习之,先安装一下开发工具IDEA,目前先总结一下常用的IDEA的快捷键
快速生成语句:①生成main()方法:psvm/main,回车 ②生成输出语句:sout,回车
内容辅助键:Ctrl+Alt+space
快捷键:注释:单行Ctrl+/,多行Ctrl+Shift+/ 格式化:Ctrl+Alt+L
对于数组的定义和遍历我们用一个实例来进行表示
public class test {
public static void main(String[] args) {
//定义一个数组
int[] array = {1, 2, 3, 4, 5, 6};
//array.length用来表示数组的长度
for (int i = 0; i < array.length; i++) {
//用for循环来遍历数组
System.out.println(array[i]);
}
}
}
开始类的学习和调用,了解java的运行顺序,从main函数开始运行
public class test {
public static void main(String[] args) {
//调用方法,直接传递常量
isEvenNumber(5);
//定义一个参数再传递到方法中去,这个参数为实参
int number = 5;
isEvenNumber(number);
}
//定义一个方法,方法中的参数为形参
public static void isEvenNumber(int number) {
if (number % 2 == 0)
System.out.println(true);
else
System.out.println(false);
}
}
对于方法的重载来说,重载只对于方法的名称和参数有关,与返回值无关,来进行一个重载的练习实例
//比较两个整数是否相同,兼容所有的整数数据类型
public class test {
public static void main(String[] args) {
//调用方法,调用其他三种方法需要强行转换
System.out.println(inEven(10,20));
System.out.println(inEven((byte)10,(byte)20));
System.out.println(inEven((long)10,(long)20));
System.out.println(inEven((short)10,(short)20));
}
//方法定义用Boolean类型来输出是否相同
public static boolean inEven(byte a, byte b) {
System.out.println("byte");
return a == b;
}
//方法重载,整数默认调用int类型
public static boolean inEven(int a, int b) {
System.out.println("int");
return a == b;
}
public static boolean inEven(long a, long b) {
System.out.println("long");
return a == b;
}
public static boolean inEven(short a, short b) {
System.out.println("short");
return a == b;
}
}
接下来会学习一个重点,就是方法参数的传递(基本类型),要明白形参的值是不会影响到实参的,用一个例子来表明
public class test {
public static void main(String[] args) {
int number = 100;
//调用方法前的数为100
System.out.println("调用方法前的数为" + number);
//对参数进行调用
Change(number);
//调用方法后的数为100
System.out.println("调用方法后的数为" + number);
}
public static void Change(int number) {
//没有返回值
number = 200;
}
}
参数传递(引用类型)
但是有没有形参能影响到实参的呢,来看一个例子
public class test {
public static void main(String[] args) {
//定义一个数组,它的长度为三
int[] arr = {10, 20, 30};
//调用方法前的数为20
System.out.println("调用方法前的数为" + arr[1]);
Change(arr);
//调用方法后的数为200
System.out.println("调用方法后的数为" + arr[1]);
}
public static void Change(int[] arr) {
arr[1] = 200;
}
}
发现了再调用方法后,arr[2]的值确实被改变了,这涉及到java的栈内存和堆内存的问题,数组的数据时存放在堆内存的,形参不会影响栈内存,但是在改变数组的数的时候会直接影响到堆内存,数组在输出的时候也会直接调用堆内存中存放的数,所以在方法调用之后数据改变了,这种参数的引用我们成为引用类型的参数传递。
最后在今天进行一个总结,我们用方法来找出数组中的最大值
public class test {
public static void main(String[] args) {
//定义一个数组
int[] arr = {32, 22, 98, 60, 55};
//用一个参数存储调用方法的返回值
int number=Max(arr);
System.out.println("数组中的最大值为:"+number);
}
public static int Max(int[] arr) {
//定义一个参数用于存放最大值
int max = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
//返回最大值
return max;
}
}
今天的java学习就到这里了,明天将会进行很多实例来复习一下之前学的内容,共勉。