我已经尝试了很多方法,包括这里类似问题的解决方案,但仍然没有乐趣。
我正在使用扫描仪获取两个变量 patentName
和 patentAddress
的值,然后我希望将这些值传输到另一个类,但是当我尝试时这些值以 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();
关于java - 如何使变量值可在另一个类中使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29013657/