有人能帮我解决我的两个Java问题吗。1是try catch 2是放置一段代码的位置
我有一段代码,我正在为uni的一项任务工作,事实上我对Java不是很在行,但我已经尝试过了。我试图让我的试捕器工作,但它似乎从来没有做它所支持的。我想我的异常是错误的,但我不确定该使用哪个异常来解决这个问题,因为我正试图阻止输入字母而只是数字 我遇到的最后一个问题是,我不知道将这段代码放在哪里:有人能帮我解决我的两个Java问题吗。1是try catch 2是放置一段代码的位置,java,try-catch,Java,Try Catch,我有一段代码,我正在为uni的一项任务工作,事实上我对Java不是很在行,但我已经尝试过了。我试图让我的试捕器工作,但它似乎从来没有做它所支持的。我想我的异常是错误的,但我不确定该使用哪个异常来解决这个问题,因为我正试图阻止输入字母而只是数字 我遇到的最后一个问题是,我不知道将这段代码放在哪里: { while (true) { cancel(); } } 我不知道该去哪一部分 任何帮助都将不胜
{
while (true)
{
cancel();
}
}
我不知道该去哪一部分
任何帮助都将不胜感激
这是我的代码,如果有点混乱,我很抱歉。
我建议您学习使用IDE的调试器。有一些很棒的免费教程视频 使用调试器,您将不再需要在代码上到处放置
println
语句
真的,花点时间。您的代码非常冗长-大多数注释都是不必要的(即使您可能被要求包含它们),因此,不要:
minutesfromhours = 60*minutes;
//To change the hours into minutes by diving the hour by 60 for departure.
你可以写:
public final static int MINUTES_PER_HOUR = 60;
//...
minutes = MINUTES_PER_HOUR * hours;
整理好代码后,可以更容易地查看逻辑。您的try/catch可能会起作用
但是,不管怎样,您都会遇到处理输入的代码。您需要退出catch
块中的子例程,并处理try
块中的有效输入。e、 g
try
{
depart= Double.parseDouble(departtime);
System.out.println(depart);
System.out.println("Time accepted");
// do further processing here
}
catch (NumberFormatException e)
{
System.out.println(e.getMessage());
// exit here
}
这是解决办法。说真的,首先我认为你的计划行不通。但我在IDE中格式化了它,并添加了一个main方法来调用cancel()方法。成功了
import javax.swing.JOptionPane;
public class Time
{
public int difference =0;
// Stores the difference of departure and arrival time.
public static void main(String args [])
{
Time obj=new Time();
while (true)
obj.cancel();
}
public Time()
{
String departtime = JOptionPane.showInputDialog("Enter Depart Time in 24 hour time:");
// Allows the user to input their departure time into a JOptionPane.
String arrivaltime = JOptionPane.showInputDialog("Enter Arrival Time in 24 hour time:");
// Allows the user to input their arrival time into a JOptionPane.
double depart = 0;
// Store the time the user first inputs as a double.
double arrival = 0;
// Store the time the user inputs secondly as a double.
int minutes = 0;
// To store the hours for departure.
int minutes2 = 0;
// To store the minutes of departure.
int totalminutesfordeparture = 0;
// To store the full time for departure as minutes.
int minutesfromhours = 0;
// To store the hours as minutes for depature
int arrivals = 0;
// To store the hours arrival.
int arrival2 = 0;
// To store the minutes for arrival.
int arrivalhoursasminutes = 0;
// To store the arrival hours as minutes.
int totalminutesforarrival = 0;
// To store the full time for departure in minutes.
int actualtimehours= 0;
// The number of hours it will take on the journey.
int actualtimeminutes=0;
// The number of minutes it will take on the journey.
// **Start of removing the decimal for time 1 and time 2**\\
{
// Doesn't work
try
{
depart= Double.parseDouble(departtime);
System.out.println(depart);
}
catch (NumberFormatException e)
{
System.out.println(e.getMessage());
}
System.out.println("Time accepted");
arrival= Double.parseDouble(arrivaltime);
int time = (int)(depart*100);
// Gets rid of the decimal point in the departure time.
System.out.println ("Time with no decimal point "+ time);
// Check the decimal is being removed.
int time2=(int)(arrival*100);
// Gets rid of the decimal point in arrival time.
System.out.println ("Time with no decimal point "+ time2);
// Check the decimal is being removed in arrival.
// **End of removing the decimal in the times**\\
// **Start of seperating the hours and minutes in departure time**\\
{
minutes2=time%100;
// To separate the minutes from the hours for departure.
minutes=time/100;
// To seperate the hours ready to change them into minutes.
System.out.println("Hours of departure "+ minutes);
// Check that the hours are seperating from the minutes for
// departure.
System.out.println("Minutes of departure "+ minutes2);
// Check that the minutes are seperating from the hour for
// departure.
arrival2=time2%100;
// To separate the minutes from the hours.
arrivals=time2/100;
// To seperate the hours ready to change them into minutes.
System.out.println("Hours of arrival "+ arrivals);
// Check that the hours are seperating from the minutes for
// arrivals.
System.out.println("Minutes of arrival "+ arrival2);
// Check that the minutes are seperating from the hour for
// arrivals.
}
// **End of seperating the hours and minutes in departure time**\\
// **Start of hours being changed to minutes and adding it all up**\\
{
minutesfromhours = 60*minutes;
// To change the hours into minutes by diving the hour by 60 for
// departure.
System.out.println("Hours into minutes "+ minutesfromhours);
// Checks that the hours are being turned into minutes for
// departure.
totalminutesfordeparture= minutesfromhours+minutes2;
// To add together the hour minutes and the minutes from the
// time to give the total time in minutes for departure.
System.out.println("Total Departure time in minutes "+ totalminutesfordeparture);
// Checks that the hours as minutes are being added up with the
// minutes of the time for departure.
arrivalhoursasminutes = 60*arrivals;
// To change the hours into minutes for arrivals by diving the
// hours by 60 for arrivals.
System.out.println("Hours into minutes for arrival "+ arrivalhoursasminutes);
// Check that it is adding up the hour minutes and the minutes
// for departure.
totalminutesforarrival= arrivalhoursasminutes+arrival2;
// To add together the hour minutes and the minutes from the
// arrivals.
System.out.println("Total arrival time in minutes "+ totalminutesforarrival);
// Check that it is adding up the hour minutes and the minutes
// for arrivals
}
// **End of hours being changed to minutes and adding up**\\
// **Start of Finding the difference in minutes**\\
{
difference=totalminutesforarrival-totalminutesfordeparture;
// Works out the difference of minutes by taking arrival time in
// minutes away from the departure time in minutes.
System.out.println("Difference "+ difference);
// Check to see that it is taking arrival from departure.
JOptionPane.showMessageDialog(null, "It will take " + difference);
}
// **End of finding the difference in minutes**\\
// **start of not working changing minutes back to hours.**\\
{
actualtimehours= difference/60;
actualtimeminutes= difference/60;
System.out.println("It will take "+ actualtimehours);
System.out.println("It will take "+ actualtimeminutes);
}
}
}
// ** Method incase cancel button is pressed **\\
public void cancel()
{
String input=JOptionPane.showInputDialog("Cancel button was pressed");
if (input==null)
{
System.exit(0);
}
}
}
一些一般性评论
您应该将大部分代码移出Time()构造函数,并移到main方法中。此代码与实例化时间对象无关
while循环应该包含所有要重复的内容。在这种情况下,向用户询问出发时间、到达时间,并计算差值
如果您有重复的代码,为什么不使用一个方法要求用户输入时间字符串并对其进行解析呢。差不多
public class Time {
private int hours;
private int minutes;
etc...
}
// in main
while (true) {
Time departTime = askUser("depart");
Time arriveTime = askUser("arrive");
calculateDifference(departTime, arriveTime);
}
// elsewhere
public Time askUser(String name) {
String theTime = JOptionPane.showInputDialog(
String.format("Enter %s Time in 24 hour time:", name));
Time result = parseTime(theTime, name);
return result;
}
好吧,我现在已经想明白了:)在睡了一会儿,清早,仔细思考之后 首先,我把所有重要的事情都放进了方法中,就像你们很多人告诉我的那样,释放我的构造函数区域,是的,我同意现在更容易看到正在发生的事情 来解决try-catch问题。今天早上我意识到我把它放错了地方,它没有尝试我想要它尝试的东西,我想要它尝试的主线意味着我必须把我的其他代码放在里面,如果它被击中,catch语句现在结束。我只需要找出如何循环它而不是结束 为了解决我的另一个问题,即取消按钮,我使用了while(true)语句,并将它放在JOptionPane所在的位置,因为这是唯一可以按下取消按钮的两个位置。。。我不知道这是否正确,所以如果有人能告诉我是否正确(或者更好的是,我会在这些地方评论出来) 这是工作代码,里面仍然有一些bug,比如我必须找出如何将它限制在hh.mm之内,因为此时我可以输入任意时间。我还需要找出如何处理24小时和00时间,因为它现在根本不能处理这个问题,它也不喜欢你把12.00和3.00放进去,它会得出-9或者其他任何结果,这也是关于管理24小时的问题,它的最后一个小毛病是,如果你错了,它将关闭,而不是循环,我将试图解决现在
感谢大家昨晚对我的帮助,你们帮了我这么多请把这个问题分成三个独立的问题。我原以为把它作为一个问题来做会更容易,很抱歉这是我第一次使用这个网站,我原以为这样会更容易,论坛上的垃圾邮件也会更少。“家庭作业”标签应该是合适的我被告知我的任务工作我必须这样做,这让我知道我所做的实际上是正确的。这不会给我时间而不是分钟吗?我在几分钟内就有了它,所以我有1小时=60分钟,我不能用另一种方式取回它60分钟=1小时你的代码是:minutesfromhours=60*minutes;这是没有道理的。将分钟数乘以60得到秒。为变量选择好的名称比注释重要得多。是的,我已经修复了这个名称,并且得到了它,因此也有几分钟的时间。由于我试图一次解决太多的问题,我承受了很大的压力。谢谢你的帮助,这真的很有帮助,我试着找到有意义的名字,有时很难想到它们。谢谢你,我会看你的博客,因为我可能会经常访问它。它确实起作用了,只是我想我已经编辑了我的代码,使我的一个问题起作用了感谢上帝,我只是希望我能让try-catch(尝试捕捉)功能发挥作用,这是最烦人的,这样我就知道你做了什么,我只是检查了一下,如果我对如何使用“取消”按钮进行操作有点偏离,那么你所做的就是创建一个新对象,然后调用“取消”对象?我只是想反复检查一下,因为我可能会在某个时候再次使用它。因为我不太确定如何做到这一点,事实上,导师已经和我们一起经历了一些事情,但他是那些经历事情如此之快的导师之一,有时很难理解事情,没有时间提问。我正试图在其中添加方法,我只是对添加它们没有信心
public class Time {
private int hours;
private int minutes;
etc...
}
// in main
while (true) {
Time departTime = askUser("depart");
Time arriveTime = askUser("arrive");
calculateDifference(departTime, arriveTime);
}
// elsewhere
public Time askUser(String name) {
String theTime = JOptionPane.showInputDialog(
String.format("Enter %s Time in 24 hour time:", name));
Time result = parseTime(theTime, name);
return result;
}