Java 从具有多个变量的方法返回值
我遇到的问题是,我不知道主要如何调用这些方法来使它们工作 首先,我问用户,在“publicstaticvoiduserinput”方法中,他希望计算它的面积是什么形状。用户有3个选项可供选择 然后有三种方法,一种是圆,一种是三角形,一种是正方形Java 从具有多个变量的方法返回值,java,Java,我遇到的问题是,我不知道主要如何调用这些方法来使它们工作 首先,我问用户,在“publicstaticvoiduserinput”方法中,他希望计算它的面积是什么形状。用户有3个选项可供选择 然后有三种方法,一种是圆,一种是三角形,一种是正方形 public static void userInput(double radius, int base, int height, int side){ int object = Integer.parseInt(JOptionPane.sho
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方法,考虑到您是初学者,我建议您阅读一本解释所有语言的书,用Java或Head First Java思考应该非常适合您,请查看它们。“试着对Java方法进行一些实践。”这就是我本周要做的。我提供的答案正确吗?是的,谢谢。这两行对我来说是新的“NewClass1 obj=newnewclass1();”和“obj.userInput(object);”第一行是在我创建类NewClass1的对象时。从类创建的每个对象都有该类中定义的所有方法。然后我将用户输入值对象传递给方法Userinput。此处通过if语句选择了所需多边形。有趣。“System.out.println”是绑定方法的工具。结构是c语言家族的一部分;它们不是java的一部分。在java中,您可以返回类的对象