2013-03-27 65 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")) 

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