任何人都可以帮我解决我的2个Java问题。 1是try catch 2是放置一段代码的地方
题
我正在编写一段代码,我正在为uni工作,事实上我用Java并不是那么好,但我已经尝试过了。我试图让我的尝试捕获工作但它似乎永远不会做它支持的。我认为我有异常错误,但我不确定哪个例外用于解决问题,因为我试图阻止输入字母而只是输入数字。
我遇到的最后一个问题是我不知道将这段代码放在哪里:
{
while (true)
{
cancel();
}
}
我不确定支持哪一部分。
任何帮助都会受到很大的关注。
这是我的代码,如果它有点乱,我很抱歉。 编码
解决方案
您的代码非常冗长 - 大多数评论都是不必要的(即使您可能会被要求包含它们)所以代替:
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可能 工作
try
{
depart= Double.parseDouble(departtime);
System.out.println(depart);
}
catch (NumberFormatException e)
{
System.out.println(e.getMessage());
}
System.out.println("Time accepted");
但是你接下来的代码然后处理输入而不管。您需要退出 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所在的位置,因为只有2个地方可以按下取消...我不知道知道那个是否正确所以如果有人可以告诉我它是否是(或者更好的是我会在那些地方发表评论)
所以这里是工作代码,还有一些错误就像我必须找到如何将它限制为hh.mm,因为我可以随意放入任何时间。我还需要找到如何处理24小时00时,因为它目前根本没有处理它,它也不喜欢它,如果你把12.00和3.00提出-9或任何它已经解决了它再说一次是关于24小时管理,最后一件小问题就是如果你错了,它会关闭而不是循环,我现在要解决这个问题。
谢谢大家的帮助昨晚你们都帮了我很多