// public class Test{
// public int changeNum(int x){
// System.out.println("方法执行开始:"+x);
// x = 10;
// System.out.println("方法执行最终:"+x);
// return x;
// }
// public static void main(String[] args){
// Test t = new Test();
// int a = 1;
// a = t.changeNum(a);
// System.out.println("方法执行完毕,main方法中的值"+a);
// }
// }
// public class Test{
// public void changeArray(int[] x){
// System.out.println("方法执行开始"+x[0]);
// x[0] = 10;
// System.out.println("方法执行最终"+x[0]);
// }
// public static void main(String[] args){
// Test t = new Test();
// int[] a = {1,2,3};
// t.changeArray(a);
// System.out.println("方法执行结束"+a[0]);
// }
// }
//方法一 用for循环将两个数组中对应位置进行交换
//是否需要条件 两个数组
//是否需要留下结果 不需要 值交换
// public class Test{
// public void changeTwoArray(int[] a, int[] b){
// for(int i=0; i<a.length; i++){
// int temp;
// temp = a[i];
// a[i] = b[i];
// b[i] = temp;
// }
// }
// public static void main(String[] args){
// Test t = new Test();
// int[] x = {1,2,3,4};
// int[] y = {5,6,7,8};
// t.changeTwoArray(x,y);
// for(int v:x){
// System.out.println(v);
// }
// System.out.println("-----");
// for(int v:y){
// System.out.println(v);
// }
// }
// }
//方式二 将两个数组的地址引用直接交换
//需不需要条件 两个数组的地址
//需不需要留下结果 交换后数组的地址,放在一个二维数组里存下来,赋值给两个数组
// public class Test{
// public int[][] changeTwoArray(int[] a, int[] b){
// int[] temp = a;
// a = b;
// b = temp;
// int result[][] = {a,b};
// return result;
// }
// public static void main(String[] args){
// Test t = new Test();
// int[] x = {1,2,3,4};
// int[] y = {5,6,7,8,9};
// int [][] array = t.changeTwoArray(x,y);
// x = array[0];//array数组的第1个内容是形参a的地址,即交换前y的地址
// y = array[1];//array数组的第2个内容是形参b的地址,即交换前x的地址
// for(int v:x){
// System.out.println(v);
// }
// System.out.println("-----");
// for(int v:y){
// System.out.println(v);
// }
// }
// }
//设计一个方法,用来交换一个数组(头尾交换)
//是否需要参数 需要提供一个数组
//返回值 不需要
// public class Test{
// public void changeArrayElements(int[] array){
// for(int i=0; i<array.length/2; i++){
// int temp = array[i];
// array[i] = array[array.length-1-i];
// array[array.length-1-i] = temp;
// }
// }
// public static void main(String[] args){
// Test t = new Test();
// int[] x = {1,2,3,4,5,6,7,8,9};
// t.changeArrayElements(x);
// for(int v:x){
// System.out.println(v);
// }
// }
// }
//设计一个方法,用来寻找数组中的极值(最大值或最小值)
//是否需要参数 需要提供一个数组 需要提供一个值(最大或最小)
//是否需要返回值 返回最大的值或最小的值
// public class Test{
// public int findMaxOrMinNum(int[] array,boolean flag){
// if(flag){
// int max = array[0];
// for(int i=1; i<array.length; i++){
// if(array[i]>max){
// max = array[i];
// }
// }
// return max;
// }else{
// int min = array[0];
// for(int i=1; i<array.length; i++){
// if(array[i]<min){
// min = array[i];
// }
// }
// return min;
// }
// }
// public static void main(String[] args){
// Test t = new Test();
// int[] x = {1,3,5,7,9,0,8,6,4,2};
// int value = t.findMaxOrMinNum(x,false);
// System.out.println(value);
// }
// }
//优化
// public class Test{
// public int findMaxOrMinNum(int[] array,boolean flag){
// //找一个临时变量
// int temp = array[0];
// for(int i=1; i<array.length; i++){
// if(flag && array[i]>temp){//寻找最大值
// temp = array[i];
// }else if(!flag && array[i]<temp){//寻找最小值
// temp = array[i];
// }
// }
// return temp;//返回最大或最小值
// }
// public static void main(String[] args){
// Test t = new Test();
// int[] x = {1,3,5,7,9,0,8,6,4,2};
// int result = t.findMaxOrMinNum(x,false);
// System.out.println(result);
// }
// }
//设计一个方法 用来找寻给定的元素是否在数组内存在(Scanner输入)
//是否需要参数及返回值 需要提供一个数组 需要一个目标元素, 返回值 是否找到
// import java.util.Scanner;
// public class Test{
// public String isExist(int[] array, int value){
// // boolean flag = false;
// String result = "没找到";
// for(int i=0; i<array.length; i++){
// if(value==array[i]){
// // flag = true;
// result = "找到了";
// break;
// }
// }
// return result;
// }
// public static void main(String[] args){
// int[] x = {1,2,3,4,5,6};
// Scanner input = new Scanner(System.in);
// System.out.println("输入想要找的值");
// int input_value = input.nextInt();
// Test t = new Test();
// String result = t.isExist(x,input_value);
// System.out.println(result);
// }
// }
//设计一个方法 合并两个数组
//是否需要参数 需要两个数组 返回值 需要一个大数组
// public class Test{
// public int[] mergeArray(int[] a, int[] b){
// int[] newArray = new int[a.length+b.length];
// for(int i=0; i<a.length; i++){
// newArray[i] = a[i];
// }
// for(int i=0; i<b.length; i++){
// newArray[a.length+i] = b[i];
// }
// return newArray;
// }
// public static void main(String[] args){
// Test t = new Test();
// int[] x = {1,2,3,4};
// int[] y = {5,6,7,8};
// int newArray = new int[x.length+y.length];
// newArray = t.mergeArray(a,b);
// for(int v: newArray){
// System.out.println(v);
// }
// }
// }
//设计一个方法 将一个数组按最大值位置拆分
//参数及返回值 需要提供一个数组 返回两个数组
public class Test{
public int[][] split(int[] array){
int max = array[0];
int index = 0;
for(int i=1; i<array.length; i++){
if(array[i]>max){
max = array[i];
index = i;
}
}
int[] newArray1 = new int[index];
int[] newArray2 = new int[array.length-index-1];
for(int i=0; i<newArray1.length; i++){
newArray1[i] = array[i];
}
for(int i=0; i<newArray2.length; i++){
newArray2[i] = array[index+1+i];
}
int[][] result = {newArray1,newArray2};
return result;
}
public static void main(String[] args){
Test t = new Test();
int[] x = {1,3,5,9,7,4,3,5};
int[][] result = t.split(x);
int[] a = result[0];
int[] b = result[1];
for(int v:a){
System.out.println(v);
}
System.out.println("----");
for(int v:b){
System.out.println(v);
}
}
}
//设计一个方法 用来去掉数组中等于某个值的元素
//参数 需要提供一个数组 需要提供删除的元素是什么 返回值 一个新的数组
public class Test{
public int[] deleteElement(int[] array, int element){
int count = 0;
for(int i=0; i<array.length; i++){
if(array[i]!=element){
count++;
}
}
int[] newArray = new int[count];
int index = 0;
for(int i=0; i<array.length; i++){
if(array[i]!=element){
newArray[index++] = array[i];
}
}
return newArray;
}
public static void main(String[] args){
Test t = new Test();
int[] x = {1,2,3,4,6,6,7,8};
int[] newArray = t.deleteElement(x,6);
for(int v: newArray){
System.out.println(v);
}
}
}
//设计一个方法,用来存储给定范围内的素数(2-100)素数在自然数内
//是否需要提供条件 起始值begin 终点end 返回值 一个装满素数的数组
public class Test{
public int[] findPrimeNumber(int begin, int end){
if(begin<0 || end<0){
System.out.println("素数没有负数 不给你找啦");
return null;//自定义一个异常 人为规定的一种不正常现象
}
if(begin>=end){
System.out.println("您提供的范围有误 begin应该比end要小");
return null;//自定义一个异常 人为规定的一种不正常现象
}
//创建一个足够长的数组
int[] array = new int[(end-begin)/2];
int index = 0;//记录新数组的索引变化 同时记录个数
for(int num = begin; num<=end; num++){
boolean b = false;//标记
for(int i=2; i<=num/2; i++){
if(num%i==0){
b = true;
break;
}
}
if(!b){
array[index++] = num;
}
}
//将数组后面多余的0去掉
int[] primeArray = new int[index];
for(int i=0;i<primeArray.length;i++){
primeArray[i] = array[i];
}
array = null;
return primeArray;
}
public static void main(String[] args){
Test t = new Test();
int[] x = t.findPrimeNumber(100,10);
for(int v:x){
System.out.println(v);
}
}
}
//设计一个方法 对数组进行冒泡排序(既能升序又能降序)
//是否需要提供条件 数组 排序规则(升序 降序)返回值 没有
//flag==true升序排列 flag==false 降序排列
public class Test{
public void orderArray(int[] array, boolean flag){
for(int i=1;i<array.length;i++){//控制执行的轮次
for(int j=array.length-1;j>=i;j--){//控制比较的次数
if((flag==true && array[j]<array[j-1]) ||(flag==false && array[j]>array[j-1])){
int temp = array[j];
array[j] = array[j-1];
array[j-1] = temp;
}
}
}
}
public static void main(String[] args){
Test t = new Test();
int[] x = {5,4,3,2,1};
t.orderArray(x,true);
for(int v: x){
System.out.println(v);
}
}
}
//设计一个方法实现用户登录
//参数 输入用户名和密码 返回值 String类型 成功与否
import java.util.Scanner;
public class Test{
public String login(String user,String password){
//小数据库 存储真实用户名和密码
String[][] userBox ={{"郑中拓","123456"},{"渡一教育","666666"},{"Java","888"}};
//验证比较
String result = "用户名或密码错误";
for(int i=0;i<userBox.length;i++){
if(userBox[i][0].equals(user)){
if(userBox[i][1].equals(password)){
result = "登录成功";
}
break;
}
}
return result;
}
public static void main(String[] args){
Test t = new Test();
Scanner input = new Scanner(System.in);
System.out.println("请输入账号");
String user = input.nextLine();
System.out.println("请输入密码");
String password = input.nextLine();
String result = t.login(user,password);
System.out.println(result);
}
}