안녕하세요, 저는이 자바 프로그램을 사용하여 코멘트를 추가하기 전에 작업을 했었습니다.하지만 이제는 else와 if와 else 명령문에 오류가 있습니다. if 명령문이 없으면 if 문은 if 문을 읽는 방법을 방해합니다. 어떤 도움이라도 대단히 감사합니다. 의Java - else if 및 if else if?
/**
* 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");
}
}
예, 강조 표시된 부분을 볼 수 있습니다. –
그리고 '분'계산으로 원하는 것을하지 않을 것입니다.블록 주석은 블록이며,'/ *'와'* /'사이의 모든 것은 컴파일러에게 보이지 않습니다. –
오 와우 어떻게 그리웠는지 지금 고마워. – Reddy