抱歉我的英语不好,我有2个问题让我困惑。第一个问题我希望用户输入3个东西(名字,联系人,idNumber),但当我尝试输入名称时,名称输入位置丢失。我发现解决方案输入位置丢失解决方案,当用(scan.nextLine())修复它时,另一个问题出来了,输入丢失了。第二个问题是如何将构造函数分配给数组。
public class newClient {
Scanner scan= new Scanner(System.in);
Random rand=new Random();
static int IDumber;
String unique;
int pin;
static int count = 0;
private String name;
private String idNumber;
private String contact;
public newClient(){
setContact(contact);
setIdNumber(idNumber);
setName(name);
count++;
}
public String getName(){
return name;
}
public void setName(String name){
this.name=name;
}
public String getIdNumber(){
return idNumber;
}
public void setIdNumber(String idNumber){
this.idNumber=idNumber;
}
public String getContact(){
return contact;
}
public void setContact(String contact){
this.contact=contact;
}
public int uniqueID(){
unique = idNumber.substring(8);
IDumber = Integer.parseInt(unique);
return IDumber;
}
public void display(){
System.out.println("This is the password for first time login "+pin);
}
public int pinCode(){
pin = rand.nextInt(9000)+1000;
return pin;
}
public String toString(){
return "Name: "+name+"\nUniqueID: "+IDumber+"\nContact: "+getContact()+"\npinCode: "+pin ;
}
public void admin(){
newClient []nc = new newClient[10];
login();
while(true){
System.out.println("Select 1:add Client\n 2:add Account\n 3:login as Client");
try{
int selection = scan.nextInt();
switch(selection)
{
case 1: addClient(nc);
break;
case 2: addAccount(nc);
break;
case 3: client.client();
break;
default: System.out.println("INvalid selection");
}
}
catch(InputMismatchException ex){
System.out.println("Invalid input");
scan.nextLine();
}
}
}
public void addClient(newClient []nc){
newClient nw = new newClient();
System.out.println("Enter name");
nw.setName(scan.nextLine());
scan.nextLine();
System.out.println("Enter contact");
nw.setContact(scan.nextLine());
System.out.println("Enter id number");
nw.setIdNumber(scan.nextLine());
nc[newClient.count]= new newClient();
System.out.println("count"+nc[newClient.count]);
nw.pinCode();
nw.uniqueID();
System.out.println(nw.toString());
}
}
答案 0 :(得分:1)
首先,我不明白你在做什么以及你想要问什么。 其次,我意识到你是Java的新手。虽然,你甚至不知道如何编码,我建议你在开始编码之前阅读一些材料书。你的代码有一些重要的流程和错误,其中一些我在下面纠正过。我也描述了其中一些。
代码从这里开始。
import java.util.InputMismatchException;
import java.util.Random;
import java.util.Scanner;
`public class Client` {
private String name;
private String idNumber;
private String contact;
private String unique;
private int pin;
//Constructor is for initialize values and basic functionality
public Client()
{
name = null;
idNumber = null;
contact = null;
unique = null;
pin = 0;
}
//Thats how you can interact with constructor
public Client(String name, String idNumber, String contact, String unique, int pin)
{
this.name = name;
this.idNumber = idNumber;
this.contact = contact;
this.unique = unique;
this.pin = pin;
}
public String getName(){
return name;
}
public void setName(String name){
this.name=name;
}
public String getIdNumber(){
return idNumber;
}
public void setIdNumber(String idNumber){
this.idNumber=idNumber;
}
public String getContact(){
return contact;
}
public void setContact(String contact){
this.contact=contact;
}
public String getUnique(){
return unique;
}
public void setUnique(String unique){
this.unique=unique;
}
public int getPin(){
return pin;
}
public void setPin(int pin){
this.pin=pin;
}
@Override
public String toString() {
return "Client [name=" + name + ", idNumber=" + idNumber + ", contact=" + contact + ", unique=" + unique
+ ", pin=" + pin + "]";
}
}
class NewClient
{
public int uniqueId(String idNumber){
String unique = idNumber.substring(8);
return Integer.parseInt(unique);
}
public void display(int pin){
System.out.println("This is the password for first time login "+pin);
}
public int pinCode(){
Random rand = new Random();
return rand.nextInt(9000)+1000;
}
public void admin(){
Client []nc = new Client[10];
@SuppressWarnings("resource")
Scanner scan = new Scanner(System.in);
//You have somes method like login(), and i don't know what they are, so i comment rhem..
//login();
while(true){
System.out.println("Select 1:add Client\n 2:add Account\n 3:login as Client");
try{
int selection = scan.nextInt();
switch(selection)
{
case 1: nc[1] = addClient();
break;
case 2: //addAccount(nc);
break;
case 3: //client.client();
break;
default: System.out.println("INvalid selection");
}
}
catch(InputMismatchException ex){
System.out.println("Invalid input");
scan.nextLine();
}
}
}
public Client addClient(){
Client client = new Client();
Scanner scan = new Scanner(System.in);
System.out.println("Enter name");
client.setName(scan.nextLine());
scan.nextLine();
System.out.println("Enter contact");
client.setContact(scan.nextLine());
System.out.println("Enter id number");
client.setIdNumber(scan.nextLine());
client.setPin(pinCode());
client.setUnique(String.valueOf(uniqueId(client.getIdNumber())));
System.out.println(client.toString());
return client;
}
}
我不知道,我回答你的问题,但你应该尝试这种方法!......问候