Java 如何使变量值在另一个类中可用?
我尝试了很多方法,包括这里类似问题的解决方案,但仍然没有乐趣 我正在使用扫描仪获取两个变量Java 如何使变量值在另一个类中可用?,java,inheritance,constructor,Java,Inheritance,Constructor,我尝试了很多方法,包括这里类似问题的解决方案,但仍然没有乐趣 我正在使用扫描仪获取两个变量patientName和patientAddress的值,然后我希望将这些值传输到另一个类,但当我尝试时,这些值显示为null。这是代码 import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.util.Scanner; pu
patientName
和patientAddress
的值,然后我希望将这些值传输到另一个类,但当我尝试时,这些值显示为null
。这是代码
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class Patients {
String patientName;
String patientAddress;
public void patientDetails() {
Scanner patientScanner = new Scanner(System.in);
System.out.println("Please input a patients name");
patientName = patientScanner.nextLine();
System.out.println("Please input a patients address");
String patientAddress = patientScanner.nextLine();
}
public void printInfo() {
System.out.println(patientName);
System.out.println(patientAddress);
}
public String getName() {
return this.patientName;
}
public String getAddress() {
return this.patientAddress;
}
}
我想是可变范围 更改此项:
String patientAddress = patientScanner.nextLine();
对此
patientAddress = patientScanner.nextLine();
你如何打电话获取值?第二个类在哪里?你从哪里得到错误?除了
patientAddress=patientScanner.nextLine()
,我很确定你的问题不在Patients
类中:恐怕不是这样,它们仍然是空值。