找到一个“用户输入”代码行的真正问题是允许2个程序(一个是构造函数)相互通信。我希望用户输入他/她的值,将该值转到构造函数,然后将其返回到原始程序并进行任何修改。提前致谢!
//condensed...
Scanner input = new Scanner(System.in);
System.out.println("Please enter a year between 2001 - 2025: ");
date = input.nextInt();
while(date < DATE_START || date > DATE_STOP)
{
System.out.println("You entered an incorrect value.");
System.out.println("You entered " + date);
System.out.print("Please enter a year between 2001 - 2025: ");
date = input.nextInt();
}
System.out.println("The number associate with this delivery is: " + ?????);
我无法找到如何访问相关程序,寻找用户输入此程序中的值,以及其他程序返回它以进行显示
//Other program condensed...
public class Delivery
{
private int year;
private int number;
private int code;
private double weight;
private double fee;
public int getYear()
{
return year;
}
public void setYear(int yr)
{
year = yr;
}
...//condensed
public Delivery()
{
year = ????
number =
code =
weight =
fee =
}
答案 0 :(得分:2)
您需要添加一个带参数的构造函数,然后让一个方法以您需要的形式返回值。
例如,您可以:
class Delivery {
private int year;
public Delivery(String date) {
year = Integer.parseInt(date);
}
public int getYear() {
return year;
}
}
然后在你的程序的其他部分你可以:
Delivery delivery = new Delivery(date);
int year = delivery.getYear();
嗯,不管怎么说......
答案 1 :(得分:1)
简单的答案,这是一般的Java。代码可以更简单。以下步骤。