我正在开发一个基于2个类的程序,这个类可以转换军事时间,同时还使用异常处理。我遇到3个编译错误。前2个属于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
答案 0 :(得分:0)
如果未声明变量
,您将收到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);
答案 1 :(得分:0)
您必须将时间变量声明为
String time;
public Time(String time)
{
this.time = time;
}
在Time类中,以便可以使用它,因为你得到的字符串时间与放在
中一样// Create militaryTime variable to accept input
//CODE to make it work;
String time;
time = input.nextLine();
然后将该变量传递为
//Display your results
String results = timeToConvert.convertToMilitary(time);
答案 2 :(得分:0)
将time
变量传递给Time
类构造函数是正确的,但是,您从未将变量实际传输到类中。下面我在time
下面声明了一个实例变量am_pm
,并且在构造函数中我将它设置为等于传入的time
变量。
private int hours;
private int minutes;
private boolean afternoon;
String am_pm;
String time;
public Time(String time)
{
this.time = time;
}
您似乎对方法参数和方法返回值感到困惑。你需要在构造函数中传递time变量并在类中使用它,或者直接将它传递给convertToMilitary
,而不需要通过构造函数。
convertToMilitary
的返回值是您的预定军事时间,参数应该只是常规时间值。记住这一点,你的代码应该是这样的。
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 militaryTime = timeToConvert.convertToMilitary(time);
JOptionPane.showMessageDialog(null,militaryTime );