我怎样才能编写一个更好的代码(比较)

时间:2015-11-28 09:30:38

标签: java boolean compare operators

对于汽车,具有以下特点: - 年龄(年龄) - BGN的价格(价格) 汽车是高端的,如果超过5年,成本超过20 一千或五年或更少的年,成本超过4万 写一个表达式,确定车辆是否是给定的特征 高端。 我尝试过:

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

可以吗?

2 个答案:

答案 0 :(得分:0)

您正在检查价格是否大于4,000,而不是40,000,如您的问题所示。

答案 1 :(得分:-1)

尝试以这种方式写作:

boolean isHightClass = (age >= 5 && price > 20000) || (age <= 5 && price > 4000);