[英]calling method to a main in java
我正在編寫一個計算三角形斜邊的程序,並且應該在主函數中調用一個方法。
最好將它們放在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);
}
這段代碼有很多問題,很難知道從哪里開始。
這里有一些建議:
從此開始。 它運行並給出正確的結果:
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);
}
}
代碼分析
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));
}
}
您需要選擇一個正確的返回類型,無論它是void,int,double還是其他類型,每個具有返回類型的方法都應返回具有設置類型的值。
您還始終在程序中至少需要一種主要方法。 不同的類中可以有多個。
您將需要使用更具體的變量名稱,並遵循oracle約定將括號{}。
不要像下面那樣兩次聲明變量:
int x, y;
int x = 1; // WRONG
x = 1; // Correct
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.