コメントを追加する前にこの Java プログラムが動作していたのですが、else if ステートメントと else ステートメントでエラーが発生しました。どんな助けでも大歓迎です。
/**
* A class that takes 2 different times in military time as input and
* outputs the difference in hours and minutes.
*
* @author Name
*/
import java.util.Scanner;
public class Assignment1Q3
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("Please enter the first time: ");
int fTime = in.nextInt();
System.out.print("Please enter the second time: ");
int lTime = in.nextInt();
int tDifference = Math.abs(fTime - lTime);//Calculates the absolute difference.
String strTDiff = String.valueOf(tDifference);//Converts the value to a String.
int length = strTDiff.length();//Obtains the length of the value.
String hours = "";//Declares the values to be initialized in the if statement.
String minutes = "";
if (length == 4)
{
hours = strTDiff.substring(0, 2);/**If the number of digits is 4, the first
minutes = strTDiff.substring(2, 4);*two are the hour.
} */
else if (length == 3)
{
hours = strTDiff.substring(0, 1);/**If the number of digits is 3, the first
minutes = strTDiff.substring(1, 3);*one is the hour.
} */
else
{
hours = ("0"); /**If the number of digits is not 4 or 3,
minutes = strTDiff.substring(0, 1);*the value is less than 1 hour.
} */
System.out.println(hours + " hours " + minutes + " minutes");
}
}