不知何故,我尝试让用户选择哪个厨师必须准备食物。我已经创建了变量 whichcook
并将其放置在 cook1
原来的位置。但我不知道如何继续下去。我想让用户在“Jan de Vries”或“SinBad”之间进行选择以进行准备。因此,方法 deliverer.delivered(whichcook, customer);
继续使用所选名称。我想是否必须使用 instanceof
我猜,但不知道如何真正做到这一点。我知道如何进行用户输入和案例等,这不是问题。更多的是关于如何隔离正确的实例!
有人给个关键建议吗???
java
package KebabStore;
public class DamascusKebab
{
public static int cooksnumber;
public static int deliverersnumber;
@SuppressWarnings("unused")
public static void main(String[] args)
{
Cook cook1 = new Cook ("Jan de Vries", "Butcherknife 1", "1212-IS", "Allahmelo", 123456);
Cook cook2 = new Cook ("Sinbad", "Camelhumb 2","2323-IS", "Halal-lem", 654321);
Deliverer deliverer1 = new Deliverer ("Ali Baba", "Helmgras 11", "3434-JH", "Ji-Hattem",456789);
Deliverer deliverer2 = new Deliverer ("Muammar", "Zadeldreef 22", "4545-JH", "Moskemenade", 987654);
Customer customer = new Customer ("Piet Hein", "Klantlaan 25", "5656-KL", "Darmstadt");
cooksnumber = Cook.numberofcooks;
deliverersnumber = Deliverer.numberofdeliverers;
Cook whichcook = cook1;
deliverer.delivered(whichcook, customer);
}
}
最佳答案
如何获取用户输入?
如果来自扫描仪
Cook whichcook;
Scanner scanner = new Scanner(System.in);
int choice = scanner.nextInt()
if (choice == 1)
whichcook = cook1;
else
whichcook = cook2;
deliverer.delivered(whichcook, customer);
关于java - 选择要继续的实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59106104/