2015-03-02 34 views
0

我需要編寫一個程序,幫助便利店職員決定是否向顧客出售啤酒。啤酒只能賣給21歲或以上的人,並且有足夠的錢(啤酒成本爲5.00美元)。如果客戶太年輕,告訴他們在返回之前必須等待多少年。如果他們的錢太少,告訴他們需要多少錢。我不知道該怎麼做,請幫忙。編寫一個Java程序來幫助便利店職員決定是否向顧客出售啤酒?

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歲或以上,那麼當我不想要時它仍會打印年齡。請幫我完成這個程序。不要求出手。

+1

對我的作品 - 不打印出來歲的時候我進入22.你確定你正在運行顯示的版本? – DNA 2015-03-02 17:15:24

+0

謝謝大家的幫助! – Kyle 2015-03-02 17:32:37

回答

0

你的代碼看起來不錯。有時編譯器會感到困惑,並且在它應該時不會註冊新的更改。嘗試保存正在處理的文件,然後編譯,然後嘗試重新啓動編譯器。如果失敗了,在許多編譯器中通常會有一個「乾淨的項目」功能,可以提供幫助。

0

您可以使用下面的代碼

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"); 
} 
} 
}