Java:调用类变量并在主变量中为其赋值
我有一个类和一个公共类,并希望从该类中分配一个变量,该类包含用户在公共类中输入的数据。是否可以这样做,或者我必须移动变量 这是我的密码Java:调用类变量并在主变量中为其赋值,java,class,variables,Java,Class,Variables,我有一个类和一个公共类,并希望从该类中分配一个变量,该类包含用户在公共类中输入的数据。是否可以这样做,或者我必须移动变量 这是我的密码 import java.util.Scanner; class People { String name; int age; String hair_colour; String personality; void confirm(){ System.out.println(name +" is " +
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();
}
}
您可以通过为此特定变量创建一个
设置器
来指定该变量
比如说你的财产
String hair_colour;
应采用以下方法
public void setHairColour(string hair_colourIn) {
hair_colour = hair_colourIn;
}
您将调用此属性作为其类中的任何其他方法
People people = new People();
people.setHairColour(-your input string -)
要从其他类获取值,需要一个getter
在给定的示例属性头发颜色中,应如下所示:
public string getHairColour() {
return hair_colour;
}
所以你最终会:
People people = new People();
string hairColour = people.getHairColour();
您不能为您的人员类重载构造函数吗?
看起来是这样的:
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();
}
试试这个:
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();
}
}
您的代码要比按常规格式(理想情况下使用常规名称)更难阅读。此外,请参见。但请注意,目前您甚至没有创建人员的实例
…为了澄清,我正在尝试为所有输入分配给定的名称(因为我在那里尝试调用名称时出错)等等。@Nathan没有人员的实例,就没有可分配的名称。您首先必须实例化一个人员对象,然后才能为该对象指定名称。切勿将字符串与==