简体   繁体   English

在 Scanner 输入中使用类、参数、对象

[英]Using classes , parameters, objects with Scanner input

I have a class City.java and State.java with automatically generated constructors, getters and setters (if needed I can add them to the question).我有一个 City.java 和 State.java 类,其中包含自动生成的构造函数、getter 和 setter(如果需要,我可以将它们添加到问题中)。

In Main.java my task is to get information with Scanner as input and make objects with all the information written in.Main.java我的任务是使用Scanner作为输入获取信息,并使用写入的所有信息制作对象。

Data input for each object is needed to be taken out in separate methods that will be eventually called from main method.每个对象的数据输入需要在最终从 main 方法调用的单独方法中取出。 Each of these methods must return the object that fills.这些方法中的每一个都必须返回填充的对象。 It's allowed to only one object of class Scanner .只允许一个Scanner类的对象。

public Class City { 
   private String name;
   private Bigdecimal surface;
   //+constructor , + getters and setters
}
public Class State {
   private String name;
   private City city;
 //+constructor , + getters and setters
}

public class Main {

    public static City city (Scanner scanner) {
       System.out.println("Store City name : ");
       String cityName = scanner.nextLine();
       System.out.println("Store City surface : ");
       BigDecimal citySurface = scanner.nextBigDecimal();

    return new City(cityName,citySurface);
    }

    public static State state (Scanner scanner){
      System.out.println("Store State name : ");
      String stateName = scanner.nextLine();

I'm lost at this point.我在这一点上迷路了。 The task is that I should return new State with parameters (stateName,City) but I can't figure out how to pass the previously entered city here as a parameter.任务是我应该返回带有参数 (stateName,City) 的新状态,但我不知道如何将先前输入的城市作为参数传递给这里。

      }


  public static void main(String[] args) {
    Scanner mainScanner = new Scanner (System.in);
  }
}

You could try this:你可以试试这个:

public class Main {

  public static City city (Scanner scanner) {
   System.out.println("Store City name : ");
   String cityName = scanner.nextLine();
   System.out.println("Store City surface : ");
   BigDecimal citySurface = scanner.nextBigDecimal();

   return new City(cityName,citySurface);
  }

  public static State state (Scanner scanner, City city){
    System.out.println("Store State name : ");
    String stateName = scanner.nextLine();
    //Do stuff here with city e.g. city.getName()
    return new State(stateName, city);
  }

  public static void main(String[] args) {
    Scanner mainScanner = new Scanner (System.in);
    City myCity = city(scanner);
    state(mainScanner, myCity);
    //I'm sure you could also do this code in shorthand like below because of the callstack order.
    //state(mainScanner, city(scanner));
  }

}

You just need to pass the city object through to the state and then handle the code that should happen once you have it.您只需要将城市对象传递给州,然后处理一旦拥有它就应该发生的代码。 To be honest I don't think you should have those methods there in the Main class and as statics and stuff, but that is another issue entirely.老实说,我认为您不应该在 Main 类中将这些方法作为静态和东西使用,但这完全是另一个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM