形参与实参值传递举例
public class Customer1{
Order1 order = new Order1();
Menus1 mu = new Menus1();
Scanner scan = new Scanner(System.in);
public void orderFood(){
mu.showFoodMenu();
System.out.print("请选择菜品:");
String foodOpt = scan.next();
String[] food = mu.findFoodById(foodOpt);
调用mu.findFoodById();方法 并且把foodOpt这个实参传递给菜单类中的foodid这个形参 最后用food接收返回值
if (food != null){
order.addOrder(food);
调用order.addOrder();方法 将food(从上一个方法接收到的一维数组)这个实参传递给订单类中的food形参 接收到数据后执行语句
} else {
System.out.println("请输入正确的菜品编号");
}
}
public String[] findFoodById(String foodid){
for(int i=0; i<menus.length; i++){
if (foodid.equals(menus[i][0])){
return menus[i];
}
}
return null;
}
public void addOrder(String[] food){
int pos = 0;
for (int i=0; i<orders.length && orders[i][0]!= null; i++) {
pos++;
}
orders[pos][0]=getCurrentOrderId();
orders[pos][1]=food[1];