2013-01-31 35 views
-5

嘿所以我有這個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"); 
    } 
} 
+3

是的,你可以從突出顯示。 –

+0

而且它當然不會按照'分鐘'計算來做你想要的。塊註釋就是這樣,一個塊,'/ *'和'* /'之間的任何內容對編譯器都是「不可見的」。 –

+0

哇哇我怎麼想我現在得到它謝謝。 – Reddy

回答

9

這是你最後一次else/**/之間的評論。

任何正確的代碼編輯器的顏色應該明顯,就像它在問題中一樣。

3

您已經評價右花支架,如果,否則和elseif.Remove它

1

正如你可以清楚地看到這裏直接在網站(語法高亮)你確實註釋掉其中的一些上:}。三,如果我沒有記錯。這就是導致錯誤的原因。你還註釋了三行代碼(這可能是有意的,但我想我會提到它)。我建議你使用一些IDE。我自己使用Eclipse for Java。

可以使用雙斜槓,//註釋掉單個行,在Java(一切以雙斜線的權利將被註釋掉)

1

你有你所有的如果,否則註釋掉右括號如果和其他語句

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. 
}          */ 

它看起來像你不知道如何塊註釋工作。您假設註釋僅適用於每行上*字符後面的文本。實際上,所有角色(包括換行符和回車符)都會被註釋掉,直到達到*/。 你可能想改變你的代碼,以便它看起來像這樣爲每個條目:

if (length == 4) 
{ 
    /* If the number of digits is 4, the first 
    * two are the hour. 
    */ 
    hours = strTDiff.substring(0, 2); 
    minutes = strTDiff.substring(2, 4); 
}   

這可能是有用的注意,在註釋的第二行中的明星是沒有必要的。例如:

/* If the number of digits is 4, the first 
    two are the hour. 
*/ 

這仍然是一個有效的塊註釋。 (儘管不可讀)

1

是的。您的*/(關閉註釋)與}(關閉if塊)位於同一行,因此使其成爲註釋的一部分。它還評論了一些我假設你想要執行的代碼。

塊註釋標記/**/使它們之間的所有內容變成註釋,包括換行符。代碼開始處的註釋中每個新行的星號僅用於樣式和對齊方式,並沒有實際意義。

0

您已插入註釋,以便將大部分編程內容包含在該註釋中。只要刪除你的評論,然後編譯該程序。 您應該使用BlueJNet Beans,它會向您展示其不同的配色方案,指出您的編程模式有什麼問題。