如何将扫描仪放在方法中,并将其输出放在不同的方法中

How to put scanner inside a method and its output on a different method

提问人:bruuh 提问时间:3/3/2022 更新时间:3/3/2022 访问量:288

问:

我想通过一个 scanner 类询问用户,但我希望这个scanner 位于一个名为 readInput() 的方法中,并使用 gett-setter 在一个名为 writeOutput() 的不同方法上输出

这是我在下面的代码:

public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        LaboratoryExercise2 gro = new LaboratoryExercise2();
        String[] SecondQuestion;
        System.out.println("Select the item your purchasing.");
            String Product1  = sc.nextLine();
        System.out.println("Enter the Quantity and price separated by SPACE.");
            SecondQuestion = sc.nextLine().split(" ");
                int Quantity1 = Integer.parseInt(SecondQuestion[0]);
                double Price1 = Double.parseDouble(SecondQuestion[1]);
                double Amount1 = 0;
                Amount1 = Quantity1 * Price1;
                int Quantity = 0 + Quantity1;
                double Amount = 0 + Amount1;
                double Price = 0 + Price1;

我希望它的输出显示在不同的方法上

        gro.setGrocery(Price, Quantity, Amount);
    System.out.println("You've selected " + gro.getitemQuantity() + " " + Product1 + " " + "at " + " " + 
                                                                            gro.getitemPrice() + " each");

    System.out.println("Amount due is " + gro.getamountDue());

这是我的整个代码:

import java.util.Scanner; 

公共类 LaboratoryExercise2 {

private double itemPrice;
private int itemQuantity;
private double amountDue;


public void setGrocery(double newitemPrice, int newitemQuantity, double newamountDue) {

    itemPrice = newitemPrice;
    itemQuantity = newitemQuantity;
    amountDue = newamountDue;
}

public double getitemPrice() {
    return itemPrice;
    
}

public int getitemQuantity() {
    return itemQuantity;
    
}

public double getamountDue() {
    return amountDue;
    
}



public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    
    
    LaboratoryExercise2 gro = new LaboratoryExercise2();
    String[] SecondQuestion;

    System.out.println("Select the item your purchasing.");
        String Product1  = sc.nextLine();
        
        
        
    System.out.println("Enter the Quantity and price separated by SPACE.");
        SecondQuestion = sc.nextLine().split(" ");
        
            int Quantity1 = Integer.parseInt(SecondQuestion[0]);
            double Price1 = Double.parseDouble(SecondQuestion[1]);
            double Amount1 = 0;
            Amount1 = Quantity1 * Price1;
            int Quantity = 0 + Quantity1;
            double Amount = 0 + Amount1;
            double Price = 0 + Price1;

    gro.setGrocery(Price, Quantity, Amount);
    System.out.println("You've selected " + gro.getitemQuantity() + " " + Product1 + " " + "at " + " " + 
                                                                            gro.getitemPrice() + " each");

    System.out.println("Amount due is " + gro.getamountDue());
    
}   

}

方法 java.util.scanner getter-setter

评论


答:

0赞 rzwitserloot 3/3/2022 #1

把所有的代码都堆进去是个坏主意。Java 是面向对象的,而 是 不是。mainmainstatic

在方法之间共享数据的解决方案通常是创建一个字段。

class Main {
  public static void main(String[] args) throws Exception {
    new Main().go();
  }

  private Scanner scanner;

  void go() throws Exception {
    scanner = new Scanner(System.in);
    scanner.useDelimiter("\\R");

    int quantity = askInt("Enter the quantity: ");
  }

  private int askInt(String prompt) {
    while (true) {
      System.out.print(prompt);
      if (scanner.hasNextInt()) return scanner.nextInt();
      scanner.next(); // eat the non-int token
      System.out.println("ERROR: Please enter an integer number.");
    }
  }
}

这几乎是所有命令行 java 应用程序都应该有的样子:一个单行方法,除了 、 字段之外,不使用任何地方,设置有适当的分隔符 (,这意味着总是读取一行的输入;用于读取整行。永远不要打电话。 所有其他接下来的方法都以令人讨厌的方式进行交互,使扫描程序无用,或者至少在命令行“提示”中笨拙且容易出错,这就是您这样做的原因。mainstaticmainscanner\\R.nextX().next().nextLine()nextLine()