我只需要這個程序的一些幫助。 用戶必須輸入ID,密碼,最大嘗試次數&最大使用次數。 他們必須進入構造函數... 有人可以幫我把它們傳遞給構造函數嗎? 我正在使用java。 謝謝!如何將用戶輸入變量傳入構造函數?
import java.util.Scanner;
public class LoginPw{
public static void main(String[] args){
// TODO Auto-generated method stub
Scanner reader = new Scanner(System.in);
System.out.println("Enter in your id and password");
int idnum = reader.nextInt();
int password = reader.nextInt();
System.out.println("Enter in the max number of tries");
int maxtries = reader.nextInt();
System.out.println("Enter in the number of max uses");
int maxuses = reader.nextInt();
PwLogin pwl = new PwLogin(idnum,password,maxtries,maxuses);
}}
class PwLogin{
public PwLogin(int id, int pw){
//assumes max num of tries is infinite. denote by value of 0.
this(id,pw,0,0); //call the 4-argument constructor w/ maxTries & maxUses defaulted as zero(infinite)
}
public PwLogin(int id, int pw, int tries){
//assumes max number of uses is infinite. denote by value of 0.
this(id, pw, tries,0); //Call the 4-argument constructor with maxUses defaulted as zero (infinite)
}
public PwLogin(int id, int pw, int tries, int uses){
// Now set the 4 class variables from the passed-in arguments
this.idnum =id;
this.password = pw;
this.maxtries=tries;
this.maxuses=uses;
}
}
您的代碼已經在做你的要求。你能解釋你遇到的問題嗎? – 2009-12-02 01:51:24
oh publicPwLogin(int id,int pw,int tries,int uses){ does not work for「this.idnum = id;」 。或任何這樣的 ,我不知道如何解決它 或爲什麼它的錯......從丟失的'idnum','password'等領域 – Violet 2009-12-02 01:53:36
除此之外,它看起來不錯:/ – 2009-12-02 01:54:26