简体   繁体   English

未知的编译错误Java

[英]Unknown compiling error java

I have been going through some online exercises in Java, and I can't figure out why this tid-bit of code won't compile. 我一直在用Java进行一些在线练习,但我不知道为什么这些小技巧无法编译。

I am forgetting something obvious I know it. 我忘记了一些我知道的明显的东西。

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");
}

}
}

Thanks for the replies, The obvious (now) fact that the if statements have to have a variable to test for each condition eg. 感谢您的答复。很明显(现在)的事实是,if语句必须具有变量才能测试每个条件,例如。 if (age >=16 && age <18). if(年龄> = 16 &&年龄<18)。 without the both variables (age) the compiler spat out 17 errors with non-specific error messages. 如果没有这两个变量(年龄),则编译器会吐出17个带有非特定错误消息的错误。

I will try to make sure to follow convention when naming variables and class names. 在命名变量和类名时,我将尝试确保遵循约定。 no need to make it extra complicated. 无需使其变得更加复杂。

Thanks again. 再次感谢。

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");
}

}
}

I have changed the parameters of the if statements so only one of them can be satisfied, changed the variable age to aga, and re-written the conditions of the if statements to read correctly. 我已经更改了if语句的参数,因此只能满足其中之一,将变量age更改为aga,然后重新编写if语句的条件以正确读取。

The less than and greater than operators in Java (and frankly, in most programming languages) are <= and >= , respectively, not =< and => like your code currently has. Java中的小于和大于运算符(坦率地说,在大多数编程语言中)分别是<=>= ,而不是像当前代码一样=<=>

Additionally, you cannot apply to comparison operators to a single variable/value - you'd need to have two separate conditions with a logical operator between them. 此外,您不能将比较运算符应用于单个变量/值-您需要具有两个单独的条件,并且它们之间必须有逻辑运算符。

Eg, instead of your current: 例如,代替您当前的:

if (age =<16 && <18)

You'd need to write: 您需要写:

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

Note also that this condition is redundant - if an age is less than or equal to 16, it is obviously less than 18, so the condition could be simplified to 另请注意,此条件是多余的-如果年龄小于或等于16,则显然小于18,因此可以将条件简化为

if (age <= 16)

You forget to import Scanner class from java.util package and also using the less than operator in wrong way. 您忘记了从java.util包中导入Scanner类,并且还以错误的方式使用了less than operator Below is the corrected snippet: 以下是更正后的代码段:

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");
        }

    }
}

As the users have mentioned before, you used your operator incorrectly. 如用户之前所述,您错误地使用了操作员。 One other thing worth nothing here is that the way you have set up your if statements allows multiple executions (which I'm not sure is what you wanted). 另一件事毫无价值,那就是您设置if语句的方式允许多次执行(我不确定这是您想要的)。 For example, if age were 10, all four println statements would be executed because it satisfies every single condition in all of the if statements. 例如,如果age为10,则将执行所有四个println语句,因为它满足所有if语句中的每个条件。 Consider using 'if-else' statements if this is not your intention. 如果这不是您的意图,请考虑使用“ if-else”语句。

You have to show the error message when you post these but even without that there are some pretty glaring errors: 发布这些消息时,必须显示错误消息,但即使没有这些错误消息,也会出现一些明显的错误:

  1. The form of your tests should be >=, <=, etc (and not =>, =<) 测试的形式应为> =,<=等(而不是=>,= <)
  2. if (age =<16 && <18) is really just if (a < 16) but if you want to have 2 conditions it has to be if (age =<16 && a<18) if (age =<16 && <18)实际上只是if (a < 16)但如果要具有2个条件,则必须为if (age =<16 && a<18)
  3. It's bad form to have a variable inside your class with the same name as your class. 在类中使用与您的类同名的变量是一种不好的形式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM