如何获取用户输入并存储在自定义对象中?JAVA
亨洛, 基本上,我试图做的是获取用户输入并存储在自定义对象中,但我不知道如何进行。我已经创建了loadDataFromConfig()方法?当创建对象如何获取用户输入并存储在自定义对象中?JAVA,java,class,oop,Java,Class,Oop,亨洛, 基本上,我试图做的是获取用户输入并存储在自定义对象中,但我不知道如何进行。我已经创建了loadDataFromConfig()方法?当创建对象SmartHome app=new SmartHome(loadDataFromConfig())时,这可以正常工作。 但如何获取用户输入并以这种格式存储它们,我完全被难住了:dev[0]=新的智能设备(“设备1”,1.3,true) 所有要运行的代码都应该在Step1.java中的main方法中 下面是代码使用的3个类(忽略注释,它们只是我的注释
SmartHome app=new SmartHome(loadDataFromConfig())时,这可以正常工作代码>。
但如何获取用户输入并以这种格式存储它们,我完全被难住了:dev[0]=新的智能设备(“设备1”,1.3,true)代码>
所有要运行的代码都应该在Step1.java中的main方法中
下面是代码使用的3个类(忽略注释,它们只是我的注释):
打包SmartHomeApp;
公共级智能设备{
私有字符串名称;
私人双重定位;
私有布尔开关;
公共智能设备(字符串val1、双值val2、布尔值val3){
setName(val1);
设定位置(val2);
设置开关(val3);
}
//您无法访问“私有类”,因此需要获取它们
public void setName(字符串值){name=value;}
public void setLocation(双值){location=value;}
public void setSwitchedOn(布尔值){switchedOn=value;}
公共字符串getName(){return name;}
public double getLocation(){return location;}
公共布尔getSwitchedOn(){return switchedOn;}
}
打包SmartHomeApp;
公共级智能家居
{
私有智能设备[]smrtDev;
公共智能家庭(内部大小){
smrtDev=新智能设备[尺寸];
}
公共智能家庭(智能设备[]值){
smrtDev=值;
}
public int size(){return smrtDev.length;}
//因为某种原因不能执行toString()??
公共无效ToString(){
对于(int i=0;i,如建议,我尝试执行以下操作:
public static void main(String args[]) {
Scanner myObj = new Scanner(System.in);
System.out.println("Enter size: ");
int size = myObj.nextInt();
SmartDevice[] newList = new SmartDevice[size];
for(int i =0; i<newList.length;i++) {
System.out.println("Name: ");
String x = myObj.next();
System.out.println("Location: ");
double y = myObj.nextDouble();
System.out.println("Is on?: ");
boolean z = myObj.nextBoolean();
newList[i] = new SmartDevice(x,y,z);
}
SmartHome newDevice = new SmartHome(newList);
newDevice.ToString();
}
publicstaticvoidmain(字符串参数[]){
扫描仪myObj=新扫描仪(System.in);
System.out.println(“输入大小:”);
int size=myObj.nextInt();
SmartDevice[]新列表=新的SmartDevice[大小];
对于(int i=0;i中需要的一些改进如下:
遵循例如ToString()
应该是ToString()
。检查以了解有关ToString()
的更多信息。大多数IDE(例如eclipse)都提供了一个功能,可以在单击按钮时生成ToString()
方法。无论采用何种方式(手动或在IDE的帮助下)生成它时,它必须返回一个字符串
你应该放弃使用next()
,nextout()
,nextDouble()
等,而改用nextLine()
查看以了解更多信息。要了解next()
,nextDouble()
可能导致的问题,请尝试输入带有空格的名称,例如
下面给出了一个包含上述改进的示例代码:
import java.util.Scanner;
class SmartDevice {
private String name;
private double location;
private boolean switchedOn;
public SmartDevice(String val1, double val2, boolean val3) {
setName(val1);
setLocation(val2);
setSwitchedOn(val3);
}
// YOU CANT ACCESS the 'private classes' so you need to GET them
public void setName(String value) {
name = value;
}
public void setLocation(double value) {
location = value;
}
public void setSwitchedOn(boolean value) {
switchedOn = value;
}
public String getName() {
return name;
}
public double getLocation() {
return location;
}
public boolean getSwitchedOn() {
return switchedOn;
}
@Override
public String toString() {
return "SmartDevice [name=" + name + ", location=" + location + ", switchedOn=" + switchedOn + "]";
}
}
class SmartHome {
private SmartDevice[] smrtDev;
public SmartHome(int size) {
smrtDev = new SmartDevice[size];
}
public SmartHome(SmartDevice[] values) {
smrtDev = values;
}
public int size() {
return smrtDev.length;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
for (SmartDevice smartDevice : smrtDev) {
sb.append(smartDevice.toString()).append("\n");
}
return sb.toString();
}
}
public class Main {
public static void main(String[] args) {
Scanner myObj = new Scanner(System.in);
int size = getPositiveInt(myObj, "Enter size: ");
SmartDevice[] newList = new SmartDevice[size];
for (int i = 0; i < newList.length; i++) {
System.out.print("Name: ");
String x = myObj.nextLine();
double y = getFloatingPointNumber(myObj, "Location: ");
boolean z = getBoolean(myObj, "Is on?: ");
newList[i] = new SmartDevice(x, y, z);
}
SmartHome newDevice = new SmartHome(newList);
System.out.println(newDevice);
}
static int getPositiveInt(Scanner in, String message) {
boolean valid;
int n = 0;
do {
valid = true;
System.out.print(message);
try {
n = Integer.parseInt(in.nextLine());
if (n <= 0) {
throw new IllegalArgumentException();
}
} catch (IllegalArgumentException e) {
System.out.println("This in not a positive integer. Please try again.");
valid = false;
}
} while (!valid);
return n;
}
static double getFloatingPointNumber(Scanner in, String message) {
boolean valid;
double n = 0;
do {
valid = true;
System.out.print(message);
try {
n = Double.parseDouble(in.nextLine());
} catch (NumberFormatException | NullPointerException e) {
System.out.println("This in not a number. Please try again.");
valid = false;
}
} while (!valid);
return n;
}
static boolean getBoolean(Scanner in, String message) {
System.out.print(message);
return Boolean.parseBoolean(in.nextLine());
}
}
我建议您尝试在web上查找Java用户输入,这里有一个很好的示例:我已经看过了,但问题是这只显示了如何将输入存储为单个变量,而不是存储在包含值数组的对象中。提示:您可以将变量用作新的表达式的参数。这给了您一些想法吗?您有什么想法吗你认为在效率方面可以改进吗?回答:不太多(:-),但问问自己是否有什么可以做得更好总是好的。但在这种情况下,你确实做了必要的事情,而且(最重要的是)没有了,你再也没有比这更有效的了。谢谢凯文,谢谢你的建议!谢谢你的详细解释。我不需要在这项工作中做任何错误检查,但这无疑会在以后的任何编程问题/工作中派上用场。我可以看出你在“隐藏机制”,我们通常我会这样做:public int getIntInput(字符串提示){Scanner input=new Scanner(System.in);display(prompt);return input.nextInt();}
但是这样更好:)
import java.util.Scanner;
class SmartDevice {
private String name;
private double location;
private boolean switchedOn;
public SmartDevice(String val1, double val2, boolean val3) {
setName(val1);
setLocation(val2);
setSwitchedOn(val3);
}
// YOU CANT ACCESS the 'private classes' so you need to GET them
public void setName(String value) {
name = value;
}
public void setLocation(double value) {
location = value;
}
public void setSwitchedOn(boolean value) {
switchedOn = value;
}
public String getName() {
return name;
}
public double getLocation() {
return location;
}
public boolean getSwitchedOn() {
return switchedOn;
}
@Override
public String toString() {
return "SmartDevice [name=" + name + ", location=" + location + ", switchedOn=" + switchedOn + "]";
}
}
class SmartHome {
private SmartDevice[] smrtDev;
public SmartHome(int size) {
smrtDev = new SmartDevice[size];
}
public SmartHome(SmartDevice[] values) {
smrtDev = values;
}
public int size() {
return smrtDev.length;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
for (SmartDevice smartDevice : smrtDev) {
sb.append(smartDevice.toString()).append("\n");
}
return sb.toString();
}
}
public class Main {
public static void main(String[] args) {
Scanner myObj = new Scanner(System.in);
int size = getPositiveInt(myObj, "Enter size: ");
SmartDevice[] newList = new SmartDevice[size];
for (int i = 0; i < newList.length; i++) {
System.out.print("Name: ");
String x = myObj.nextLine();
double y = getFloatingPointNumber(myObj, "Location: ");
boolean z = getBoolean(myObj, "Is on?: ");
newList[i] = new SmartDevice(x, y, z);
}
SmartHome newDevice = new SmartHome(newList);
System.out.println(newDevice);
}
static int getPositiveInt(Scanner in, String message) {
boolean valid;
int n = 0;
do {
valid = true;
System.out.print(message);
try {
n = Integer.parseInt(in.nextLine());
if (n <= 0) {
throw new IllegalArgumentException();
}
} catch (IllegalArgumentException e) {
System.out.println("This in not a positive integer. Please try again.");
valid = false;
}
} while (!valid);
return n;
}
static double getFloatingPointNumber(Scanner in, String message) {
boolean valid;
double n = 0;
do {
valid = true;
System.out.print(message);
try {
n = Double.parseDouble(in.nextLine());
} catch (NumberFormatException | NullPointerException e) {
System.out.println("This in not a number. Please try again.");
valid = false;
}
} while (!valid);
return n;
}
static boolean getBoolean(Scanner in, String message) {
System.out.print(message);
return Boolean.parseBoolean(in.nextLine());
}
}
Enter size: x
This in not a positive integer. Please try again.
Enter size: -2
This in not a positive integer. Please try again.
Enter size: 10.5
This in not a positive integer. Please try again.
Enter size: 2
Name: Light Amplification by Stimulated Emission of Radiation
Location: 123.456
Is on?: true
Name: Vacuum Diode
Location: 234.567
Is on?: no
SmartDevice [name=Light Amplification by Stimulated Emission of Radiation, location=123.456, switchedOn=true]
SmartDevice [name=Vacuum Diode, location=234.567, switchedOn=false]