2016-04-05 160 views
0

我无法调用主方法上的方法。这是我必须的主要方法:如何正确调用主方法中的其他方法

打印横幅消息

获取产品的ArrayList

获得来自用户的产品订单,并检查它存在于产品

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

} 

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

+0

哪种麻烦?请发布你观察到的错误。 – Harald

+0

我不一定有错误,我只是困惑于如何调用主要方法中的不同方法。所以更多的是,我不太清楚如何从这一点上做到这一点。 – cazyaboutjava2020

回答

0

你走在正确的道路上。

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

在main方法的第一行,bannerPrinter()调用你的bannerPrinter方法。

现在为您的第二个电话productBuilder()你基本上得到的结果。现在使用该结果,您可以查看该产品是否存在,获取产品价格等。

因此,如果要从main调用方法,您只需使用方法名称进行调用即可。

对于像getOrder(ArrayList<String> products)这样的参数的方法,您必须将参数传递给该方法。

在你的例子中它将是getOrder(products)。那会用正确的参数调用该方法并获得boolean结果。

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

productBuilder()返回List<String>所以你这样做

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

你的其他获得者正在做类似的事情。你需要存储结果,然后用它来调用你的其他方法。

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

+0

好的,我可以写'getOrder()'和'getPrice()'方法。不过,我现在对最后三个方法调用感到困惑。我写了'getTax(tax)','getTotal(saleTotal)'和'printTotal(saleTotal)'。然而,在这些调用中,它表示找不到符号? – cazyaboutjava2020

+0

你可以更新你的原始文章吗? – wiredniko

+0

我更新了我所做的更改 – cazyaboutjava2020

0

有可能是一个可能的问题,在您的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" 
     } 


    } 
+0

哦,抓住布尔错误!现在我只是困惑于如何调用其他方法 – cazyaboutjava2020

+0

您的回答也有帮助;但是,我不能接受两个答案。但我非常感谢你帮助我解决问题! – cazyaboutjava2020

+0

没问题,不客气 – TheUknown

相关问题