编写程序以查找三角形的长度C.

时间:2014-03-17 03:45:29

标签: java eclipse

我正在尝试编写一个程序,找到一个直角三角形的C面,考虑到A和B边的长度。这是我到目前为止所做的:

/* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Math
{
    public static void main (String[] args) throws java.lang.Exception
    {
        // Scanner scan = new scanner(system.in);
        int a, b, c;
        int length c; c = sqrt(a^(2)+b^(2))

        System.out.println("Length of side a?");
        a = scan.nextdouble();

        System.out.println("Length of side b?");
        b = scan.nextdouble();

        c = math.sqrt(a^(2)+b^(2));

        System.out.print("The length of side C is");
        System.out.println(c+ "units.");

        scan.close();

    }
}
Eclipse告诉我,我有一个参数错误,但我不知道该怀疑什么。有关修复此问题的建议吗?我知道这很简单,我觉得解决方案就在我面前。

5 个答案:

答案 0 :(得分:1)

将此int length c; c = sqrt(a^(2)+b^(2))更改为int length;

答案 1 :(得分:1)

^符号与XOR相同。它并不意味着“以权力为中心”。您需要使用

Math.pow(a, 2) + Math.pow(b, 2);

a*a + b*b

答案 2 :(得分:1)

有很多错误......这是工作版本:

import java.util.*;
import java.lang.*;
import java.io.*;

class Math
{
    public static void main (String[] args) throws java.lang.Exception {
        Scanner scan = new Scanner(System.in); // this had errors
        double a, b, c; // doubles are better for division, 
                        // unless you want imprecise results

        System.out.println("Length of side a?");
        a = scan.nextDouble(); // note the case in the method name, this was wrong

        System.out.println("Length of side b?");
        b = scan.nextDouble(); // same here

        c = java.lang.Math.sqrt(java.lang.Math.pow(a,2) + java.lang.Math.pow(b,2));

        System.out.print("The length of side C is ");
        System.out.println(c + "units.");

        scan.close();
    }
}

由于您将类命名为Math,因此必须使用sqrt和pow的完整java.lang.Math前缀。

答案 3 :(得分:0)

删除int length c; 第一个长度未使用, c 已经宣布。

答案 4 :(得分:0)

我不知道你是怎么设法犯这么多错误的。

这是一个有效的代码

import java.util.Scanner;


public class Lame {

    /**
     * @param args
     */
    public static void main(String[] args) {
        double a, b, c;

        Scanner scan = new Scanner(System.in);

        System.out.println("Length of side a?");
        a = scan.nextDouble();

        System.out.println("Length of side b?");
        b = scan.nextDouble();

        c = Math.sqrt((a*a + b*b));

        System.out.print("The length of side C is");
        System.out.println(c+ "units.");
    }

}

一些提示。注意你的资本化。 Java区分大小写。 扫描仪和扫描仪在java中不是一回事! (类是大写的,而函数不是)

Java不会自动转换类型 你不能设置一个= scan.nextDouble():因为它将返回一个double而不是一个int。 (如果你想把一个double加到int中你必须自己做)

Java中没有电源符号^。您需要使用pow()函数或者只需要像* a。

那样执行变量

从Math更改您的班级名称。

尝试谷歌的东西,并从书中做很多练习只是处理代码。过了一会儿,你会开始理解事情。