Java课后练习
public class Login {
public static void main(String[] args) {
Users user = new Users();
user.setLogin(0);
int isok=1;
Scanner scanner= new Scanner(System.in);
while (user.getLogin()==0){
System.out.println("请输入账号");
String username=scanner.next();
System.out.println("请输入密码");
String userpwd=scanner.next();
if ("zzm".equals(username)&&"123".equals(userpwd)&&isok<4){
user.setLogin(1);
break;
}else if (isok>2){
System.out.println("错误3次,系统退出");
System.exit(0);
}else {
System.out.println("密码错误请重新输入");
}
isok++;
}
System.out.println("密码成功,欢迎您");
while(1==1){
System.out.println("*********************");
System.out.println("1.抽奖 2.猜数字 3.排序功能 4.查找功能 5.退出系统");
System.out.println("*********************");
int i=scanner.nextInt();
switch (i){
case 1:
System.out.println("抽中的数字:"+ Math.random());break;
case 2:
System.out.println("请输入一个数字");
int k=scanner.nextInt();
int l;
if ((l=new Random().nextInt(100))==k){
System.out.println("恭喜你猜对了");
}else {
System.out.println("很遗憾猜错了"+l);
}
break;
case 3:
int[] arr=init();
bubbleSort(arr);
break;
case 4:
int[] arr1=init();
find(arr1);
break;
case 5:System.exit(0);break;
default:
}
}
}
public static int[] init(){
System.out.println("请输入5个数");
Scanner scanner= new Scanner(System.in);
int [] arr=new int[5];
for (int i = 0; i < arr.length; i++) {
arr[i]=scanner.nextInt();
}
return arr;
}
public static void bubbleSort(int[] arr){
for (int i = 0; i < arr.length; i++) {
for (int j = 1; j < arr.length-i; j++) {
if (arr[j-1]>arr[j]){
int temp=arr[j];
arr[j]=arr[j-1];
arr[j-1]=temp;
}
}
}
System.out.println(Arrays.toString(arr));
}
public static void find(int [] arr){
System.out.println("请输入需要查找的数");
Scanner scanner =new Scanner(System.in);
int i=scanner.nextInt();
for (int j = 0; j < arr.length; j++) {
if (i==arr[j]){
System.out.println("找到位置:"+(j+1));
}
}
}