我需要编写一个程序来帮助便利店店员决定是否将啤酒卖给顾客。啤酒只能卖给21岁或以上的人,并且有足够的钱(啤酒5美元)。如果客户太年轻,告诉他们必须等待多少年才能返回。如果他们的钱太少,告诉他们还需要多少钱。我不知道该怎么办,请帮帮忙。
import java.util.Scanner;
public class example1 {
public static void main (String [] args) {
Scanner scan = new Scanner (System.in);
int age;
double beerPrice;
double cash;
System.out.print( "Please type customer age:" );
age = scan.nextInt();
System.out.println();
System.out.print ("Type customer cash amount:");
cash= scan.nextDouble();
System.out.println();
if (age <= 21) {
System.out.println(21-age);
}
}
}到目前为止,这是我所拥有的全部。每次我运行程序时,如果年龄是21岁或更大,当我不想打印年龄时,它仍然打印年龄。请帮我完成这个程序。而不是请求帮助。
发布于 2015-03-03 01:18:38
您的代码看起来很好。有时编译器会感到困惑,并且在应该注册新更改时不注册新更改。尝试保存您正在处理的文件,然后进行编译,然后尝试重新启动编译器。如果失败了,通常在许多编译器中有一个“干净项目”函数可以提供帮助。
发布于 2015-03-03 01:19:33
您可以使用以下代码
public class example1{
public static void main (String [] args) {
Scanner scan = new Scanner (System.in);
int age;
double beerPrice;
double cash;
System.out.print( "Please type customer age:" );
age = scan.nextInt();
System.out.println();
System.out.print ("Type customer cash amount:");
cash= scan.nextDouble();
System.out.println();
if (age < 21) {
System.out.println(""+(21-age)+" years till you can buy beer");
}
if(cash<5.0)
{
System.out.println("$"+(5.0-cash)+" more is required");
}
}
}https://stackoverflow.com/questions/28815546
复制相似问题