2009-12-02 72 views
0

我只需要這個程序的一些幫助。 用戶必須輸入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; 

    } 

} 
+2

您的代碼已經在做你的要求。你能解釋你遇到的問題嗎? – 2009-12-02 01:51:24

+0

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

+0

除此之外,它看起來不錯:/ – 2009-12-02 01:54:26

回答

2

您需要在Java中聲明成員變量:

class PwLogin{ 

    private int idnum; 
    private int password; 
    private int maxtries; 
    private int maxuses; 

    public PwLogin(int id, int pw){ 
     etc... 
+0

那麼這將是用戶輸入的數字? 像我在我的主要閱讀的那些? 它們與主用戶輸入的值是否具有相同的值? – Violet 2009-12-02 02:01:08

+1

最初他們被設置爲零,但是在你將它們設置爲'this.idnum = id'之後那麼它們將被設置爲與您輸入的值相同的值。 – 2009-12-02 02:03:31

+0

謝謝你的幫助! =] – Violet 2009-12-02 02:04:41

相關問題