java如何用数组新建用户名和密码?

2025-03-03 17:54:16
推荐回答(3个)
回答1:

import java.util.ArrayList;
import java.util.Scanner;
/**
 * 
 * @author young
 *
 */
class People {
private String name;
private String password;
private double height;
private double tz;
private String blood;

public People(String name, String password, double height, double tz,
String blood) {
super();
this.name = name;
this.password = password;
this.height = height;
this.tz = tz;
this.blood = blood;
}

public static ArrayList addPeople(){
ArrayList al = new ArrayList();
Scanner sc = new Scanner(System.in);
System.out.print("请输入姓名:");
String n = sc.next();
System.out.print("请输入密码:");
String pa = sc.next();
System.out.print("请输入身高:");
double h = sc.nextDouble();
System.out.print("请输入体重:");
double t = sc.nextDouble();
System.out.print("请输入血型:");
String b = sc.next();
People p = new People(n, pa, h, t, b);
al.add(p);
System.out.println("添加用户成功!");
return al;
}

public static int cd(){
int i ;
System.out.println("*********************");
System.out.println("*******菜      单********");
System.out.println("***** 1   添加*******");
System.out.println("***** 2   查找*******");
System.out.println("***** 0   退出*******");
System.out.println("*********************");
Scanner sc = new Scanner(System.in);
System.out.print("请输入你的选择:");
i = sc.nextInt();
return i;
}

public static void find(ArrayList al){
Scanner sc = new Scanner(System.in);
System.out.print("请输入查找姓名:");
String n = sc.next();
System.out.print("请输入查找密码:");
String pa = sc.next();
for(People p : al){
if(p.name.equals(n) && (p.password.equals(pa))){
System.out.println("姓名:" + p.name + " 身高:" + p.height + " 体重:" + p.tz + " 血型:" + p.blood);
}else { 
System.out.println("对不起!查无此人.");
}
}
}

public static void main(String[] args) {
int i ;
ArrayList al = new ArrayList();
i = cd();
while(i != 0){
switch (i) {
case 1:
al = addPeople();
break;
case 2:
find(al);
break;
case 0:
System.out.println("退出系统!");
break;
default:
break;
}
i = cd();
}
}
}

回答2:

你好:用数组的话,可以先新建一个user用户,然后每次给这个user增加属性完成后保存到List里面这样的话,你的就是一个数组对象,然后输出的时候直接匹配你的用户名密码,发现符合条件的直接输出就可以了。

回答3:

你得用数据库