找到“用户输入”代码行确实存在问题,该行允许两个程序(一个是构造函数)相互通信。我希望让用户输入他/她的值,将该值传递给构造函数,然后将其进行任何修改后返回给原始程序。提前致谢!

  //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 =
}

最佳答案

您将需要添加一个带参数的构造函数,然后有一个以所需形式返回值的方法。

例如,您可能有:

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();

好吧,无论如何...

关于java - Java:将用户输入链接到构造函数并返回,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22587177/

10-11 00:28