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

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

我想通过扫描仪询问用户 class 但我希望此扫描仪位于名为 readInput() 的方法中,并使用 gett-[=29 在名为 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; 

public class 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());
    
}   

}

将所有代码堆放在 main 中是个坏主意。 Java 是面向对象的,而 mainstatic,不是。

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

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 应用程序都应该是这样的:一个 one-liner main 方法,除了 main 之外的任何地方都不使用 staticscanner 字段,使用适当的分隔符设置(\R,这意味着 .nextX() 始终读取一行输入;使用 .next() 读取整行。不要call .nextLine(), ever. nextLine() 和所有其他 next 方法以令人讨厌的方式交互,使扫描器无用或至少笨拙且容易出现命令行 'prompting' 的错误,这就是为什么你这样做.