有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何正确调用main方法中的其他方法

我在调用主方法上的方法时遇到问题。这是我在主要方法中需要的:

打印横幅消息

获取产品列表

从用户处获取产品订单,并检查其是否存在于产品中

ArrayList

如果产品存在

了解产品价格

计算产品税

计算总销售额

输出总销售额

否则

输出“未找到产品”

import java.util.ArrayList;
import java.util.Scanner;

public class Unit6ProblemSet {


public static void main(String[] args) {

    bannerPrinter();
    ArrayList<String> products = productBuilder();
    Boolean productExists = getOrder(products);
    if(productExists) {
        double price = getPrice();
        getTax(tax);
        getTotal(saleTotal);
        printTotal(saleTotal);
    }
    else {
        System.out.println("Product not found.");
    }    
}

public static void bannerPrinter() {

    System.out.println("******************************************");
    System.out.println("****** Welcome to my eCommerce app! ******");
    System.out.println("******************************************");
    System.out.println();
}

public static ArrayList<String> productBuilder() {

    ArrayList<String> products = new ArrayList<String>();

    products.add("Headphones");
    products.add("Pencils");
    products.add("Pens");
    products.add("Computers");
    products.add("Videogames");

    return products;
}

public static boolean getOrder(ArrayList<String> products) {

    Scanner scnr = new Scanner(System.in);

    String userStr = "";

    System.out.println("Enter a product: ");
    userStr = scnr.nextLine();

    boolean productName = products.contains(userStr);

    if (productName) {
        System.out.println("True");
    }
    else {
        System.out.println("False");
    }

    return productName;
}

public static double getPrice() {

    double price = 0.0;
    price = (Math.random() + 1) * 100;

    return price;
}

public static double getTax(double price) {

    double tax = 0.0;
    tax = price * 0.10;

    return tax;
}

public static double getTotal(double price, double tax) {

    double saleTotal = 0.0;
    saleTotal = price + tax;

   return saleTotal;
}

public static void printTotal(double saleTotal) {

    System.out.println("Your sale total is: " + saleTotal);
}

}

我只是在调用不同的方法时遇到了问题


共 (2) 个答案

  1. # 1 楼答案

    你走在正确的道路上

    bannerPrinter();
    ArrayList<String> products = productBuilder();
    

    主方法中的第一行bannerPrinter()调用bannerPrinter方法

    现在,对于productBuilder()的第二次调用,您基本上得到了一个结果。现在使用这个结果,你可以看到产品是否存在,得到产品价格等等

    所以在排序中,如果你想从main调用一个方法,你所要做的就是使用方法名来进行调用

    对于具有getOrder(ArrayList<String> products)等参数的方法,必须将参数传递给该方法

    在您的示例中,它将是getOrder(products)。这将使用正确的参数调用该方法,并得到boolean结果

    此外,根据最近的编辑,当您调用具有返回类型的方法时,例如getOrder(),您需要有一个获得结果的变量

    productBuilder()返回一个List<String>所以你这么做了

    ArrayList<String> products = productBuilder();基本上就是说,获取我的产品并将它们存储在我的products数组列表中,这样我就可以使用它了

    你的其他获得者也在做类似的事情。您需要存储结果,然后使用该结果调用其他方法

    我建议你总体上阅读一下Java,因为这是一个基本问题。如果这是Java 101类型的类赋值,请重新阅读第一个代码章节,以便更好地理解方法和类如何相互调用

  2. # 2 楼答案

    你的getOrder方法可能有问题

        public static boolean getOrder(ArrayList<String> products) {
    
            Scanner scnr = new Scanner(System.in);
    
            String userStr = "";
    
    
            System.out.println("Enter a product: ");
            userStr = scnr.nextLine();
    
            //Move this after scanning user input
            boolean productName = products.contains(userStr); 
    
            if (productName) {
                System.out.println("True");
            }
            else {
                System.out.println("False");
            }
    
            return productName;
        }
    

    在main方法中,通过传递所需的参数并将其返回结果保存在变量中,可以继续调用所需的方法

        public static void main(String[] args) {
    
            bannerPrinter();
            ArrayList<String> products = productBuilder();
            Boolean productExists= getOrder(products); //getOrder needs arraylist and return boolean
    
            //then check if the returned result is true i.e if product exists
            if(productExists){
            double price = getPrice();
    
             //do other stuff, calcualte tax on price and then get total and etx
            }
            else{
            //Print "product not found"
            }
    
    
        }