Java调用方法
我很难理解我到底应该用什么来通过主课的第三种方法。我现在真的有点迷路了。任何帮助都会很棒。以下是我编写的代码: 此外,以下是名为“getOrder”的布尔方法的说明: 编写一个名为getOrder的方法,该方法将字符串的ArrayList作为参数(products ArrayList)并返回布尔值。 在方法体中,提示用户输入产品名称(字符串),然后检查产品名称是否存在于字符串的ArrayList中。 如果存在,则返回true,否则返回falseJava调用方法,java,arraylist,methods,boolean,Java,Arraylist,Methods,Boolean,我很难理解我到底应该用什么来通过主课的第三种方法。我现在真的有点迷路了。任何帮助都会很棒。以下是我编写的代码: 此外,以下是名为“getOrder”的布尔方法的说明: 编写一个名为getOrder的方法,该方法将字符串的ArrayList作为参数(products ArrayList)并返回布尔值。 在方法体中,提示用户输入产品名称(字符串),然后检查产品名称是否存在于字符串的ArrayList中。 如果存在,则返回true,否则返回false public static void main(S
public static void main(String[] args) {
// Call your methods here
bannerPrinter();
productBuilder();
getOrder(??); -----------------------------Confused as to what to pass this method with
}
// Write your methods below here
public static boolean getOrder(ArrayList<String> products) {
@SuppressWarnings("resource")
Scanner in = new Scanner(System.in);
System.out.println("Please enter a product name: ");
String productName = in.nextLine();
if (products.contains(productName)) {
return true;
}
else {
return false;
}
}
public static ArrayList<String> productBuilder() {
ArrayList<String> products = new ArrayList<String>();
products.add("Desktop");
products.add("Phone");
products.add("TV");
products.add("Speaker");
products.add("Laptop");
return products;
}
public static void bannerPrinter() {
System.out.println();
System.out.println("******************************************");
System.out.println("****** Welcome to my eCommerce app! ******");
System.out.println("******************************************");
System.out.println();
}
publicstaticvoidmain(字符串[]args){
//在这里调用您的方法
bannerPrinter();
productBuilder();
getOrder(??)-------------------------------对传递此方法的内容感到困惑
}
//在下面写下你的方法
公共静态布尔getOrder(ArrayList产品){
@抑制警告(“资源”)
扫描仪输入=新扫描仪(系统输入);
System.out.println(“请输入产品名称:”);
字符串productName=in.nextLine();
if(products.contains(productName)){
返回true;
}
否则{
返回false;
}
}
公共静态ArrayList productBuilder(){
ArrayList产品=新的ArrayList();
产品。添加(“桌面”);
产品。添加(“电话”);
产品。添加(“电视”);
产品。添加(“扬声器”);
产品。添加(“笔记本电脑”);
退货产品;
}
公共静态无效横幅(){
System.out.println();
System.out.println(“*************************************************************”);
System.out.println(“******欢迎使用我的电子商务应用程序!”;
System.out.println(“*************************************************************”);
System.out.println();
}
}您需要传递一个
ArrayList
。查看您的代码,您可以使用productBuilder()
构建一个,否则它将不被使用。因此:
ArrayList<String> products = productBuilder();
getOrder(products);
顺便说一句,以下代码:
if (products.contains(productName)) {
return true;
}
else {
return false;
}
更容易写成
return products.contains(productName);
您需要传递一个
ArrayList
。查看您的代码,您可以使用productBuilder()
构建一个,否则它将不被使用。因此:
ArrayList<String> products = productBuilder();
getOrder(products);
顺便说一句,以下代码:
if (products.contains(productName)) {
return true;
}
else {
return false;
}
更容易写成
return products.contains(productName);
您只需传递一个
ArrayList
。像这样:
ArrayList<String> lst = new ArrayList<String>();
lst.add("Phone");
lst.add("Laptop");
getOrder(lst);
ArrayList lst=new ArrayList();
第1条添加(“电话”);
第1条添加(“笔记本电脑”);
getOrder(lst);
当然,您需要先填写
ArrayList
,然后通过它。您只需通过ArrayList
。像这样:
ArrayList<String> lst = new ArrayList<String>();
lst.add("Phone");
lst.add("Laptop");
getOrder(lst);
ArrayList lst=new ArrayList();
第1条添加(“电话”);
第1条添加(“笔记本电脑”);
getOrder(lst);
当然,您需要先填写ArrayList
,然后通过它