-1

我正在制作一个勾股定理程序来解决缺失的一面,如果用户输入 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);
  }
}
4

1 回答 1

0

好的!所以我看到这个Java代码的问题是它不一定遵循实际的勾股定理。我看到 if (A==0) then 执行实际的方程,但你必须记住变量 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);

小费!请记住将变量名称中的第二个单词大写,例如 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!");


        }
    }
于 2015-12-18T00:49:49.113 回答