Java初学者租赁计划

时间:2018-07-06 11:35:02

标签: java

我对编程很陌生。我正在尝试为自行车出租服务制定程序。问题是在特定时间有两个关税(1和2)。

关税1:00:00至07:00和17:00至24:00。 (1 $) 运价2:07:00至17:00 (2 $) 我需要使用扫描仪,所以我要写下两个数字,它必须计算出关税1和关税2的小时数以及总费用。问题在于,我不知道如何编写程序,因此它可以检测一个或另一个关税中有多少小时。

程序必须以下列方式工作:

  1. 输入1
  2. 输入2
  3. 计算塔里夫1和2的小时数,得出总计。
  4. 给出总价。

所以这是代码:

import java.util.Scanner;

public class Bike {

public static void main(String[] args) {

    Scanner clavier = new Scanner(System.in);
    System.out.print("Give the starting hour : ");
    int start = clavier.nextInt();
    System.out.print("Give the ending hour : ");
    int finish = clavier.nextInt();

    if (start < 0 || start > 24) { 
    System.out.println("Hours must be within 0 and 24 !");
    }
    if (start == finish) {
    System.out.println("Strange, you didn't take it long enough !");
    }
    if (start >= 24 || start > finish) {
    System.out.println("Strange, the starting hour is after the end ...");
    }
    int total = finish - start;
    if (total > 0) {
    System.out.println("You have rented the bike for " + total + " hours.");
    }
//This is where it becomes complicated.

    if ((start >= 0 && start < 7)||(start >= 17 && start <= 23)) {
        System.out.println("Amount of hours in Tariff 1 is : " + ((7 - start) +(finish - 17)));
    } else {
        System.out.println("Amount of hours in Tariff 2 is : "
        System.out.print("Total amount to pay is : ");
        System.out.println(" dollars.");
        {

        }

1 个答案:

答案 0 :(得分:0)

好的,您是编程的新手,所以我将尝试保持简单。这里的问题不是语法或Java中的秘密,它只是缺少逻辑。从您写的内容中可以看出,您是刚上编程的高中/中学,还是刚开始学习编码的人。

因此,我将尝试使其保持简单,而不会给您太多提示。

您不能仅在其他地方拥有关税2。我建议创建2个int,riffOne和arrivalTwo(或您喜欢的任何名称),使它们=0。使用一些if语句计算它们。然后,当您拥有值时,请在System.out中使用它们。

这很好,因为您可以添加任何想要添加到int的内容,而您将始终拥有它们的值。

之所以会否决这个问题,是因为您拥有所有可以回答该问题的工具,而且您还没有掌握所需的逻辑,这样做通常不太好,因为它似乎很懒惰,即使您真的很努力。

相关问题