将java代码从用户输入更改为命令行参数
因此,我正在处理一个项目,该项目将采用命令行参数ex:11:45:12 11:48:13,并以秒为单位的已用时间形式输出:181 尽管我在修改代码时遇到了困难,从要求用户按任意键启动计时器,到从命令行参数获取直接时间。非常感谢您的帮助!下面是我的两个班级:时钟和测试时钟。testClock是将使用命令行参数运行的将java代码从用户输入更改为命令行参数,java,command,arguments,Java,Command,Arguments,因此,我正在处理一个项目,该项目将采用命令行参数ex:11:45:12 11:48:13,并以秒为单位的已用时间形式输出:181 尽管我在修改代码时遇到了困难,从要求用户按任意键启动计时器,到从命令行参数获取直接时间。非常感谢您的帮助!下面是我的两个班级:时钟和测试时钟。testClock是将使用命令行参数运行的 import java.sql.Time; public class Clock { public Time startTime; public Time en
import java.sql.Time;
public class Clock {
public Time startTime;
public Time endTime;
Clock() {
} public void start(String startTime){
startTime.startTime(convertToSeconds(startTime));
} public void stop(String endTime){
endTime.setTime(convertToSeconds(endTime));
}
}
仍然接收错误无法转换为秒,无法找到。与获取经过的时间相同。删除System.in并使用args[0]和args[1]。主方法签名中的参数是一个命令行参数数组。您需要一种设置stopTime和startTime的方法。我建议创建另一个接受两个字符串的构造函数。或者,您可以添加新的停止和启动功能,以便进行设置
import java.sql.Time;
public class Clock {
public Time startTime;
public Time endTime;
Clock() {
} public void start(String startTime){
startTime.startTime(convertToSeconds(startTime));
} public void stop(String endTime){
endTime.setTime(convertToSeconds(endTime));
}
一旦有了这些参数,正如EvervOid提到的,使用args参数中的相应位置获取命令行参数,并将它们传递给新的停止和启动函数。您不需要删除已经拥有的函数,因为Java非常智能,可以区分具有相同名称和不同参数的两个函数之间的差异
最后一个主函数可能类似于:
public class TestClock {
public static void main(String args[]){
Clock cl = new Clock(args[0], args[1]);
System.out.println("Time elapsed is " + cl.getElapsedTime() + " seconds");
}
}
编辑:每个评论问题
您可以有一个接受开始和结束时间的构造函数,如下所示:
Clock(String startString, String endString) {
startTime = new Time(convertToMilliseconds(startString));
endTime = new Time(convertToMilliseconds(endString));
}
您需要按照我提到的其他帖子编写转换函数
另一种方法是将以下内容写入函数:
public void start(String startTime){
startTime.setTime(convertToMilliseconds(startTime));
System.out.println("Start time is " + startTime);
}
public void stop(String endTime){
endTime.setTime(convertToMilliseconds(endTime));
System.out.println("End time is " + endTime);
}
如果执行此操作,则主要功能变为:
public class TestClock {
public static void main(String args[]){
Clock cl = new Clock();
cl.start(args[0]);
cl.end(args[1]);
System.out.println("Time elapsed is " + cl.getElapsedTime() + " seconds");
}
}
如果你需要帮助将字符串转换成毫秒,你可以考虑这一点。确切的说,新的停止和启动函数是用来设置它们的,它会是什么样子?对不起,我现在在放屁。我理解扫描器的免除,按任意键开始/停止时间现在我得到错误,找不到符号转换毫秒字符串公共类时钟{public Time startTime;public Time endTime;Clock{}public void startString startTime{startTime.starttimeconverttosecondstarttime;}public void stopString endTime{endTime.setTimeconvertToSecondsendTime;}}这是一个可以编写的函数示例。您仍然需要以某种方式将字符串转换为毫秒。如何从字符串转换为时间在其他问题中有很好的介绍,比如我在第一篇评论中链接到的问题。
import java.sql.Time;
public class Clock {
//Create two instance of Time class with default values
//for hour,minute and seconds
private Time startTime=new Time(0, 0, 0);
private Time stopTime=new Time(0, 0, 0);
//set start time
public void start(Time startTime){
this.startTime=startTime;
}
//set end time
public void stop(Time stopTime){
this.stopTime=stopTime;
}
/**The method getElapsedTime calculates the time difference
* between two times and returns seconds as return value.
* */
public int getElapsedTime(){
int totalSeconds=0;
int hours=stopTime.getHours()-startTime.getHours();
if(hours>0) {
int minues=stopTime.getMinutes()-startTime.getMinutes();
if(minues>0) {
minues=stopTime.getMinutes()-startTime.getMinutes();
}
else
minues=startTime.getMinutes()-stopTime.getHours();
int seconds=stopTime.getSeconds()-startTime.getSeconds();
if(minues>0) {
seconds=stopTime.getSeconds()-startTime.getSeconds();
}
else
seconds=startTime.getSeconds()-stopTime.getSeconds();
totalSeconds=hours*60*60+minues*60+seconds;
}
else {
int minutes=stopTime.getMinutes()-startTime.getMinutes();
if(minutes>0) {
minutes=stopTime.getMinutes()-startTime.getMinutes();
}
else
minutes=startTime.getMinutes()-stopTime.getMinutes();
int seconds=stopTime.getSeconds()-startTime.getSeconds();
if(seconds>0) {
seconds=stopTime.getSeconds()-startTime.getSeconds();
}
else
seconds=startTime.getSeconds()-stopTime.getSeconds();
totalSeconds=hours*60*60+minutes*60+seconds;
}
//return seconds
return totalSeconds;
}//end of the getElapsedTime method
}//end class
import java.sql.Time;
public class TestClock {
public static void main(String[] args) {
//Create an instance of Clock class object
Clock clock=new Clock();
//Get first argument
String startTime=args[0];
//split the argument and get hour,minute and second
String [] time=startTime.split(":");
//split the argument and get hour,minute and second
int hour=Integer.parseInt(time[0]);
int minutes=Integer.parseInt(time[1]);
int seconds=Integer.parseInt(time[2]);
//call start method with an instance Time class
clock.start(new Time(hour, minutes, seconds));
//Get first argument
String endTime=args[1];
String [] stopTime=endTime.split(":");
//split the argument and get hour,minute and second
hour=Integer.parseInt(stopTime[0]);
minutes=Integer.parseInt(stopTime[1]);
seconds=Integer.parseInt(stopTime[2]);
//call start method with an instance Time class
clock.stop(new Time(hour, minutes, seconds));
System.out.println("Elapsed time in seconds :"+clock.getElapsedTime());
}//end main
}//end class