我正在研究一個基於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