基础案列与注意
数组静态初始化简化格式
int[] arr = {数据1,数据2...};
遍历数组
//遍历数组
public class Demo {
public static void main(String[] args) { ;
int[] arr1 = new int []{1,5,8,4,5,7};
for (int i = 0; i < arr1.length; i++) {
System.out.println(arr1[i]);
}
}
}
获取最大值
//获取最大值
public class Demo {
public static void main(String[] args) {
int[] arr1 = new int[]{6, 3, 2, 4, 5, 1};
int max = arr1[0];
for (int i = 1; i < arr1.length; i++) {
if (arr1[i] > max) {
max = arr1[i];
}
}
System.out.println(max);
}
}
- 不带参数方法的调用 :方法名();
- void类型带参数方法的调用 :方法名(实际参数);
- 带参数方法的定义
格式:public static void 方法名(参数1,参数2){…}
- 带参数方法的调用
格式:方法名(参数1,参数2);
-
形参和实参
-
形参:方法定义中的参数
实参数:方法调用的参数
-
带返回值方法的的定义
public static 数据类型 方法名(参数){
return 数据;
}
- 带返回值方法的调用
数据类型 变量名 = 方法名(参数);
- 方法注意事项
- 方法不能嵌套
- void表示没有返回值,可以省略return,也可以单独写一个return;
- return表示已经方法已经结束。
方法重载
方法重载指同一个类中的多个方法之间的关系,满足以下条件构成方法重载
- 多个方法在同一个类中
- 多个方法具有相同的方法名
- 多个方法具有参数不相同,类型不同或者数量不同
public class Demo {
public static void main(String[] args) {
System.out.println(sum(55,98));
System.out.println(sum(55,98,87));
}
public static int sum(int a,int b){
int sum = a+b;
return sum;
}
public static int sum(int a,int b ,int c){
int sum = a+b+c;
return sum;
}
}
- 注意
对于基本数据类型的参数,形式参数的改变,不影响实际参数的值
public class Demo {
public static void main(String[] args) {
int number = 100;
System.out.println("调用change前的方法"+number);
change(number);
System.out.println("调用change后的方法"+number);
}
public static void change(int number){
number = 200;
}
}
//第一一次输出:100 第二次输出 100
- 方法参数传递(引用类型)
对于应用类型的参数,形式参数改变会影响实际参数的值
public class Demo {
public static void main(String[] args) {
int number = 100;
System.out.println("调用change前的方法"+number);
change(number);
System.out.println("调用change后的方法"+number);
}
public static void change(int number){
number = 200;
}
}
/*
调用change方法前30
调用change方法后1000
*/
数组遍历
public class Demo {
public static void main(String[] args) {
int[] arr = {10,40,51,15,15,99,88,45,63};
printArray(arr);
}
public static void printArray(int[] arr){
System.out.print("[");
for (int i = 0; i <arr.length ; i++) {
if(i<arr.length &&i>0) {
System.out.print(",");
}
System.out.print(arr[i]+"\t");
}
System.out.print("]");
}
}
//[10 ,40 ,51 ,15 ,15 ,99 ,88 ,45 ,63 ]
数组最大值
//数组最大值
public class Demo {
public static void main(String[] args) {
int[] arr = {10, 40, 51, 15, 15, 99, 88, 45, 63};
System.out.println(max(arr));
}
public static int max(int[] arr) {
int max = arr[0];
for (int i = 1; i < arr.length; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
return max ;
}
}
输入一个数,判断要做什么
package Demo1;
import java.util.Scanner;
public class Demo {
public static void main(String[] args) {
while(true){
System.out.println("===========");
Scanner sc = new Scanner(System.in);
System.out.println("输入0结束运行");
System.out.println("请输入一个星期数");
int week = sc.nextInt();
System.out.println("你输入的是"+":"+week);
if (week < 0 || week > 7) {
System.out.println("你所输入有误,请重新输入");
} else if (week == 1) {
System.out.println("你要做的是:跑步");
} else if (week == 2) {
System.out.println("你要做的是:滑板");
} else if (week == 3) {
System.out.println("你要做的是:学习");
} else if (week == 4) {
System.out.println("你要做的是:玩游戏");
} else if (week == 5) {
System.out.println("你要做的是:骑马");
} else if (week == 6) {
System.out.println("你要做的是:看书");
} else if (week == 7) {
System.out.println("你要做的是:看电影");
}else if(week==0) {
System.out.println("===========");
System.out.println("已经结束运行");
break;
}
}
}
}
//输入一个数,判断要做什么
不死兔子
//不死兔子
public class Demo {
public static void main(String[] args) {
int[] arr = new int [20];
arr[0]=1;
arr[1]=1;
/*arr[3]=2;
arr[4]=3;
arr[5]=5;
arr[6]=8;
*/
for (int i = 2; i < arr.length; i++) {
arr[i]=arr[i-1]+arr[i-2];
}
System.out.println("第20个月的兔子数量是"+arr[19]);
}
}
百钱百鸡
//白钱白鸡
public class Demo {
public static void main(String[] args) {
for (int i = 0; i <=20 ; i++) {
for (int j = 0; j <=33 ; j++) {
int z = 100-i-j;
if (z%3==0 && 5*i+3*j+z/3==100) {
System.out.println(i+"\t"+j+"\t"+z);
}
}
}
}
}
判断数组内容是否相同
package Demo1;
//判断数组内容是否相同
import java.util.Scanner;
public class Demo {
public static void main(String[] args) {
int[] arr1 = {1, 2, 3, 4, 5, 6};
int[] arr2 = {1, 2, 3, 4, 5,6};
int[] arr3 = {1, 2, 3, 4, 5};
int[] arr4 = {1, 2, 3, 4, 5,7};
boolean flag = compare(arr1,arr2);
boolean flag1 = compare(arr1,arr3);
boolean flag2 = compare(arr1,arr4);
boolean flag3 = compare(arr2,arr4);
boolean flag4 = compare(arr2,arr3);
System.out.println(flag);
System.out.println(flag1);
System.out.println(flag2);
System.out.println(flag3);
System.out.println(flag4);
}
public static boolean compare(int arr1[], int arr2[]) {
if (arr1.length != arr2.length) {
return false;
}
for (int i = 0; i < arr1.length; i++) {
if (arr1[i] != arr2[i]) {
return false;
}
}
return true;
}
}
/*true
false
false
false
false
*/
查找数组中元素对应的索引
//查找数组中元素对应的索引
package Demo1;
import java.util.Scanner;
public class Demo {
public static void main(String[] args) {
int[] arr = {1, 5, 1, 58, 4, 4, 8, 4, 5, 9, 8, 4, 1, 5, 8, 8, 8, 4, 4, 5};
int index = check(arr,4);
/* Scanner sc = new Scanner(System.in);
System.out.println("输入需要查找的数据" );
int index =-1;
int s1 = sc.nextInt();
for (int i = 0; i < arr.length; i++) {
if(arr[i]== s1){
index = i;
System.out.println("index:"+ index);
*/
}
public static int check(int arr[],int a){
Scanner sc = new Scanner(System.in);
System.out.println("输入需要查找的数据" );
int index =-1;
int s1 = sc.nextInt();
for (int i = 0; i < arr.length; i++) {
if (arr[i] == s1) {
index = i;
System.out.println("index:" + index);
}
}
return index;
}
}
数组反转
//数组反转
package Demo1;
import javax.xml.soap.SAAJMetaFactory;
import java.util.Scanner;
public class Demo {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9};
printArray(arr);
reverse(arr);
System.out.println();
printArray(arr);
}
public static void printArray(int[] arr){
System.out.print("[");
for (int i = 0; i <arr.length ; i++) {
if (i ==arr.length-1) {
System.out.print(arr[i]);
}else {
System.out.print(arr[i]+",");
}
}
System.out.print("]");
}
public static void reverse(int[] arr) {
for (int start = 0, end = arr.length - 1; start <= end; start++, end--) {
int temp = arr[start];
arr[start] = arr[end];
arr[start] = temp;
}
}
}
评委打分
package Demo1;
//评委打分
import javax.xml.soap.SAAJMetaFactory;
import java.util.Scanner;
public class Demo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int arr[] = new int[6];
for (int i = 0; i < arr.length; i++) {
System.out.println("其输入第个" + (i + 1) + "评委的打分");
arr[i] = sc.nextInt();
}
System.out.println();
int max = max(arr);
int min= min(arr);
int sum = sum(arr);
int avg = (sum - min -max )/(arr.length-2);
System.out.println("=================");
System.out.print("你输入的分数分别是");
printArray(arr);
System.out.println();
System.out.println("最终得分为:"+" "+avg);
}
public static void printArray(int[] arr) {
System.out.print("[");
for (int i = 0; i < arr.length; i++) {
if (i == arr.length - 1) {
System.out.print(arr[i]);
} else {
System.out.print(arr[i] + ",");
}
}
System.out.print("]");
}
public static int max(int arr[]) {
int max = arr[0];
for (int i = 1; i < arr.length; i++) {
if (arr[ i] >max) {
max = arr[i];
}
}
return max;
}
public static int min(int arr[]) {
int min = arr[0];
for (int i = 1; i < arr.length; i++) {
if (arr[i ] < min) {
min = arr[i ];
}
}
return min;
}
public static int sum(int arr[]){
int sum = 0;
for (int i = 0; i < arr.length; i++) {
sum = arr[i]+sum;
}
return sum;
}
}