嘿所以我有這個Java程序工作之前,我添加了評論,但現在我得到一個錯誤與其他如果和其他聲明說他們沒有一個if是否是干擾if語句被讀取的意見?任何幫助深表感謝。因爲}
之前Java - 否則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