未知的编译错误java

时间:2015-06-16 20:28:18

标签: java compilation syntax-error

我一直在用Java进行一些在线练习,但我无法弄清楚为什么这些代码不能编译。

我忘记了一些我知道的事情。

import java.util.Scanner;

class age {

public static void main (String[] args) {

Scanner keyboard = new Scanner (System.in);

int age;

System.out.println("How old are you?");
age = keyboard.nextInt();
System.out.println("You are " + age);

if (age =< 16) {
    System.out.println("You cannot Vote, Drive or Drink Alcohol");
}

if (age =<16 && <18) {
    System.out.println("You can DRIVE, you may NOT Vote or Drink Alcohol");
}

if (age =>18 && <21) {
    System.out.println("You can Drive and Vote, you may NOT Drink Alcohol");
}
if (age =>21){
    System.out.println("You May Drive, Vote and Drink Alcohol");
}

}
}

感谢回复,显而易见的(现在)事实是if语句必须有一个变量来测试每个条件,例如。 if(年龄> = 16&amp;&amp; age&lt; 18)。如果没有这两个变量(年龄),编译器会使用非特定错误消息吐出17个错误。

在命名变量和类名时,我会尽量遵循惯例。不需要让它变得更复杂。

再次感谢。

import java.util.Scanner;

class age {

public static void main (String[] args) {

Scanner keyboard = new Scanner (System.in);

int aga;

System.out.println("How old are you?");
aga = keyboard.nextInt();
System.out.println("You are " + aga);

if (aga <= 16) {
    System.out.println("You cannot Vote, Drive or Drink Alcohol");
}

if (aga >=16 && aga <18) {
    System.out.println("You can DRIVE, you may NOT Vote or Drink Alcohol");
}

if (aga >=18 && aga <21) {
    System.out.println("You can Drive and Vote, you may NOT Drink Alcohol");
}
if (aga >=21){
    System.out.println("You May Drive, Vote and Drink Alcohol");
}

}
}

我已经更改了if语句的参数,因此只能满足其中一个,将变量age更改为aga,并重写if语句的条件以便正确读取。

4 个答案:

答案 0 :(得分:2)

Java中的小于和大于运算符(坦率地说,在大多数编程语言中)分别是<=>=,而不是=<=>。代码目前有。

此外,您不能将比较运算符应用于单个变量/值 - 您需要有两个单独的条件,它们之间有逻辑运算符。

例如,而不是你当前的:

if (age =<16 && <18)

你需要写:

if (age <= 16 && age < 18)

另请注意,这种情况是多余的 - 如果年龄小于或等于16,则显然小于18,因此条件可以简化为

if (age <= 16)

答案 1 :(得分:1)

您忘记从Scanner软件包导入java.util类,并以错误的方式使用less than operator。以下是更正的片段:

import java.util.Scanner;

class age {

    public static void main(String[] args) {

        Scanner keyboard = new Scanner(System.in);

        int age;

        System.out.println("How old are you?");
        age = keyboard.nextInt();
        System.out.println("You are " + age);

        if (age <= 16) {
            System.out.println("You cannot Vote, Drive or Drink Alcohol");
        }

        if (age <= 16 && age < 18) {
            System.out
                    .println("You can DRIVE, you may NOT Vote or Drink Alcohol");
        }

        if (age <= 18 && age < 21) {
            System.out
                    .println("You can Drive and Vote, you may NOT Drink Alcohol");
        }
        if (age <= 21) {
            System.out.println("You May Drive, Vote and Drink Alcohol");
        }

    }
}

答案 2 :(得分:1)

正如用户之前提到的,您错误地使用了您的操作员。另外一件事值得一提的是你设置if语句的方式允许多次执行(我不确定你想要的是什么)。例如,如果age为10,则将执行所有四个println语句,因为它满足所有if语句中的每个条件。如果这不是您的意图,请考虑使用“if-else”语句。

答案 3 :(得分:0)

发布这些内容时必须显示错误消息,但即使没有,也会出现一些非常明显的错误:

  1. 您的测试形式应为&gt; =,&lt; =等(而不是=&gt;,=&lt;)
  2. if (age =<16 && <18)实际上只是if (a < 16)但如果您想要有2个条件则必须if (age =<16 && a<18)
  3. 在班级中使用与班级同名的变量是不好的形式。
相关问题