java 求直角三角形的斜边
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30956696/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Find Hypotenuse of right triangle
提问by Madhav Vadalia
Assignment:
任务:
Write a method named Hypo, which calculates the hypotenuse of a right triangle. This method accepts two double values representing the sides of the triangle. The method Hypo calculates and displays on the screen the value of the 3rd side of the right triangle. The method main( ) should read in two double values from the user, using an input box, and then call Hypo, sending those two values as parameters to it. Hypo( ) then prints the result in an output box onto the screen. Both main( ) and Hypo( ) reside in the same class, named A2.
编写一个名为 Hypo 的方法,它计算直角三角形的斜边。此方法接受两个表示三角形边的双精度值。Hypo 方法计算并在屏幕上显示直角三角形第三边的值。方法 main() 应该使用输入框从用户那里读取两个双精度值,然后调用 Hypo,将这两个值作为参数发送给它。Hypo() 然后将输出框中的结果打印到屏幕上。main() 和 Hypo() 都位于同一个类中,名为 A2。
Here is my code. There is no error but it doesn't give me any output.can you help me out?
这是我的代码。没有错误,但它没有给我任何输出。你能帮我吗?
import java.util.Scanner;
import javax.swing.JOptionPane;
public class A2
{
public static void main(String[] args)
{
double height=Double.parseDouble(JOptionPane.showInputDialog("Enter 1st side of triangle: "));
double base=Double.parseDouble(JOptionPane.showInputDialog("Enter 2nd side of triangle: "));
RightTriangle newTriangle = new RightTriangle(height, base);
newTriangle.getHypotenuse();
double hypotenuse = newTriangle.getHypotenuse();
JOptionPane.showMessageDialog(null,hypotenuse);
}
public double height;
public double base;
public final double hypotenuse = Math.sqrt(Math.pow(height, 2) + Math.pow(base, 2));
public A2(double triHeight, double triBase)
{
height = triHeight;
base = triBase;
}
public double getHypotenuse()
{
return hypotenuse;
}
}
回答by TMR
You are calculating the hypotenuse before you're actually acceptin height and width as parameters. You have two options. One is to change your constructor to initialize hypotenuse there. the other (and my preference) is this:
在您实际接受高度和宽度作为参数之前,您正在计算斜边。你有两个选择。一种是更改构造函数以在那里初始化斜边。另一个(也是我的偏好)是这样的:
public double getHypotenuse()
{
return Math.sqrt(Math.pow(height, 2) + Math.pow(base, 2));
}
That way, you won't even need to store the hypotenuse.
这样,您甚至不需要存储斜边。
回答by Sotti
You can just use Math.hypo().
你可以只使用Math.hypo()。
回答by Kandarp
\Sawh Class CCM
import javax.swing.JOptionPane;
import javax.swing.text.*;
import java.lang.Math;
import java.text.DecimalFormat;
public class A2
{
public static void main(String[] args)
{
double base;
double height;
base = Double.parseDouble(JOptionPane.showInputDialog("Enter base: "));
height = Double.parseDouble(JOptionPane.showInputDialog("Enter height: "));
Hypo(base, height);
}
public static void Hypo(double baseInp, double heightInp)
{
double hypo;
DecimalFormat round = new DecimalFormat("0.00");
hypo = Math.sqrt((Math.pow(baseInp, 2)) + (Math.pow(heightInp, 2)));
JOptionPane.showMessageDialog(null, "Hypotenuse is: " + round.format(hypo));
}
}
回答by Null7
import java.util.Scanner;
import javax.swing.JOptionPane;
public class A2
{
public static double height;
public static double base;
public static double hypotenuse;
public static void main(String[] args)
{
height = Double.parseDouble(JOptionPane.showInputDialog("Enter 1st side of triangle: "));
base = Double.parseDouble(JOptionPane.showInputDialog("Enter 2nd side of triangle: "));
hypotenuse = Hypo(height, base);
JOptionPane.showMessageDialog(null,hypotenuse);
}
public static double Hypo(double height, double base)
{
return Math.sqrt(Math.pow(height, 2) + Math.pow(base, 2));
}
}
That may work...
那可能工作...
Couple of things: It looked like you had your class (A2) setup so that you could create an instance of it to represent the triangle that you were trying to find the hypotenuse of, but then you went and created a RightTriangle. Really, both are probably not needed, as a method (called Hypo as per your instructions) within the class A2 is probably sufficient. Additionally, you had some issues with the order of your code (ex: you were calculating the hypotenuse at the wrong point), and you had some redundant/unused code.
几件事:看起来你已经设置了你的类 (A2),这样你就可以创建它的一个实例来表示你试图找到它的斜边的三角形,但随后你创建了一个 RightTriangle。实际上,两者可能都不需要,因为 A2 类中的一种方法(根据您的说明称为 Hypo)可能就足够了。此外,您的代码顺序存在一些问题(例如:您在错误的点计算斜边),并且您有一些冗余/未使用的代码。