【发布时间】:2015-11-28 09:30:37
【问题描述】:
对于汽车,具有以下特征: - 年龄(年龄) - BGN 价格(价格) 一辆汽车是高端的,如果超过 5 年且价格超过 20 千或 5 年或更少,成本超过 40,000 编写一个表达式,确定车辆是否为给定特征 高端。 我试过了:
import java.util.Scanner;
public class Vehicle {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.println("Age: ");
int age = input.nextInt();
Scanner input1 = new Scanner(System.in);
System.out.println("Price: ");
int price = input1.nextInt();
boolean isHightClass = age >= 5 && price > 20000 || age <= 5 && price > 4000;
System.out.println(isHightClass);
还好吗?
【问题讨论】:
-
如果超过 5 年且成本超过 2 万或 5 年或更少年且成本超过 4 万,我如何使用布尔值比较年龄和价格?
-
您需要一些括号围绕您想要与的条件。否则可能无法正确获取。
-
@Arc676 不,她没有。
-
@DavidWallace 为什么不呢?还是应该说“推荐”?
-
根本不需要。所有比较运算符的运算顺序都高于
&&,&&的运算顺序高于||。所以 Venetsia 的代码是正确的 - 她不需要添加任何括号。
标签: java boolean compare operators