[英]calling method to a main in java
I'm writing a program that calculates the hypotenuse of a triangle, and I'm supposed to call up a method into the main. 我正在编写一个计算三角形斜边的程序,并且应该在主函数中调用一个方法。
Is it better to have them in 2 separate files, or to have the method I'm calling up in the program I'm running? 最好将它们放在2个单独的文件中,还是在正在运行的程序中使用我要调用的方法?
In the program, I keep getting error messages about the last line of code, with the JOptionPane output. 在程序中,我通过JOptionPane输出不断收到有关最后一行代码的错误消息。
What am I getting wrong? 我怎么了?
import javax.swing.JOptionPane;
import java.util.Scanner;
public class A2
{
public static double Hypo(double a,double b,double c);
double a,b,c;
{
hyp=((a*a)+(b*b));
c=Math.sqrt(hyp);
}
int x,y;
double c;
String text1=JOptionPane.showInputDialog("How long is side A? ");
int x=Integer.parseInt(text1);
String text2=JOptionPanes.howInputDialog("How long is side B? ");
int y=Integer.parseInt(text2);
double c=A2.Hypo(x,y);
JOptionPane.showMessageDialog(null, "The hypotenuse of the triangle is " +c);
}
This code has so many problems it's hard to know where to begin. 这段代码有很多问题,很难知道从哪里开始。
Here's some advice: 这里有一些建议:
Start with this. 从此开始。 It runs and gives correct results:
它运行并给出正确的结果:
import javax.swing.*;
/**
* A2
* @author Michael
* @link https://stackoverflow.com/questions/30965862/calling-method-to-a-main-in-java
* @since 6/21/2015 11:00 AM
*/
public class SimpleMathDemo {
public static double hypotenuse(double a,double b) {
return Math.sqrt(a*a+b*b);
}
public static void main(String[] args) {
String text1= JOptionPane.showInputDialog("How long is side A? ");
int x=Integer.parseInt(text1);
String text2=JOptionPane.showInputDialog("How long is side B? ");
int y=Integer.parseInt(text2);
double c= SimpleMathDemo.hypotenuse(x,y);
JOptionPane.showMessageDialog(null, "The hypotenuse of the triangle is " +c);
}
}
Code analysis 代码分析
public class A2 {
//Missing method body no return values ..Is this an abstact function?/
public static double Hypo(double a, double b, double c);
double a, b, c;
//Whats this part doing hanging in the middle??
{
//where is the variable declaration of hyp
hyp = ((a * a) + (b * b));
c = Math.sqrt(hyp);
}
int x, y;
//variable c is already declared
double c;
String text1 = JOptionPane.showInputDialog("How long is side A? ");
//variable x is already declared
int x = Integer.parseInt(text1);
//JOptionPane not JOptionPanes
String text2 = JOptionPanes.howInputDialog("How long is side B? ");
//variable y is already declared
int y = Integer.parseInt(text2);
//variable c is already declared and Hypo function has three arguements in the declaration
double c = A2.Hypo(x, y);
//wont work because the whole code is buggy
JOptionPane.showMessageDialog (null, "The hypotenuse of the triangle is " +c);
}
}
To elaborate more: 详细说明:
import javax.swing.JOptionPane;
public class A2 {
public static double Hypo(int a, int b) {
double hyp=((a*a)+(b*b));
double c=Math.sqrt(hyp);
return c;
}
public static void main(String[] args) {
int x, y;
double c;
String text1=JOptionPane.showInputDialog("How long is side A? ");
x=Integer.parseInt(text1);
String text2=JOptionPane.showInputDialog("How long is side B? ");
y=Integer.parseInt(text2);
JOptionPane.showMessageDialog(null, "The hypotenuse of the triangle is " + Hypo(x,y));
}
}
You need to choose a correct return type, whether it be void, int, double, etc, and each method with a return type should return a value with the set type. 您需要选择一个正确的返回类型,无论它是void,int,double还是其他类型,每个具有返回类型的方法都应返回具有设置类型的值。
You also always need at least one main method in a program. 您还始终在程序中至少需要一种主要方法。 There can be multiple in different classes.
不同的类中可以有多个。
You will need to use a more specific variable names, and follow oracle convention for brackets {}. 您将需要使用更具体的变量名称,并遵循oracle约定将括号{}。
DO not declare a variable twice as in: 不要像下面那样两次声明变量:
int x, y;
int x = 1; // WRONG
x = 1; // Correct
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.