使用带有用户输入的If / else语句(Java)

时间:2017-09-26 04:24:00

标签: java if-statement switch-statement java.util.scanner

我对编码很新,不知道如何完成我的代码。我不确定整个代码是否正确。我一直在努力研究,但我的导师没有给出太多指导。它还没有完成,但这就是我现在所拥有的。 任务是:

您已被聘请自动化Summerdale销售办公室的公寓价格估算系统。 它需要:

要求用户选择:

  • 1 for park view
  • 2 for golf course view
  • 3 for lake view

公园景观公寓价值15万美元,公寓享有黄金景色,价值17万美元,湖景公寓价值21万美元。如果用户输入无效代码,请将价格设置为0。

它还应该要求用户指定他们是否需要:

  • 1车库
  • 2个停车位

但仅限于视图选择有效。

将任何带车库的公寓的价格加5美元。如果停车场无效,请显示适当的按摩,并假设没有车库的公寓的价格。

foo__

带有停车位的公园景观的预期输出样本:

请选择一个视图:(1)公园(2)高尔夫球场(3)湖畔 请选择停车选项:(1)车库(2)空间
您的选择:带停车位的公园景观
预计价格:150000美元

2 个答案:

答案 0 :(得分:0)

import java.util.*;

enum ViewType {
Unknown, Park, GolfCourse, Lake
}

public class ApartmentSales {
 public static void main(String args[]) {
    Scanner in = new Scanner(System.in);
    ViewType Type = ViewType.Unknown;
    int choice = 0;
    int garage = 1;
    int space = 2;
    int rate = 0;

    System.out.println("Please select a view: (1) Park (2) Golf Course (3) 
                       Lake");

    choice = in.nextInt();
    switch (choice) {

    case 1:
        Type = ViewType.Park;
        rate = 150000;
        spaces(rate, Type);
        break;
    case 2:
        Type = ViewType.GolfCourse;
        rate = 170000;
        spaces(rate, Type);
        break;
    case 3:
        Type = ViewType.Lake;
        rate = 210000;
        spaces(rate, Type);
        break;
    default:
        Type = ViewType.Unknown;
        rate = 0;
        break;
    }

}

public static void spaces(int price, ViewType type) {
    System.out.println("Please select a parking option: (1) Garage (2) 
                          Space");
    Scanner in = new Scanner(System.in);
    int parking = in.nextInt();
    int finalPrice = price;
    if (parking == 1) {
        finalPrice = price + 5000;
        System.out.println("Your choice: " + type + " view with a parking 
              garage with total Price of $"
                + finalPrice / 1000 + "k");
    }
    if (parking == 2) {
        System.out.println("Your choice: " + type + " view with a parking 
                   space with total Price of $"
                + finalPrice / 1000 + "k");
    }
}

}

答案 1 :(得分:-2)

import java.util.Scanner;

public class ApartmentSales {     public static void main(String [] args){

    //select park view and calculate price
    String viewSelection ="";
    int viewPrice = 0;

    System.out.println("Please select a view: (1) Park (2) Golf Course (3) Lake");
    Scanner scView = new Scanner(System.in);
    int numView = scView.nextInt();

    switch(numView) {
    case 1: {
        viewSelection = "Park";
        viewPrice = 150000;
        break;
    }
    case 2:{
        viewSelection = "Golf Course";
        viewPrice = 170000;
        break;
    }
    case 3:{
        viewSelection = "Lake";
        viewPrice = 210000;
        break;
    }
    }

    //select parking space and calculate price
    String parkingSelection = "";
    int parkingPrice = 0;

    System.out.println("Please select a parking option: (1) garage (2) parking space");
    Scanner scParking = new Scanner(System.in);
    int numParking = scParking.nextInt();

    switch(numParking) {
    case 1: {
        parkingSelection ="garage";
        parkingPrice = 5000;
        break;
    }
    case 2: {
        parkingSelection = "parking space";
        parkingPrice = 0;
        break;
    }
    }

    //output the result
    int sumPrice = viewPrice + parkingPrice;
    System.out.println("Your choice: " + viewSelection +" view with a "+ parkingSelection);
    System.out.println("Estimated Price: $"+sumPrice);
}

}