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

发布于 2025-01-11 08:46:50 字数 2991 浏览 0 评论 0原文

我想通过扫描器类询问用户,但我希望该扫描器位于名为 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; 

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

}

I want to ask the user through a scanner class but I want this scanner to be in a method named readInput() and the output on a different method named writeOutput() using gett-setter

this is my code below:

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;

I want the output of this to show on a different method

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

This is my whole code:

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

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

素年丶 2025-01-18 08:46:50

将所有代码堆放在 main 中是一个坏主意。 Java 是面向对象的,而 main 作为静态,则不是。

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

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 应用程序都应该是这样的:一个单行 main 方法,除了 main 之外的任何地方都不使用 static,一个 < code>scanner 字段,使用正确的分隔符设置(\\R,这意味着 .nextX() 始终读取一行输入;使用 .next() 读取整行。永远不要调用 .nextLine() 以及所有其他 next 方法以令人讨厌的方式进行交互,这会使扫描器变得毫无用处,或者至少难以操作且容易出错。 “提示”行,这就是你这样做的原因。

Piling all your code into main is a bad idea. Java is object oriented, and main, being static, isn't.

The solution for sharing data between methods is usually to make a field.

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.");
    }
  }
}

That's what virtually all command line java apps should look like: a one-liner main method, no use of static anywhere except main, a scanner field, set with the proper delimiter (\\R, which means .nextX() always reads one line worth of input; use .next() to read an entire line. Don't call .nextLine(), ever. nextLine() and all the other next methods interact in nasty ways that make scanner useless or at least unwieldy and bugprone for command line 'prompting', which why you do it this way.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文