gpt4 book ai didi

java - 异常处理错误

转载 作者:行者123 更新时间:2023-12-02 11:10:41 26 4
gpt4 key购买 nike

我正在开发一个基于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

最佳答案

如果未声明变量,则将出现cannot be resolved to a variable编译错误

Time类中

public String convertToMilitary(String militaryTime) {
try {
if (militaryTime == null) {
throw new IllegalArgumentException("No time was entered");

} else if (militaryTime.length() < 5) {
throw new IllegalArgumentException("Less than 5 characters were entered");
TimeConverterClass
        String results = timeToConvert.convertToMilitary(time);

关于java - 异常处理错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33138622/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com