-6

嘿,所以我在添加注释之前让这个 Java 程序工作,但现在我收到 else if 和 else 语句的错误,说它们没有 if,这些注释是否干扰了 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");
    }
}
4

6 回答 6

8

那是因为}就在您的最后一个之前在andelse之间进行了评论。/**/

任何适当的代码编辑器的颜色都应该使它明显,就像它在问题中一样。

于 2013-01-31T18:50:34.437 回答
3

您已经评论了 if、else 和 elseif 的右大括号。删除它

于 2013-01-31T18:50:36.180 回答
1

正如您可以在网站上直接看到的那样(语法突出显示),您确实已经注释掉了其中的一些:}. 三,如果我没记错的话。这就是导致错误的原因。您还注释掉了三行代码(这可能是故意的,但我想我会提到它)。我建议你使用一些IDE。我自己使用 Eclipse for Java。

您可以在 Java 中使用双斜杠 , 注释掉单行//(双斜杠右侧的所有内容都将被注释掉)

于 2013-01-31T18:50:48.177 回答
1

您的所有 if、else if 和 else 语句中的右括号都被注释掉了

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

这仍然是有效的块注释。(虽然不那么可读)

于 2013-01-31T18:50:56.853 回答
1

是的。您的(关闭评论)与(关闭块)*/在同一行,因此使其成为评论的一部分。它还注释掉了一些我假设你想要执行的代码。}if

块注释标记/*并将*/它们之间的所有内容变成注释,包括换行符。代码开头注释中每个新行上的星号仅用于样式和对齐,并没有实际意义。

于 2013-01-31T18:50:58.007 回答
0

You have inserted comment in a way that your most of the programming is enclosed into that comment. Just remove your comments and then compile the program. You should rather use BlueJ or Net Beans which will show you with its different color schemes that what is wrong with your programming pattern.

于 2013-03-02T10:47:06.313 回答