2017-03-03 86 views
-1

我有一個類和一個公共類,並且想要在該類中爲用戶輸入的數據分配一個變量。是否有可能以這種方式做到這一點,或者我必須移動變量。Java:調用一個類變量併爲其分配一個值

這是我的代碼

import java.util.Scanner; 

class People { 
    String name; 
    int age; 
    String hair_colour; 
    String personality; 

    void confirm(){ 
     System.out.println(name +" is " + age + " years old and has " 
      + hair_colour + " hair and has a " + personality +" personality"); 
     System.out.println("Is this correct? Y/N"); 
     Scanner correct = new Scanner(System.in); 
     String reply = correct.nextLine(); 
     if(reply=="Y" || reply=="y"){ 
      System.out.println("Profile confirmed. Thank you."); 
     } 
     else { 
      System.out.println("Returning to profile creation"); 
     } 
    } 
} 

public class PeopleProfile { 

    public static void main(String[]args){ 
     Scanner input_details = new Scanner(System.in); 
     System.out.println("Please enter a name: "); 
     String given_name = input_details.nextLine(); 
     System.out.println("Please enter a age: "); 
     int given_age = input_details.nextInt(); 
     System.out.println("Please enter the persons hair colour: "); 
     String given_hair_colour = input_details.nextLine(); 
     System.out.println("Please enter the persons personality: "); 
     String given_personality = input_details.nextLine(); 
    } 
} 
+2

你的代碼是顯著難度比閱讀如果你按照慣例格式化了它,那麼最好使用常規名稱。另外,請參閱http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java。但請注意,您目前甚至沒有創建「People」實例... –

+0

爲了澄清我試圖將given_name分配給名稱(因爲在嘗試在其中調用名稱時出現錯誤)等等的輸入。 – Nathan

+3

@Nathan沒有人的實例,沒有分配的名稱。您必須首先實例化People對象,然後才能爲該對象指定名稱。 –

回答

1

試試這個:

import java.util.Scanner; 

class People { 
    String name; 
    int age; 
    String hairColour; 
    String personality; 

    void confirm() { 
     System.out.println(name + " is " + age + " years old and has " + hairColour + " hair and has a " + personality + " personality"); 

     Scanner correct = new Scanner(System.in); 

     System.out.println("Is this correct? Y/N"); 
     String reply = correct.nextLine(); 

     if ("Y".equals(reply) || "y".equals(reply)) { 
      System.out.println("Profile confirmed. Thank you."); 
     } else { 
      System.out.println("Returning to profile creation"); 
     } 
    } 
} 

public class PeopleProfile { 
    public static void main(String[] args) { 
     People boy = new People(); 
     Scanner inputDetails = new Scanner(System.in); 

     System.out.println("Please enter a name: "); 
     boy.name = inputDetails.nextLine(); 

     System.out.println("Please enter a age: "); 
     boy.age = Integer.parseInt(inputDetails.nextLine()); 

     System.out.println("Please enter the persons hair colour: "); 
     boy.hairColour = inputDetails.nextLine(); 

     System.out.println("Please enter the persons personality: "); 
     boy.personality = inputDetails.nextLine(); 

     boy.confirm(); 
    } 
} 
+0

謝謝,這工作完美。 – Nathan

0

您可以通過此某個變量創建setter ASIGN變量。

因此,例如,你的財產

String hair_colour; 

應當具備下列方法

public void setHairColour(string hair_colourIn) { 
    hair_colour = hair_colourIn; 
} 

你會調用該財產的任何其他方法在同級車

People people = new People(); 
people.setHairColour(-your input string -) 

要到來自不同班級的值,您需要一個getter 在給定的例子財產hair_colour本應是如下:

public string getHairColour() { 
    return hair_colour; 
} 

所以你最終有:

People people = new People(); 
string hairColour = people.getHairColour(); 
-1

你不能超載你的人類的構造函數?
它看起來就像這樣:

class People { 
String name; 
int age; 
String hair_colour; 
String personality; 

public People(String name, int age, String hair_colour, String personality){ 
    this.name = name; 
    this.age = age; 
    this.hair_colour = hair_colour; 
    this.personality = personality; 
} 

void confirm(){ 
    System.out.println(name +" is " + age + " years old and has " + hair_colour + " hair and has a " + personality +" personality"); 
    System.out.println("Is this correct? Y/N"); 
    Scanner correct = new Scanner(System.in); 
    String reply = correct.nextLine(); 
    if(reply.equals("Y") || reply.equals("y")){ 
     System.out.println("Profile confirmed. Thank you."); 
    } 
    else{ 
     System.out.println("Returning to profile creation"); 
    } 
} 
} 

然後在你的主,你會說它是這樣:

public class PeopleProfile { 
public static void main(String[]args){ 
    Scanner input_details = new Scanner(System.in); 
    System.out.println("Please enter a name: "); 
    String given_name = input_details.nextLine(); 
    System.out.println("Please enter a age: "); 
    int given_age = input_details.nextInt(); 
    System.out.println("Please enter the persons hair colour: "); 
    String given_hair_colour = input_details.nextLine(); 
    System.out.println("Please enter the persons personality: "); 
    String given_personality = input_details.nextLine(); 
    People myPeople = new People(given_name, given_age, given_hair_colour, given_personality); 
    myPeople.confirm(); 

} 
+0

進行比較不要在字符串比較中使用'=='。 –