大家好,我有一个功能,可以在与食物类型和食物名称匹配时向我返回商品的价格。 当我将它们作为字符串输入传递时,它起作用,但是当我将它们作为数组传递时,它回退并返回0。我已经通过打印数组中的值及其完全相同的值进行了手动调试。

返回价格函数:

public static double getPrice (String foodType,String foodName) {     for (FoodMenu fud : getFoodMenuList ())     {         if(fud.getFoodType().equals(foodType) && fud.getFoodName().equals(foodName) )         {             return fud.getPrice();         }     }     return 0; } 

主功能 :

    public void totalCost (String [] type, String [] name, String [] quantity) {     for (int i = 0; i < type.length;i++)     {         System.out.println(type[i].toString()); // value is "Rice"         System.out.println(name[i].toString()); // value is "Nasi Lemak"         System.out.println(FoodMenu.getPrice("Rice","Nasi Lemak")); // returns 2.5         System.out.println(FoodMenu.getPrice(type [i], name [i])); // returns 0     } } 

FoodMenu类的要求:

import java.io.Serializable; import java.util.ArrayList; public class FoodMenu implements Serializable {      private int ID;     private String foodName;     private double price;     private String foodType;     private static ArrayList<FoodMenu> foodMenuList = new ArrayList<FoodMenu>();      public static double getPrice (String foodType,String foodName)     {         for (FoodMenu fud : getFoodMenuList ())         {             if(fud.getFoodType().equals(foodType) && fud.getFoodName().equals(foodName) )             {                 return fud.getPrice();             }         }         return 0;     }     public FoodMenu (int ID, String foodName,double price, String foodType)     {         this.ID = ID;         this.foodName = foodName;         this.price = price;         this.foodType = foodType;     }       public int getID() {         return ID;     }     public void setID(int iD) {         ID = iD;     }     public String getFoodName() {         return foodName;     }     public void setFoodName(String foodName) {         this.foodName = foodName;     }     public double getPrice() {         return price;     }     public void setPrice(double price) {         this.price = price;     }     public String getFoodType() {         return foodType;     }     public void setFoodType(String foodType) {         this.foodType = foodType;     }      public static ArrayList<FoodMenu> getFoodMenuList() {         return foodMenuList;     }      public static void setFoodMenuList(ArrayList<FoodMenu> foodMenuList) {         FoodMenu.foodMenuList = foodMenuList;     }     } 

===============>>#1 票数:2

我在在线Java编译器中尝试过: https : //www.tutorialspoint.com/compile_java_online.php

一切似乎都正常,这是我所做的:

import java.io.Serializable; import java.util.ArrayList;   public class HelloWorld{   public static void main(String []args){       String [] type = {"Rice", "Meat", "Chicken"};      String [] name=   {"Nasi Lemak", "sweet sour beef", "sweet sour chicken"};      String [] quantity=   {"4", "6", "17"};      FoodMenu foodmen1 = new FoodMenu(1, "sweet sour chicken", 10.55, "Chicken");     FoodMenu foodmen2 = new FoodMenu(2, "sweet sour beef", 20.00, "Meat");     FoodMenu foodmen3 = new FoodMenu(3, "Nasi Lemak", 80.00, "Rice");      ArrayList<FoodMenu> foodItems = new ArrayList();     foodItems.add(foodmen1);     foodItems.add(foodmen2);     foodItems.add(foodmen3);      FoodMenu.setFoodMenuList(foodItems);        totalCost (type, name, quantity);  }   public static void totalCost (String [] type, String [] name, String [] quantity)  {     for (int i = 0; i < type.length;i++)     {         System.out.println("Type: " + type[i].toString());          System.out.println("Name: " + name[i].toString());          System.out.println("Price: " + FoodMenu.getPrice(type [i], name[ i]));      }  } }  class FoodMenu implements Serializable {      private int ID;     private String foodName;     private double price;     private String foodType;     private static ArrayList<FoodMenu> foodMenuList = new ArrayList<FoodMenu>();      public static double getPrice (String foodType,String foodName)     {         for (FoodMenu fud : getFoodMenuList ())         {             if(fud.getFoodType().equals(foodType) && fud.getFoodName().equals(foodName) )             {                 return fud.getPrice();             }         }         return 0;     }      public FoodMenu (int ID, String foodName,double price, String foodType)     {         this.ID = ID;         this.foodName = foodName;         this.price = price;         this.foodType = foodType;     }       public int getID() {         return ID;     }     public void setID(int iD) {         ID = iD;     }     public String getFoodName() {         return foodName;     }     public void setFoodName(String foodName) {         this.foodName = foodName;     }     public double getPrice() {         return price;     }     public void setPrice(double price) {         this.price = price;     }     public String getFoodType() {         return foodType;     }     public void setFoodType(String foodType) {         this.foodType = foodType;     }      public static ArrayList<FoodMenu> getFoodMenuList() {         return foodMenuList;     }      public static void setFoodMenuList(ArrayList<FoodMenu> foodMenuList) {         FoodMenu.foodMenuList = foodMenuList;     }  } 

这是输出:-

类型:大米

名称:椰浆饭

价格:80.0

类型:肉

名称:甜酸牛肉

价格:20.0

类型:鸡肉

名称:糖醋鸡

价格:10.55

  ask by Blacky_99 translate from so

===============>>#2 票数:0

我犯的错误是这样的菜鸟错误。 我以错误的顺序传递了输入。 谢谢各位先生的帮助。

我的功能的正确顺序是:

totalCost (name,type,quantity); 

我犯的错误是:

totalCost (type,name,quantity); 

  ask by Blacky_99 translate from so

本文未有回复,本站智能推荐: