首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >计算时间差和距离差的方法

计算时间差和距离差的方法
EN

Stack Overflow用户
提问于 2017-10-01 18:52:27
回答 2查看 85关注 0票数 0

我正在制作公共交通卡。我希望你能帮助定义三种相关的方法:

代码语言:javascript
复制
isCheckedIn(long time) 

返回它是否成功地在最后120分钟内签入,并且自上一次成功的checkIn之后还没有签出。

代码语言:javascript
复制
checkIn(int x, int y, long time)  

哪一个

  • X和y是参数,这是当前位置。
  • 如果签入一个新的旅程,信息:“签入”。
  • 如果继续当前的一个,如果已经签入,消息:“继续旅程”。
  • 可以永远旅行,只要你至少每120分钟办理一次入住手续。

checkOut(int x,int y,长时间)

其中:

  • 结束当前的旅程,并使用公式12 + (maxX - minX + maxY - minY) * 3从余额中扣除整个旅程的价格,其中最大值和最小值将接管checkIns和最终checkOut的所有坐标。

示例消息:

  • 退房!价格是2美元,余额是98美元
  • “错误:无法签出;当前未签入”

我到目前为止所做的事:

代码语言:javascript
复制
import java.time.Instant;
import java.time.temporal.ChronoUnit;

public class TravelCard {

//Card balance
private int balance;

// isCheckedIn
private boolean isCheckedIn;
private Instant instant;

// distant x,y
private int x;
private int y;



public TravelCard()

{
    balance = 100;
    isCheckedIn = false;


}

public void depositMoney(int amount) {


    if (amount >= 0) {
        balance += amount;
        System.out.println(amount + " dollars deposited. New balance: " + balance + " dollars");
    } else {
        System.out.println("Error: Cannot deposit negative amount");
    }

}

public boolean isCheckedIn(long time) {

    Instant instant1 = Instant.now();
    time = ChronoUnit.MINUTES.between(instant, instant1);

    // time
    if (isCheckedIn && time <= 120) return true;
    else return false;

}

public void checkIn(int x, int y, long time) {

    this.x = x;
    this.y = y;

    if (time == 0) {
        System.out.println("Checked in");
    } else if (time <= 120) {
        System.out.println("Continued journey");
    }

}


public void checkOut(int x, int y, long time) {

}



public static void main(String[] args) {


     }

}

我完全迷上了checkOut方法,除了打印消息的方法。有人能帮我解决这个问题吗?事先非常感谢!

EN

回答 2

Stack Overflow用户

发布于 2017-10-01 20:30:19

下面的代码只是为了向您展示一种如何存储x和y的最大值和最小值的方法,另一种方法是在数组中存储所有的x和y值。但是内存消耗会不必要地增加。

代码语言:javascript
复制
public TravelCard(){
    x_min = 9999999;
    y_min = 9999999;
    x_max = 0;
    y_max = 0;
    balance = 100;
    isCheckedIn = false;
}

public void checkIn(int x, int y, long time) {
    //we save the new x and y, only if they are larger than previous ones
    if (x >this.x_max){
        this.x_max = x;
    } else if(x < this.x_min){
        this.x_min = x;
    }
    if (y >this.y_max){
        this.y_max = y;
    } else (y<y_min){
        this.y_min = y;
    }


    if (ischeckedIn()) {
        System.out.println("Continued Journey");
    } else if {
        System.out.println("CheckingIn Now");
        this.instant = Instant.now()
        isCheckedIn = true;
    }

 }

public boolean isCheckedIn(l) {

    Instant instant1 = Instant.now();
    if(this.isCheckedIn){
        time = ChronoUnit.MINUTES.between(instant, instant1);
    } else {
         return false;
    }

    // time
    if (isCheckedIn && time <= 120) return true;
    else{
        System.out.println("Your time is out! i.e Auto Checkout")
        this.isCheckedIn = false;
        return false;
}
public void checkOut(int x,int y,long time){
    if(this.isCheckedIn){
        //updating x and y at checkout time
        if (x >this.x_max){
            this.x_max = x;
        } else if(x < this.x_min){
            this.x_min = x;
        }
        if (y >this.y_max){
            this.y_max = y;
        } else (y<y_min){
            this.y_min = y;
        }
        //Now we alredy have max and min for x and y
        int expense = 12 +((this.x_max - this.x_min)+(this.y_max - this.y_min))*3;
        this.balance = this.balance - expense;
        System.out.println("Expense for trip: "+expense+", Available Balance:" +this.balance);
    }else{
        System.out.println("Error: Cannot check out; Not currently checked in");
}

即使在编写了这段代码之后,仍然存在一个问题,如果person在120分钟后被自动签出,您将如何在120分钟内检索x和y值。在现实生活中,我们经常听到x和y值的变化,并不断更新。

票数 1
EN

Stack Overflow用户

发布于 2017-10-01 20:49:01

这听起来很像大学家庭作业的问题。我认为,了解问题的全部背景,而不仅仅是你对问题的解释,可能会更有帮助。例如,为什么isCheckedIn()函数要求您传递一个时间值?这是一个要求,还是仅仅是你对答案的解释。实际上,你似乎并没有在任何计算中使用这个变量,所以不管我把它放在0还是1000,结果都不会改变。我还建议在来这里之前请你的助教/老师帮忙,因为这是一个特殊的问题,你可能会得到更多的即时帮助。

但是,试着回答你的一些问题。

代码语言:javascript
复制
ischeckedIn(lomg time)

我注意到的第一件事是,您使用即时变量而没有在构造函数中初始化它。看起来,在checkIn时间之前,该变量不应该有一个值,所以您应该对该逻辑进行空检查。第二件事是,由于您有一个值来查看票证是否已经签入,这应该是您的第一步,因此您不会重复工作:

代码语言:javascript
复制
public boolean isCheckedIn() {
    if(!isCheckedIn) return false;
    ....
    //some logic
}

函数的第二件事是,由于120是签入时间的常量,所以应该将其移出函数,并将其存储为类的静态最后int。

我还建议,如果您的isCheckedIn()方法要返回false,那么应该将isCheckedIn值设置为false,直到它们再次签入为止。

代码语言:javascript
复制
checkIn(int x, int y, long time)

这里有几点。因为您需要跟踪x和y的最小值和最大值,所以需要将签入坐标存储在某种数组中,或者需要为坐标设置3个变量。minX,maxX,currentX,y也一样。

在执行了存储坐标的逻辑之后,您应该检查此人是否已经签入。

代码语言:javascript
复制
if(isCheckedIn(time)) {
    // your logic
}

如果时间>120分钟,当前的方法将失败。如果时间超过120时,最好在这里使用一些方法来失败checkIn进程,其中的消息是,在再次签入之前,他们必须支付余额。如果您已签入,则应使用您的时间值来创建一个新的瞬间来替换旧的时间。我会考虑将您的返回值设置为字符串,这样您就可以让您的方法的使用者决定如何处理结果。

代码语言:javascript
复制
checkOut(int x, int y, long time)

这个方法应该非常类似于checkIn方法。您的第一步应该是检查您是否实际签入。

代码语言:javascript
复制
if(!isCheckedIn(time)) {
    // return error message
}

如果您是签入的,那么您将执行为checkIn创建的存储坐标的逻辑。此时,您可以运行计算成本、减少余额和返回消息的公式。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46515742

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档