# 知识点
方法
## 题目1
定义一个方法,该方法能够找出两个小数中的较小值并返回。在主方法中调用方法进行测试。
### 训练提示
1. 根据方法的功能描述,方法的参数应该是两个小数。
2. 要返回两个小数的较小值,所以返回值类型也是小数类型。
### 解题方案
### 操作步骤
1. 定义方法getMin(),方法的参数是double a ,double b。
2. 在方法中对两个数字进行判断,返回较小值。
3. 在主方法中调用getMin()方法并接受返回值。
4. 在主方法中打印结果。
import java.util.Scanner;
public class day {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("请输入x的数值");
double x=sc.nextDouble();
System.out.println("请输入y的数值");
double y=sc.nextDouble();
contrast(x,y);
}
public static void contrast(double x,double y){
if(x>y) {
System.out.println("x更大"+x);
}else if(x<y){
System.out.println("y更大"+y);
}else{
System.out.println("一样大");
}
}
}
## 题目2
定义一个方法,该方法能够找出三个整数中的最大值并返回。在主方法中调用方法测试执行。
### 训练提示
1. 根据题意,方法中需要使用三个整数,所以方法参数应该是三个整数类型。
2. 方法需要有返回值,返回值的类型也是整数类型。
### 解题方案
### 操作步骤
1. 定义方法getMax(),方法的参数是三个int类型变量a,b,c,方法的返回值是int类型。
2. 在方法中使用多分支if...else...或者三元运算符判断出最大值并返回。
3. 在主方法中调用getMax()方法并接受返回值。
4. 在主方法中打印结果。
### 参考代码
public class day {
public static void main(String[] ages) {
System.out.println(getMax(20, 5, 154));
}
public static int getMax(int a, int b, int c) {
int max = 0;
if (a > b && a > c) {
System.out.println("a最大");
max = a;
} else if (b > a && b > c) {
System.out.println("b最大");
max = b;
} else if (c > a && c > b) {
System.out.println("c最大");
max = c;
} else {
System.out.println("输入的数字不符合规范");
}
return max;
}
}
三元运算符,最后9999代表输入数字不符合,因为三元运算符返回值只能int类型,前边加一个判断即可
return max=(a>b)&&(a>c)?a:((b>a)&&(b>c)?b:((c>a)&&(c>b)?c:(9999)));
## 题目3
在主方法中通过键盘录入三个整数。定义一个方法,方法接收三个整数变量,在方法中从大到小依次打印三个变量。执行效果如下:
```
请输入第一个整数:10
请输入第二个整数:30
请输入第三个整数:20
从大到小的顺序是: 30 20 10
```
### 训练提示
1. 方法需要接受三个整数,那么方法的形式参数如何定义?
2. 方法没有返回值的需求,返回值类型是什么?
### 解题方案
### 操作步骤
1. 使用键盘录入分别录入三个整数。
2. 定义method方法,方法的参数是三个int类型,方法的返回值类型是void。
2.1. 定义整数变量max用于存储最大值,定义min变量用于存储最小值。
2.2. 使用if..else..多分支判断语句或者三元运算符计算三个整数中的最大值并赋值给max。
2.3. 使用if..else..多分支判断语句或者三元运算符计算三个整数中的最小值并赋值给min。
2.4. 定义变量mid代表中间数,三个整数的和减去max,再减去min,就是中间数的值。
2.5. 依次打印最大值,中间值和最小值。
3. 在主方法中调用method方法,传入参数。
### 参考答案
用数组输出:
import java.util.Scanner;
public class day {
public static void main(String[] ages){
Scanner sc=new Scanner(System.in);
System.out.println("请输入第一个整数:");
int a=sc.nextInt();
System.out.println("请输入第二个整数:");
int b=sc.nextInt();
System.out.println("请输入第三个整数:");
int c=sc.nextInt();
method(a,b,c);
}
public static void method(int a,int b,int c){
int max=0;//用作暂时存储数据
int []arr=new int[]{a,b,c};//利用数组进行排序
//用for循环将最小值放到最后一位
for (int i = 0; i < arr.length-1; i++) {
if (arr[i] < arr[i + 1]) {
max = arr[i + 1];
arr[i + 1] = arr[i];
arr[i] = max;
}
}
//用if判断出前面两个数值的大小并进行排序,将最大值放在第一位
if(arr[0]<arr[1]) {
max = arr[1];
arr[1] = arr[0];
arr[0] = max;
}
System.out.print("从大到小的顺序是:");
//遍历输出数组的值
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i]+" ");
}
}
}
用if语句和三元运算符:
import java.util.Scanner;
public class day {
public static void main(String[] ages) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入第一个整数:");
int a = sc.nextInt();
System.out.println("请输入第二个整数:");
int b = sc.nextInt();
System.out.println("请输入第三个整数:");
int c = sc.nextInt();
method(a, b, c);
}
public static void method(int a, int b, int c) {
int max = 0, mid = 0, min = 0;//分别存储最大值,中间值,最小值
//
//
//用三元运算符
max = (a >= b) && (a >=c) ? a : (((b >= a) && (b >= c) ? b : ((c >= a) && (c >=b) ? c : 999)));
min = (a <= b) && (a <= c) ? a : (((b <= a) && (b <= c) ? b : ((c <= a) && (c <= b) ? c : 999)));
mid = a + b + c - max - min;
System.out.print(max + " " + mid + " " + min);
//
//
//用if esls语句
if (a >= b && a >= c) {
max = a;
} else if (b >= a && b >= c) {
max = b;
} else if (c >= a && c >= b) {
max = c;
}
if (a <= b && a <= c) {
min = a;
} else if (b <= a && b <= c) {
min = b;
} else if (c <= a && c <= b) {
min = c;
}
mid = a + b + c - max - min;
System.out.print(max + " " + mid + " " + min);
}
}
## 题目4
数字是有绝对值的,负数的绝对值是它本身取反,非负数的绝对值是它本身。请定义一个方法,方法能够得到小数类型数字的绝对值并返回。请定义方法并测试。
### 训练提示
1. 方法的功能是得到一个小数的绝对值,参数应该是一个小数类型。
2. 绝对值需要被返回,返回值类型应该也是小数类型。
### 解题方案
### 操作步骤
1. 定义一个小数变量num。
2. 定义获取绝对值的方法,方法的参数是一个double类型,返回值类型是double。
3. 在方法内部使用if..else..判断。
3.1. 如果是负数则对负数取反并返回。
3.2. 如果不是负数则直接返回数字本身。
4. 在主方法中调用绝对值方法,传入参数num,并接受返回值。
5. 打印返回的结果。
### 参考答案
import java.util.Scanner;
public class day{
public static void main(String[] ages){
Scanner sc=new Scanner(System.in);
System.out.println("请输入一个小数");
double num=sc.nextDouble();
System.out.println(absoluteValue(num));
}
public static double absoluteValue(double num){
double decimals=0;
if(num>=0){
decimals=num;
}else{
//用0减负数就可以将其转正数
decimals= 0-num;
}
return decimals;
}
}
## 题目5
键盘录入一个正整数
定义一个方法,该方法的功能是计算该数字是几位数字,并将位数返回
在main方法中打印该数字是几位数
演示格式如下:
(1)演示一:
请输入一个整数:1234
控制台输出:1234是4位数字
(2)演示二:
请输入一个整数:34567
控制台输出:34567是5位数字
### 训练提示
1. 方法的功能是求有多少位,所以参数是一个,就是要计算的数据。
2. 题目说要返回,所以方法必须有返回值。
### 解题方案
### 操作步骤
1. 键盘录入一个正整数
2. 定义获取位数的方法,方法的参数是一个int类型,返回值类型是int。
3. 在方法内部使用循环获取有多少位
可以不断的除以10,当结果为0时,循环结束。
除以10的次数,就是数字的位数。
举例:
123 除以第一次10之后为:12
除以第二次10之后为:1
除以第三次10之后为:0
表示123是三位数
4. 在主方法中调用方法,传入参数,并接受返回值。
5. 打印返回的结果。
### 参考答案
import java.util.Scanner;
public class day {
public static void main(String[] ages) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个整数:");
int num = sc.nextInt();
digit(num);
}
public static int digit(int num) {
int j = 0;//计数器
for (int i = num; i >=1; j++) {
i = i / 10;
}
int ge = num % 10;
System.out.println(num + "是" + j + "位数字");
return ge;
}
}
## 题目6
需求:
定义一个方法equals(int[] arr1,int[] arr2).
功能:
比较两个数组是否相等(长度和内容均相等则认为两个数组是相同的)
public class day {
public static void main(String[] ages){
int []arr1=new int[]{2,4,6};
int []arr2=new int[]{2,4};
System.out.println(equals(arr1,arr2));
}
public static boolean equals(int[] arr1,int[] arr2){
if(arr1.length!=arr2.length){
return false;
}
for (int i = 0; i < arr1.length; i++) {
if(arr1[i]==arr2[i]){
}else{
return false;
}
}
return true;
}
}
## 题目7:
需求:
定义一个方法fill(int[] arr,int value)
功能:
将数组arr中的所有元素的值改为value
import java.util.Scanner;
public class day {
public static void main(String[] ages){
int[]arr=new int[]{1,5,6,9,7};
Scanner sc=new Scanner(System.in);
int value =sc.nextInt();
fill(arr,value);
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i]+" ");
}
}
public static int[] fill(int[] arr,int value){
for (int i = 0; i < arr.length; i++) {
arr[i]=value;
}
return arr;
}
}
## 题目8:(较难)
需求:
定义一个方法fill(int[] arr,int fromIndex,int toIndex,int value)
功能:
将数组arr中的元素从索引fromIndex开始到toIndex(不包含toIndex)对应的值改为value
import java.util.Scanner;
public class day {
public static void main(String[] ages){
int[]arr=new int[]{1,5,6,9,745};
Scanner sc=new Scanner(System.in);
System.out.println("请输入value的值");
int value =sc.nextInt();
System.out.println("请输入fromIndex的值");
int fromIndex =sc.nextInt();
System.out.println("请输入toIndex的值");
int toIndex =sc.nextInt();
fill(arr,fromIndex,toIndex,value);
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i]+" ");
}
}
public static int[] fill(int[] arr,int fromIndex,int toIndex,int value){
for (int i = fromIndex; i < toIndex; i++) {
arr[i]=value;
}
return arr;
}
}
## 题目9:(较难)
需求:
定义一个方法copyOf(int[] arr, int newLength)
功能:
将数组arr中的newLength个元素拷贝到新数组中,并将新数组返回,从索引为0开始
import java.util.Scanner;
public class day {
public static void main(String[] ages) {
int[] arr = new int[]{1, 5, 6, 9, 745};
Scanner sc = new Scanner(System.in);
System.out.println("请输入newLength的值");
int newLength = sc.nextInt();
int []brr=copyOf(arr,newLength);
for (int i = 0; i < brr.length; i++) {
System.out.print(brr[i] + " ");
}
}
public static int[] copyOf(int[] arr, int newLength) {
int[] brr = new int[newLength];
for (int i = 0; i < newLength; i++) {
brr[i] = arr[i];
}
return brr;
}
}
## 题目10:(较难)
需求:
定义一个方法copyOfRange(int[] arr,int from, int to)
功能:
将数组arr中从索引from(包含from)开始,到索引to结束(不包含to)的元素复制到新数组中,
并将新数组返回。
import java.util.Scanner;
public class day {
public static void main(String[] ages) {
int[] arr = new int[]{1, 5, 6, 9, 745};
Scanner sc = new Scanner(System.in);
System.out.println("请输入from的值");
int from = sc.nextInt();
System.out.println("请输入to的值");
int to = sc.nextInt();
int []brr=copyOfRange(arr,from,to);
for (int i = 0; i < brr.length; i++) {
System.out.print(brr[i] + " ");
}
}
public static int[] copyOfRange(int[] arr,int from, int to) {
int[] brr = new int[to-from];
int j=0;
for (int i = from; i < to; i++,j++) {
brr[j] = arr[i];
}
return brr;
}
}
## 题目11(很难)
一个大V直播抽奖,奖品是现金红包,分别有{2,588,888,1000,10000}五个奖金。请使用代码模拟抽奖,打印出每个奖项,奖项的出现顺序要随机且不重复。打印效果如下:(随机顺序,不一定是下面的顺序)
```
888元的奖金被抽出
588元的奖金被抽出
10000元的奖金被抽出
1000元的奖金被抽出
2元的奖金被抽出
```
### 训练提示
1. 奖项要随机出现,但奖金不是连续的数字,不能被随机产生。能随机产生的只有数组的索引了,可以使用随机索引来代表随机元素。因为索引和元素是一一对应的,
2. 哪些奖被抽过了,哪些奖没有被抽过,要定义一个数组来存放已经被抽过的奖项。
3. 每个奖项只能被抽出一次,要写一个方法来控制奖项不重复。
### 解题方案
使用数组存放多个奖金,再使用另一个数组存放已经被抽过的奖金,使用方法来判断某个奖金是否已经被抽取过。
### 操作步骤
1. 定义奖金的数组arr。
2. 定义数组brr准备存放已经被抽过的奖金,两个数组长度相同。
3. 定义一个变量index,用户代表数组brr的索引。
4. 定义方法,判断数组中是否存在某数字,存在返回true,不存在返回false。
5. 写一个while循环,如果index<arr.length则说明奖项没有被抽完继续抽取。
6. 在循环中使用随机数产生一个随机索引i。
7. 使用步骤4的方法判断brr数组中是否包含arr[i]奖金。
8. 如果不包含,则打印arr[i]奖金,并且把它放入brr数组中代表已经被抽取过,同时index加一。
### 参考答案
import java.util.Random;
public class day {
public static void main(String[] ages) {
int[] arr = new int[]{2, 588, 888, 1000, 10000};
int[] brr = new int[arr.length];
int index = 0;//index作为brr数组的下标
//循环让brr下标控制在和arr数组相同位数
while (index < arr.length) {
Random r = new Random();
int x = r.nextInt(5);
//利用方法的布尔值判断随机数是否是重复的
if (ran(arr, brr, index, x) == true) {
brr[index] = arr[x];//a3,a2
index++;//不重复使下标加一
}
}
for (int i = 0; i < brr.length; i++) {
System.out.println(brr[i] + "元的奖金被抽出");
}
}
public static boolean ran(int[] arr, int[] brr, int index, int x) {
//循环将当前下标x的arr数组的据和brr里已有的数据进行对比,
// 如果相同则随机数相同,退出方法输出false,
//如果不同则随机数不同,可以放入brr数组,退出方法输出true,
for (int j = 0; j <= index; j++) {
if (arr[x] == brr[j]) {
return false;
}
}
return true;
}
}