在 java 中调用 main 方法
calling method to a main in java
我正在编写一个程序来计算三角形的斜边,我应该在 main 中调用一个方法。
是将它们放在 2 个单独的文件中,还是使用我正在 运行 的程序中调用的方法更好?
在程序中,我不断收到有关最后一行代码的错误消息,以及 JOptionPane 输出。
我哪里错了?
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);
}
这段代码有很多问题,很难知道从哪里开始。
这里有一些建议:
- 好名字很重要。 class.
你可以而且必须比 A2 做得更好
- 学习并遵循 Sun Java 编码标准。
- 风格和可读性很重要。学习良好的代码布局并坚持下去。
从这里开始。它运行并给出正确的结果:
import javax.swing.*;
/**
* A2
* @author Michael
* @link
* @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);
}
}
代码分析
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);
}
}
详细说明:
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));
}
}
您需要选择一个正确的 return 类型,无论是 void、int、double 等,每个 return 类型的方法都应该 return设置类型。
您还总是需要在一个程序中至少有一个 main 方法。可以有多个不同的类.
您将需要使用更具体的变量名,并遵循括号 {} 的 oracle 约定。
不要像这样声明一个变量两次:
int x, y;
int x = 1; // WRONG
x = 1; // Correct
我正在编写一个程序来计算三角形的斜边,我应该在 main 中调用一个方法。
是将它们放在 2 个单独的文件中,还是使用我正在 运行 的程序中调用的方法更好?
在程序中,我不断收到有关最后一行代码的错误消息,以及 JOptionPane 输出。
我哪里错了?
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);
}
这段代码有很多问题,很难知道从哪里开始。
这里有一些建议:
- 好名字很重要。 class. 你可以而且必须比 A2 做得更好
- 学习并遵循 Sun Java 编码标准。
- 风格和可读性很重要。学习良好的代码布局并坚持下去。
从这里开始。它运行并给出正确的结果:
import javax.swing.*;
/**
* A2
* @author Michael
* @link
* @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);
}
}
代码分析
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);
}
}
详细说明:
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));
}
}
您需要选择一个正确的 return 类型,无论是 void、int、double 等,每个 return 类型的方法都应该 return设置类型。
您还总是需要在一个程序中至少有一个 main 方法。可以有多个不同的类.
您将需要使用更具体的变量名,并遵循括号 {} 的 oracle 约定。
不要像这样声明一个变量两次:
int x, y;
int x = 1; // WRONG
x = 1; // Correct