【问题标题】:Asking for A User Input for Time using JOptionPane使用 JOptionPane 要求用户输入时间
【发布时间】:2016-09-18 19:57:19
【问题描述】:

我的老师要求我们通过要求用户使用 JOptionPane 输入时间(HH、mm、s.s)来向用户询问赛车手的开始时间和停止时间。我不知道该怎么做。我试过做 integer.parseint(joptionpane....) 但是当我输入时间时我不断收到错误消息。另外,我知道问题在于,因为我要求用户为我的 Time 类输入时间,所以它应该不同。我需要一些帮助,从这一点开始。这是我的代码(我还包括了我的时间类:

package Racers.java;

import javax.swing.JOptionPane;

public class Racers
{   

public static void main(String[] args)
{

    //racer1
    String racer1;
    Time startTime1;
    Time stopTime1;
    double elapsedTime1;

    //assigning racer1
    racer1 = JOptionPane.showInputDialog("Please enter the name of the first racer: ");
    startTime1 = new Time();
    stopTime1 = new Time();
    elapsedTime1 = stopTime1.minus(startTime1).getTime();

    JOptionPane.showMessageDialog(null, 
            "Here is the racer's name, start time, stop time, and elapsed time:\n"
            + racer1 + (" - ") + ("Start time: ") + startTime1 + ("; ") + ("Stop time: ") + stopTime1 + ("; ") + ("Elapsed time: ") + elapsedTime1 + "\n"

}//End main
}//End Racers


class Time 
{
//Variable to hold seconds
double seconds;

//Constructors for class Time
public Time()
{
    seconds = 0.0;
}

public Time(double newSeconds)
{
    seconds = newSeconds;
}

public Time(int hours, int minutes, double newSeconds)
{
    seconds = (double)(hours * 3600 + minutes * 60) + newSeconds;
}

//Observers for class Time
public double getTime()
{
    //Return elapsed time
    return seconds;
}

public int getHours()
{
    //Compute whole hours from seconds
    return (int)seconds / 3600;
}

public int getMinutes()
{
    //Seconds after hours taken out
    int remainingSeconds = (int)seconds % 3600;
    //Compute minutes from remainder
    return remainingSeconds / 60;
}

public double getSeconds()
{
    //Seconds after minutes taken out
    return seconds % 60.0;
}

//Returns HH:MM:SS.FFF
public String toString()
{
    int hours = (int)seconds / 3600;
    int minutes = (int)seconds % 3600 / 60;
    return hours + ":" + minutes + ":" + seconds % 60.0;
}

//Operations for class Time
public Time plus(Time otherTime)
{
    return new Time(seconds + otherTime.seconds);
}

public Time minus(Time otherTime)
{
    return new Time(seconds - otherTime.seconds);
}

}//End Time

【问题讨论】:

    标签: java input time joptionpane


    【解决方案1】:

    问题来了

    startTime1 = new Time();
    stopTime1 = new Time();
    

    您正在使用默认构造函数初始化开始和停止时间,其中默认构造函数将开始和停止时间返回为零,因此您的输出也为零。

    所以你需要使用参数化构造函数来区分时间。

    startTime1 = new Time(10,10,10d); // 10hr 10 min 10 sec
    stopTime1 = new Time(10,20,10d);  //10hr 20 min 10 sec
    

    然后你会发现 10 分钟的差异是 600 秒。

    也使用下面的行

    JOptionPane.showMessageDialog(null, "Here is the racer's name, start time, stop time, and elapsed time:\n" + racer1 + (" - ") + ("Start time: ") + startTime1 + ("; ") + ("Stop time: ") + stopTime1 + ("; ") + ("Elapsed time: ") + elapsedTime1 + "\n");
    

    希望你明白这一点。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-06-10
      • 2018-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-03
      • 1970-01-01
      相关资源
      最近更新 更多