简体   繁体   English

有人知道我在执行这个 java 代码时做错了什么吗?

[英]Anyone know what I am doing wrong to execute this java code?

I am just trying to execute some Java code and print them out to the console, however, when I try to run it, it gives an error and says illegal start to expression.我只是想执行一些 Java 代码并将它们打印到控制台,但是,当我尝试运行它时,它会出错并说非法开始表达。 Does anyone know what is going on here?有谁知道这里发生了什么?

    package com.craneai;

public class Main {

    public static void main(String[] args) {

        public static void int F(int N) {
            int X, Y, Z, I;

            int X = 2;
            if(N <= 0) {
                X = 3;
            } else {
                Y = 1;
                Z = 1;
                for (int I = 3; I <=N; I++) {
                    X = Y + Z;
                    Z = Y;
                    Y = X;
                }
            }
            return X;
        }


        System.out.println(F(6));
        System.out.println(F(0));
        System.out.println(F(1));

    }



}
public static void main(String[] args) {


        System.out.println(F(6));
        System.out.println(F(0));
        System.out.println(F(1));

    }

    public static int F(int N) {
        int X, Y, Z, I;

        X = 2;
        if(N <= 0) {
            X = 3;
        } else {
            Y = 1;
            Z = 1;
            for (int i = 3; i <=N; i++) {
                X = Y + Z;
                Z = Y;
                Y = X;
            }
        }
        return X;
    }

A few things to consider:有几点需要考虑:

1st.第一个。 You're declaring a void return and then a int, the function must have only 1 return type.您要声明一个 void 返回,然后是一个 int,function 必须只有 1 个返回类型。

public static void int F(int N)

2nd.第二。 The method should be outside the main method该方法应该在main方法之外

3rd.第三。 You declared X twice in the code, the second time you call it, you don't have to declare the type, the compiler will try to process it as a new variable with a name that is already in use.您在代码中声明了X两次,第二次调用它时,您不必声明类型,编译器将尝试将其作为名称已在使用中的新变量处理。

4th.第四。 In your for method, you declared X , once again, Java is case sensitive, so X and x are different in the compiler.在您的for方法中,您再次声明X , Java 区分大小写,因此Xx在编译器中是不同的。

5th.第五。 You've also declared I twice, you don't need to provide a temporary variable before calling the for method, since you can do it inside the for loop;您还声明了两次I ,您不需要在调用for方法之前提供临时变量,因为您可以在for循环中执行此操作;

Looks like you're starting to learn Java, a few tips from who works with it since the Java 5..看起来您开始学习 Java,从 Java 5..

  • Don't declare your variables with a single letter;不要用单个字母声明变量;
  • Don't declare your functions/methods with a single letter;不要用一个字母声明你的函数/方法;
  • Don't declare your variables in UPPER CASE, always low case and camelCase;不要在大写中声明你的变量,总是小写和驼峰式;

Even as an example you should make your code cleaner, improve the readability for others, other people will be happier in the future if you do this (this include yourself).即使作为示例,您应该使您的代码更清洁,提高其他人的可读性,如果您这样做,其他人将来会更快乐(这包括您自己)。

First: you're declaring a method inside another method - see that the F method is inside the main method.首先:您在另一个方法中声明一个方法 - 看到F方法在main方法中。 Java does not support nested methods. Java 不支持嵌套方法。

Second: you're declaring X and I twice.第二:你声明XI两次。

Third: You wrote two return types to your method: void and int .第三:您为方法编写了两种返回类型: voidint

This will work:这将起作用:

public class Main {

    public static void main(String[] args) {
        System.out.println(F(6));
        System.out.println(F(0));
        System.out.println(F(1));
    }

    public static int F(int N) {
        int X, Y, Z, I;

        X = 2;
        if (N <= 0) {
            X = 3;
        } else {
            Y = 1;
            Z = 1;
            for (I = 3; I <= N; I++) {
                X = Y + Z;
                Z = Y;
                Y = X;
            }
        }
        return X;
    }

}

Three main errors:三个主要错误:

  1. Nested method: F method inside main嵌套方法: main里面的F方法
  2. Method F return int and void方法 F 返回intvoid
  3. You declared X and I twice.你宣布 X 和 I 两次。

Here is your class fixed:这是您的 class 固定:

public class Main {
    
        public static void main(String[] args) {
            System.out.println(F(6));
            System.out.println(F(0));
            System.out.println(F(1));
    
        }
    
        public static int F(int N) {
            int X = 2, Y, Z;
    
            if (N <= 0) {
                X = 3;
            } else {
                Y = 1;
                Z = 1;
                for (int I = 3; I <= N; I++) {
                    X = Y + Z;
                    Z = Y;
                    Y = X;
                }
            }
            return X;
        }
    }

Output is going to be this: Output 将是这样的:

8
3
2

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

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