我必须创建一个简单的自动售货机程序,如果用户输入 2 个令牌,他们会得到 1 个。我的代码有点问题 - 如果我输入 1 个令牌并输入“Y”,我会收到“谢谢享受您的饮料”的消息,而它确实应该出现错误。
import java.util.Scanner;
/**
* Vending Machine
*
* @author -
* @version 1.0
*/
public class VendingMachine
{
int cans = 10;
int token = 20;
public void fillUp (int cans)
{
if(cans <= 0)
{
cans = cans+=10;
}
}
public void tokenIn (int token)
{
Scanner scan = new Scanner (System.in);
System.out.println("Do you want something to drink? (Y/N)");
boolean tokenIN = false;
if(scan.next().equals("Y"))
{
tokenIN = true;
}
else
{
tokenIN = false;
}
if(tokenIN = true && token >= 2 && cans >=1)
{
cans--;
token-=2;
System.out.println("Thanks, enjoy your drink!");
}
else
{
System.out.println("Goodbye");
}
}
public void getTokenCount (int token)
{
System.out.println(token);
}
public void getCansCount (int cans)
{
System.out.println(cans);
}
}