Java 所以,我认为这是基本的,但我需要知道

Java 所以,我认为这是基本的,但我需要知道,java,stack,Java,Stack,所以,对于作业,我必须做这部分,但如果部分是红色的,我不明白为什么 if (int >=1 && int<=7){ Scanner sc = new Scanner(System.in); String direction; System.out.println("Enter your choice: "); direction=sc.nextD(); if (direction = 'left'){ maze.

所以,对于作业,我必须做这部分,但如果部分是红色的,我不明白为什么

if (int >=1 && int<=7){
    Scanner sc = new Scanner(System.in);
    String direction;
    System.out.println("Enter your choice: ");
    direction=sc.nextD();
    if (direction = 'left'){
        maze.push('left');
        System.out.println("Pushed left to stack");

如果(int>=1&&int那么,以下是我看到的一些问题:

  • int
    不是一个变量;它是一个基本类型的名称。在java中,不能将任何变量命名为
    int
    ,当然也不能与之进行比较

  • 扫描仪没有名为nextD()的类。您可能正在查找nextLine

  • 在Java中,字符串用双引号括起来,而不是单引号括起来。此外,要比较字符串,需要使用direction.equals(“字符串”)。请参阅azurefrog的注释


  • 我假设您使用的是Eclipse或IntelliJ之类的IDE(如果不是,您可能应该这样做)。将鼠标悬停在红色部分上,它会告诉您它在抱怨什么。

    以下是对代码的更正:-

    int i=0;
    int b=0;
    if (i>=1 && b<=7){ /*you need to have variables for your int and u must 
    define them before the if statment*/ 
    Scanner sc = new Scanner(System.in);
    String direction;
    System.out.println("Enter your choice: ");
    direction=sc.next(); /*no D for Strings and .nextLine() if its separated by 
    spaces*/.
    if (direction.equals("left")) { /*String uses .equals() method inside for  
    comparing equal strings*/.
        maze.push("left"); //String uses literals " "
        System.out.println("Pushed left to stack");
    }}
    
    inti=0;
    int b=0;
    
    如果(i>=1&&b有一个
    a
    类型
    int
    的变量
    a
    if(a>=1&&a)那样去做,那就是编译器告诉你你的代码出了问题。我不是一个编译器,但从我的头脑里看
    'left'
    不是
    字符串,在Java中字符串用双引号表示:
    “left”“
    。另外,
    如果(direction=“left”)
    是赋值,而不是比较。另外,
    如果(direction==“left”)
    是比较引用等式,也将失败。请参阅替换
    如果。”(int a>=1&&int a=1&&a谢谢。现在很好用!只是给将来的提示:在编写代码时,确保在错误出现时进行修复,而不是在结束时进行修复。这将在将来为您节省大量时间。在这种情况下,不应在if语句中声明变量。此外,
    if中缺少结束符(direction.equals(“left”)
    它不会编译。它也永远不会超过第一个if语句,因为我将始终为0。它也很难阅读,因为您没有解释答案,而是在代码段中塞满了一堆注释(也缺少缩进)。:)我只是向他展示了他的问题,代码不需要编译,因为我没有他的push方法。
    int i=0;
    int b=0;
    if (i>=1 && b<=7){ /*you need to have variables for your int and u must 
    define them before the if statment*/ 
    Scanner sc = new Scanner(System.in);
    String direction;
    System.out.println("Enter your choice: ");
    direction=sc.next(); /*no D for Strings and .nextLine() if its separated by 
    spaces*/.
    if (direction.equals("left")) { /*String uses .equals() method inside for  
    comparing equal strings*/.
        maze.push("left"); //String uses literals " "
        System.out.println("Pushed left to stack");
    }}