Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/324.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 - Fatal编程技术网

Java 根据用户输入查找输入形状的区域

Java 根据用户输入查找输入形状的区域,java,Java,我想用Java创建一个程序,它根据用户输入查找输入形状的区域。但我没能做到这一点 这是我的剧本: import java.util.Scanner; public class area { static Scanner advance = new Scanner(System.in); public void main(String[] args) { nu(); } int length; int height; int area; public void nu()

我想用Java创建一个程序,它根据用户输入查找输入形状的区域。但我没能做到这一点

这是我的剧本:

import java.util.Scanner;

public class area {
static Scanner advance = new Scanner(System.in);
public  void main(String[] args) {

    nu();
}
int length;
    int height;
int area;


public  void nu(){
    String message = advance.nextLine();

    if (message.equalsIgnoreCase("rectangle")){

        System.out.println("Enter the length of the rectangle: ");

        length  = advance.nextInt();

        //length declared.//
        System.out.println("Enter the height of the rectangle");

        height = advance.nextInt();

        //Height has been declared.//

        area = length * height;

        System.out.print("The area is: " + area);
    }
}
}

第一个问题是,这段代码没有运行,所以我不知道它是否工作。其他一切都很好。你能告诉我,我做错了什么吗?

你需要在main方法中添加
static
,并创建
区域的新实例。请参阅下面的代码

public class area {
static Scanner advance = new Scanner(System.in);
public static void main(String[] args) {

    new area().nu();
}
int length;
int height;
int area;


public  void nu(){
    String message = advance.nextLine();

    if (message.equalsIgnoreCase("rectangle")){

        System.out.println("Enter the length of the rectangle: ");

        length  = advance.nextInt();

        //length declared.//
        System.out.println("Enter the height of the rectangle");

        height = advance.nextInt();

        //Height has been declared.//

        area = length * height;

        System.out.print("The area is: " + area);
    }
  }
}

由于Java程序无法找到要运行的主方法: 我们需要添加静态关键字在3个地方根据您的代码

请使用以下完整源代码进行尝试:

import java.util.Scanner;

public class area {
    static Scanner advance = new Scanner(System.in);

    public  static void main(String[] args) { //1st change

        nu();
    }
    static int length; //2nd change
    static int height; //2nd change
    static int area; //2nd change

    public static  void nu(){ //3rd change
        String message = advance.nextLine();

        if (message.equalsIgnoreCase("rectangle")){

            System.out.println("Enter the length of the rectangle: ");

            length  = advance.nextInt();

            //length declared.//
            System.out.println("Enter the height of the rectangle");

            height = advance.nextInt();

            //Height has been declared.//

            area = length * height;

            System.out.print("The area is: " + area);
        }
    }
}
  • 您需要将
    静态
    添加到程序的
    主方法
  • 您将无法从
    main
    直接调用
    nu()
    方法,因为调用它是
    实例方法,您需要创建类的对象

    public static void main(String[] args) {
    
        area a = new area();
        a.nu();
    }
    
  • 另一种选择是,您可以将
    nu()
    方法设置为静态,但这样就不能使用
    int-length;内部高度;内部区域静态方法nu()
    中编码>实例变量。然后,您需要使这些变量也是静态的,或者创建类的对象,然后根据需要使用这些变量


  • static
    放回
    main
    nu
    声明:如果您是这里的新用户,我告诉您,如果您得到问题的解决方案,您应该在这里接受答案。这有助于其他人了解什么对你有用。要接受答案,只需单击要选择的答案左侧的右勾号。