gpt4 book ai didi

java - 从具有多个变量的方法返回值

转载 作者:太空宇宙 更新时间:2023-11-04 14:03:58 26 4
gpt4 key购买 nike

我遇到的问题是我不知道如何调用 main 中的方法以使它们工作。

首先,我询问用户他想在“public static void userInput”方法中计算什么形状的面积。用户有 3 个选项可供选择。

然后有3种方法,1个圆形,1个三角形,1个正方形。

public static void userInput(double radius, int base, int height, int side){

int object = Integer.parseInt(JOptionPane.showInputDialog(
"Enter 1 if you want to know the area of a circle."
\nEnter 2 if you want to know the area of a triangle.
\nEnter 3 if you want to know the area of a square"));

if (object == 1){
radius = Double.parseDouble(JOptionPane.showInputDialog("Enter the radius"));


}else if (object == 2){
base = Integer.parseInt(JOptionPane.showInputDialog("Enter the base"));
height = Integer.parseInt(JOptionPane.showInputDialog("Enter the height"));


}else if (object == 3){
side = Integer.parseInt(JOptionPane.showInputDialog("Enter the side"));

}
}


public static double circle(){
double radio = 0;
double circle = (radio * radio) * 3.14159265358;
JOptionPane.showMessageDialog(null, "The circle area is "+circle);
return circle;
}

public static int triangle(){
int base= 0;
int height= 0;
int triangle = (base * height)/2;
JOptionPane.showMessageDialog(null, "The triangle area is "+triangle);
return triangle;
}

public static int square(){
int side = 0;
int square = side * side;
JOptionPane.showMessageDialog(null, "The square area is "+square);
return square;
}

public static void main(String[] args) {
circle(userInput(radius));
triangle(userInput(base, height));
square(userInput(side));
}

有人可以推荐一本关于方法的好读物吗?

最佳答案

希望这是您所寻找的:

import javax.swing.JOptionPane;

public class NewClass1 {
public void userInput(int x){



if (x == 1){
double radius = Double.parseDouble(JOptionPane.showInputDialog("Enter the radius"));
System.out.println(circle(radius));


}else if (x == 2){
int base = Integer.parseInt(JOptionPane.showInputDialog("Enter the base"));
int height = Integer.parseInt(JOptionPane.showInputDialog("Enter the height"));
System.out.println(triangle(base,height));

}else if (x == 3){
int side = Integer.parseInt(JOptionPane.showInputDialog("Enter the side"));
System.out.println(square(side));
}
}


public static double circle(double radio){

double circle = (radio * radio) * 3.14159265358;
JOptionPane.showMessageDialog(null, "The circle area is "+circle);
return circle;
}

public static int triangle(int base,int height){
int triangle = (base * height)/2;
JOptionPane.showMessageDialog(null, "The triangle area is "+triangle);
return triangle;
}

public static int square(int side){
int square = side * side;
JOptionPane.showMessageDialog(null, "The square area is "+square);
return square;
}

public static void main(String[] args) {

NewClass1 obj = new NewClass1();
int object = Integer.parseInt(JOptionPane.showInputDialog(
"Enter 1 if you want to know the area of a circle\nEnter 2 if you want to know the area of a triangle.\nEnter 3 if you want to know the area of a square"));
obj.userInput(object);
}
}

尝试对 Java 方法进行一些练习。

关于java - 从具有多个变量的方法返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29052038/

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