2013-03-27 78 views
0

對於學習java來說是新的,我已經爲自己設置了創建購物籃的任務。Java:我是否可以在java中使用帶有輸入的布爾值

這裏是我的代碼:

System.out.println("Grapes " + "£" + grapes + " Quantity:"); 
    input= amount.nextLine(); 
    System.out.println("You Selected " + input + " Grapes"); 

我如何添加一個布爾值,所以當有人說他們想訂1個一串葡萄它以「葡萄一串」來了,當有人訂單2+它提出了一串串葡萄「葡萄串」。

謝謝你的幫助,

彼得

回答

1

你會這樣做:

boolean multiple_grapes = (Integer.valueOf(input) > 1); 
if (multiple_grapes) { 
    System.out.println("You Selected " + input + " Bunches of Grapes"); 
} else { 
    System.out.println("You Selected " + input + " Bunch of Grapes"); 
} 

你需要爲了能夠將其與整數值1比較分析inputInteger

1
int num = Integer.parseInt(input); 
if(num == 1){ 
    // do whatever you want 
} 
else { 
    // another action 
} 

事實上,你甚至可以比較字符串表示不解析

if(input.equals("1")) 

,但我想你需要整數表示反正

相關問題