【发布时间】:2017-10-21 00:54:10
【问题描述】:
如果它与用户输入的值匹配,我正在尝试编写代码来创建特定对象。
例如:
假设我有 Person 类和 Car 类
public class Person
{
private int x, y ,z;
private String str;
//Constructors etc
}
public class Car
{
private int x,y,z;
private double doub;
//Constructors etc
}
要求用户输入 4 个不同的值。 Person 和 Car 的前三个字段相同,但如果第 4 个值是 String 或 double,则程序应创建匹配对象。
public class Input
{
public static void main (String args[])
{
int x,y,z;
?? other; //how do i declare if its either a double or string
Scanner input = new SCanner(System.in);
x = input.nextInt();
// y, z input
other = input.??? //String or double
//Create matching object
}
}
我该怎么办?
【问题讨论】:
-
检查是否可以将其解析为双精度:stackoverflow.com/questions/3543729/…
标签: java string validation input