
正文
java代码测试三角形 用java编写三角形
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
Java语言杨辉三角
打印杨辉三角代码如下:
public class woo {
public static void triangle(int n) {
int[][] array = new int[n][n];//三角形数组
for(int i=0;iarray.length;i++){
for(int j=0;j=i;j++){
if(j==0||j==i){
array[i][j]=1;
}else{
array[i][j] = array[i-1][j-1]+array[i-1][j];
}
System.out.print(array[i][j]+"\t");
}
System.out.println();
}
}
public static void main(String args[]) {
triangle(9);
}
}
扩展资料:
杨辉三角起源于中国,在欧洲这个表叫做帕斯卡三角形。帕斯卡(1623----1662)是在1654年发现这一规律的,比杨辉要迟393年。它把二项式系数图形化,把组合数内在的一些代数性质直观地从图形中体现出来,是一种离散型的数与形的优美结合。
杨辉三角具有以下性质:
1、最外层的数字始终是1;
2、第二层是自然数列;
3、第三层是三角数列;
4、角数列相邻数字相加可得方数数列。
相关问答
Q1: java初学者,判断是否为三角形,代码如下,提示我缺少类
class Triangle {
int a;
int b;
int c;
public Triangle() {
}
public Triangle(int a, int b, int c) { // 把无关的语句去掉
this.a = a;
this.b = b;
this.c = c;
}
public void isTriangle() { // 用这个方法判断是不是三角形
if ((this.a + this.b this.c) (this.a + this.c this.b)
(this.b + this.c this.a)) {
System.out.println("我是一个三角形");
} else
System.out.println("我不是一个三角形");
}
}
public class Test {
public static void main(String[] args) {
Triangle t = new Triangle(2, 3, 1);
t.isTriangle();
}
}
Q2: java代码关于三角形用junit测试
@Test
public void test() {
SetString typeSet = new HashSetString();
typeSet.add("Equilateral");
typeSet.add("Isosceles");
typeSet.add("Scalene");
Triangle t1 = new Triangle("a", "b", "c");
String type1 = t1.determineTriangleType();
if(typeSet.contains(type1)){
fail("error of type");
}
Triangle t2 = new Triangle("1", "1", "1");
String type2 = t2.determineTriangleType();
if(!typeSet.contains(type2)){
fail("error of type");
}
assertEquals("Isosceles", type2);
}
随便写了两个分支的测试,你自己可以补全,第二个分支已经测试报错了,你可以检查下代码:
if ((s1 == s3) (s2 == s3)) {
type= "Equilateral";
} else if (( s1 == s2) (s2 == s3) (s1 == s3)) {
type = "Isosceles";
} else {
type = "Scalene";
}
第一个条件需要和第二个条件换一下位置,首先判断等边再判断等腰
Q3: 如何用java代码来做三角形的判断?
public class Test7 {
public static String T(double a,double b,double c){
double tem = Math.max(a, b);
if(temc){
if(tem==a){
a = c;
}else {
b = c;
}
c = tem;
}
if(!(a+bcMath.abs(a-b)c)){
return "无法构成三角形";
}else if(a==b||a==c||b==c){
return "等腰三角形";
}else if(a*a+b*b==c*c){
return "直角三角形";
}else if(a*a+b*bc*c){
return "锐角三角形";
}else return "钝角三角形";
}
public static void main(String[] args) {
System.out.println(Test7.T(11, 5, 12));
}
}
Q4: (Java)判断三角形的形状
if ("true".equals(show.isTriangle(a, b, c))) {
改成
if (show.isTriangle(a, b, c)) {
if ((a + b c) (a + c b) (b + c a)) {
改成
if (((a + b) c) ((a + c) b) ((b + c) a)) {
java代码测试三角形你再试试
Q5: JAVA编程:根据用户输入的三角形的三个边长,判断三角形是不是直角三角形?
package abc;import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;public class Triangle {
public static void main(String[] args) {
double a=getSide();
double b=getSide();
double c=getSide();//获得三角形java代码测试三角形的三个边长
ListDouble list=new ArrayListDouble();//下面是给三个边排序java代码测试三角形,按冲小到大
list.add(a);
list.add(b);
list.add(c);
Collections.sort(list);
a=list.get(0);//获得排序好了的数字
b=list.get(1);
c=list.get(2);
if(a*a+b*b==c*c){//判断这个是不是直角三角形
System.out.println("a="+a+" b="+b+" c="+c+" 这是直角三角形");
}else
System.out.println("a="+a+" b="+b+" c="+c+" 这不是直角三角形 ");
}
public static double getSide(){//获得一个边长
InputStreamReader reader=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(reader);//控制台获得一个字符串创建流
System.out.println("输入一个整数:");
double d=0;//表示返回值
try {
String str=br.readLine();//控制台获得一个字符串
d=Double.parseDouble(str.trim());//把字符串转换成double类型的
} catch (Exception e) {
System.out.println("没有输入数字");
System.exit(1);//没有输入正确的数字强制退出
}
return d;
}
} 希望对你有帮助
关于java代码测试三角形和用java编写三角形的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。






