gpt4 book ai didi

java - 根据用户输入查找输入形状的面积

转载 作者:行者123 更新时间:2023-12-02 05:09:01 25 4
gpt4 key购买 nike

我想用 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);
}
}
}

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

最佳答案

您需要将static添加到main方法中,并创建一个area的新实例。请参阅下面的代码。

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 - 根据用户输入查找输入形状的面积,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27503371/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com