Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java程序在运行中无法正常工作_Java_If Statement - Fatal编程技术网

Java程序在运行中无法正常工作

Java程序在运行中无法正常工作,java,if-statement,Java,If Statement,我一直在学习java课程,我被其中一个作业困住了。我能得到一些帮助吗?我的代码在这里: 作业是: 使用if语句、else-if语句和else语句制作一个程序,根据给定的年龄显示不同的消息 年龄信息 不到16岁就不能开车了。 16到17岁你可以开车但不能投票。 18到24岁你可以投票,但不能租车。 25岁或更大,你几乎可以做任何事 请注意,与原始的“您的年龄”分配不同,此程序在给定的年龄内只能显示一条消息,不能显示多条消息。您有语法错误 请尝试: if ( age < 16 ) {

我一直在学习java课程,我被其中一个作业困住了。我能得到一些帮助吗?我的代码在这里:

作业是: 使用if语句、else-if语句和else语句制作一个程序,根据给定的年龄显示不同的消息

年龄信息 不到16岁就不能开车了。 16到17岁你可以开车但不能投票。 18到24岁你可以投票,但不能租车。 25岁或更大,你几乎可以做任何事


请注意,与原始的“您的年龄”分配不同,此程序在给定的年龄内只能显示一条消息,不能显示多条消息。

您有语法错误

请尝试:

if ( age < 16 )
{
    System.out.println( "You cant drive");
}
else if ( age <= 17 )
{
    System.out.println( "You can drive, but not vote." );
}
else if ( age <= 24 )
{
    System.out.println( "You can vote but not rent a car" );
}
else 
{
    System.out.println( "You can do pretty much anything" );
}
您可以了解if/then/else语句

多个问题

if ( age < 16 ) {   //SEE NO ; here. ; makes end of statement 
    System.out.println( "You cant drive");
}
else if( age <= 17 ) {
   //something else
}
试试这个

  if (age < 16){
        System.out.println( "You cant drive");
    } else if (age <= 17) {
        System.out.println( "You can drive, but not vote." );
    } else if (age <= 24) {
        System.out.println( "You can vote but not rent a car" );
    } else {
        System.out.println("You can do pretty much anything");
    }

删除所有

语句中的分号,看起来您已经交换了else和else if语句。else语句不应采用else if语句的条件。也没有,;在这种情况下。即它应该是:

if (age < 16) {
...
} else if (age <= 17 ) {
...
} else {
...
}

语法错误。不能将条件语句附加到关键字else。 请参考以下代码:

if ( age < 16 )
    {
        System.out.println( "You cant drive");
    }

    else if( age <= 17 )
    {
        System.out.println( "You can drive, but not vote." );
    }

    else if ( age <= 24 )
    {
        System.out.println( "You can vote but not rent a car" );
    }

    else
    {
        System.out.println( "You can do pretty much anything" );
    }

你到底在坚持什么?@JunedAhsan,如果你没有为if提供足够的参数,那么代码或多或少是通过提供一个指向pasteBin的链接发布的。删除if或提供参数。在其他else语句中的语句之前也缺少if。也许您还应该提到javac给您的错误。我们猜不到。请学习在IDE环境中工作。Java的主要示例是Netbeans和Eclipse。这将确保你不必在这样的网站上经常询问编译器错误。我不明白为什么这个答案会被否决,因为这是实际的解决方案。即使有一个恰当的解释也很好。@skiwi它最初有一个假的semikolons-现在看起来更好了,我是一个反对者,我这样做是因为它只是鱼,而不是鱼竿-换句话说,它不能解释为什么或什么是错的。对于这个答案没有用处的情况,建议进行向下投票。鉴于询问者正在学习Java课程,在不解释任何内容的情况下只说应该如何做是没有用的。@mthmulders我现在在下面添加了一些链接。。希望提问者能学到一些东西
if(age>24) {
     {
            System.out.println( "You can do pretty much anything" );
        }

}
 if( age >= 17 && age<=24 )
{
    System.out.println( "You can vote but not rent a car" );
}

if( age <= 17 && age>=16 )
 {
System.out.println( "You can drive, but not vote." );
}
 if ( age<16 )
  {
     System.out.println( "You cant drive");
   }
if ( age < 16 )
    {
        System.out.println( "You cant drive");
    }

    else if( age <= 17 )
    {
        System.out.println( "You can drive, but not vote." );
    }

    else if ( age <= 24 )
    {
        System.out.println( "You can vote but not rent a car" );
    }

    else
    {
        System.out.println( "You can do pretty much anything" );
    }