首页 > 解决方案 > 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.");
        {

        }

标签: java

解决方案


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

所以我会尽量保持简单,不要给你太多提示。

您不能仅在 else 中有关税 2。我建议创建 2 个整数,关税一号和关税二(或任何您喜欢的名称),使它们 = 0。使用一些 if 语句计算它们。然后,当您有值时,在 System.out 中使用它们。

这很好,因为您可以简单地将所需的任何内容添加到 int 中,并且您将始终拥有它们的值。

这个问题被否决的原因是因为你有所有工具来回答这个问题,而你只是没有掌握所需的逻辑,这样做通常不是好习惯,因为它看起来很懒惰,即使你真的很努力。


推荐阅读