我正在制作毕达哥拉斯定理程序以解决缺失的一方,如果用户输入0作为值,则意味着这是缺少的一方需要解决。我的程序没有得到正确的答案。非常感谢您的帮助。
import java.util.Scanner;
import java.lang.Math;
public class pythagTheorem {
static double a;
static double b;
static double c;
static double newa;
static double newb;
static double newc;
public static void main(String[] args) {
Scanner scan= new Scanner(System.in);
System.out.println("Enter the value of a");
a=scan.nextDouble();
scan.nextLine();
System.out.println("Enter the value of b");
b=scan.nextDouble();
scan.nextLine();
System.out.println("Enter the value of c");
c=scan.nextDouble();
scan.nextLine();
if(a==0)
{
newb=Math.pow(b, b);
newc=Math.pow(c, c);
double result=newc-newb;
newa=Math.sqrt(result);
}
System.out.println("The value of a is " + newa);
}
}
答案 0 :(得分:0)
好!所以我用这个Java代码看到的问题是它不一定遵循实际的毕达哥拉斯定理。我看到如果(A == 0)然后执行实际的等式,但你必须记住变量A不是三角形的斜边!
A ^ 2 + B ^ 2 = C ^ 2(C是斜边)如果你想要它找到三角形的任何部分你必须记住导出方程式,这样你就可以找到缺失的一面!例如,你有A面和C面,但你想找到B面,那么该等式就是B ^ 2 = C ^ 2-A ^ 2。
最后,我看到你有newb = Math.pow(b,b);这将为你自己的变量提供动力!即使是权力只有2,所以它将是newb = Math.pow(b,2);
TIP!请记住将变量名称中的第二个单词(如numa)大写为numA
/* REMEMBER THAT A NUMBER MUST NEVER BE GREAT THAN C UNLESS IT IS THE UNKNOWN OR YOU WILL ENCOUNTER A NONREAL NUMBER BECAUSE YOU CAN'T SQUARE ROOT A NEGATIVE NUMBER*/
import java.util.Scanner;
import java.lang.Math;
public class pythagTheorem {
static double a;
static double b;
static double c;
static double newa;
static double newb;
static double newc;
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter a ZERO for the unknown side!");
System.out.println("Enter the value of side A :: ");
a = scan.nextDouble();
System.out.println("Enter the value of B :: ");
b = scan.nextDouble();
System.out.println("Enter the value of C :: ");
c = scan.nextDouble();
if (a == 0) {
newc = Math.pow(c, 2);
newb = Math.pow(b, 2);
newa = Math.sqrt(newc - newb);
System.out.println("Your missing side is :: " + newa);
}
else if(b == 0){
newc = Math.pow(c, 2);
newa = Math.pow(a, 2);
newb = Math.sqrt(newc - newa);
System.out.println("Your missing side is :: " + newb);
}
else if(c == 0){
newa = Math.pow(a, 2);
newb = Math.pow(b, 2);
newc = Math.sqrt(newa + newb);
System.out.println("Your missing side is :: " + newc);
}
else
System.out.println("Sorry! There is an error!");
}
}