我正在研究一个基于2个类的程序,这个程序可以在使用异常处理的同时转换军事时间。我遇到了3个编译器错误。前两个是Time类,它说它找不到可变时间的符号。第三个错误与我如何在TimeConverterTest类中显示我的结果有关,它说它无法找到militaryTime变量。异常处理错误
import javax.swing.*;
public class Time
{
private int hours;
private int minutes;
private boolean afternoon;
String am_pm;
public Time(String time)
{
}
public String convertToMilitary(String militaryTime)
{
try{
if (time == null)
{
throw new IllegalArgumentException ("No time was entered");
}
else if (time > 5)
{
throw new IllegalArgumentException ("Less than 5 characters were entered");
}
else
{
if (militaryTime.charAt(2) != ':')
{
throw new IllegalArgumentException ("The third character was not a colon");
}
else if (!Character.isDigit(militaryTime.charAt(0)))
{
throw new IllegalArgumentException ("The first character was not passed as a digit");
}
else if (!Character.isDigit(militaryTime.charAt(1)))
{
throw new IllegalArgumentException ("The second character was not passed as a digit");
}
else if (!Character.isDigit(militaryTime.charAt(2)) )
{
throw new IllegalArgumentException ("The fourth character was not passed as a digit");
}
else if (!Character.isDigit(militaryTime.charAt(4)))
{
throw new IllegalArgumentException ("The fifth character was not passed as a digit");
}
else
{
hours = Integer.parseInt(militaryTime.substring(0,2));
minutes = Integer.parseInt(militaryTime.substring(3,5));
if(hours > 23)
{
throw new IllegalArgumentException ("Hours passed were greater than 23");
}
else if(minutes > 59)
{
throw new IllegalArgumentException ("Minutes passed were greater than 59");
}
else if (hours > 12)
{
afternoon = false;
}
else if (hours == 0)
{
hours = 12;
}
else if (hours == 12)
{
afternoon = true;
}
else
{
System.out.println(hours + ":" + minutes);
}
}
}
}
catch (IllegalArgumentException exception)
{
JOptionPane.showMessageDialog(null,
exception.getMessage(),
"Invalid Argument", JOptionPane.ERROR_MESSAGE);
System.exit(0);
}
String am_pm = hours + ":" + minutes;
return "\n\n" + hours + ":" + minutes + " " + am_pm + "\n\n";
}
}
和TimeConverterTest是
import javax.swing.*;
import java.util.*;
import java.util.ArrayList;
import java.util.regex.Pattern;
public class TimeConverterTest {
public static final String[] menuChoice = {"Convert Military Time to Standard",
"Convert Standard Time to Military",
"Exit the System"};
public static void main(String[] args)
{
while(true){
JFrame frame = new JFrame("TIME CONVERSION MACHINE");
String option = (String) JOptionPane.showInputDialog(frame,
"What do you want to do?",
"Time Conversion Machine",
JOptionPane.QUESTION_MESSAGE,
null,
menuChoice,
menuChoice[0]);
int choice = Arrays.asList(menuChoice).indexOf(option) + 1;
System.out.print(choice);
Scanner input;
switch (choice) {
case 1:
input = new Scanner(System.in);
// Prompt user to enter Military time with message "Please enter Military Time:"
//CODE to make it work
System.out.printf("Please enter Military Time: ");
// Create militaryTime variable to accept input
//CODE to make it work;
String time;
time = input.nextLine();
// Create new 'timeToConvert' object using militaryTime
//CODE to make it work
Time timeToConvert = new Time(time);
// Using your newly created object call the necessary method to convert the time and
// Display your results
//Display your results
String results = timeToConvert.convertToMilitary(militaryTime);
JOptionPane.showMessageDialog(null,results);
break;
case 2:
case 3:
System.exit(0);
}
}
}
}// end of class TimeConverterTest