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 second time: ");
int lTime = in.nextInt();
int tDifference = Math.abs(fTime - lTime);
String strTDiff = String.valueOf(tDifference);
int length = strTDiff.length();
if (length == 4)
{
String hours = strTDiff.substring(0, 2);
String minutes = strTDiff.substring(3, 5);
}
else if (length == 3)
{
String hours = strTDiff.substring(0);
String minutes = strTDiff.substring(2, 4);
}
else
{
String hours = ("0");
String minutes = strTDiff.substring(0, 1);
}
System.out.println(hours + " hours " + minutes + " minutes");
}
}
大家好,我正在尝试使用 Java 制作一个简单的程序,以找出军事时间给出的 2 次之间的差异,并以小时数和分钟数给出输出。每当我在命令提示符下编译它时,最后打印行中的变量小时和分钟变量都会出现错误,提示“找不到符号”。我想尝试在 if 语句之前声明它们,但这也不起作用。我很抱歉,但我对编程很陌生,并感谢提供的任何帮助。