public?static?void?printTriangle(int?end)?{
站在用戶的角度思考問題,與客戶深入溝通,找到臺山網站設計與臺山網站推廣的解決方案,憑借多年的經驗,讓設計與互聯網技術結合,創造個性化、用戶體驗好的作品,建站類型包括:網站設計制作、成都網站設計、企業官網、英文網站、手機端網站、網站推廣、域名注冊、網站空間、企業郵箱。業務覆蓋臺山地區。
//?打印第一行
for?(int?i?=?0;?i??end?+?1;?i++)?{
System.out.print("*");
}
//?打印剩下的所有行
for?(int?i?=?1;?i??end?+?1;?i++)?{
System.out.println();//?打印一個換行
int?front?=?i;//?斜邊之前
int?behind?=?end;//?最后一個
//?front即斜邊之前都打印空格
for?(int?k?=?0;?k??front;?k++)?{
System.out.print("?");
}
//?最后一行只打印一個*號
if?(i??end)?{
System.out.print("*");
}
//?斜邊到最后之間打印空格
for?(int?k?=?front?+?1;?k??behind;?k++)?{
//?第一行倒數第三列打印直角標志
if?(i?==?1)?{
if?(k?==?behind?-?2)?{
System.out.print("*");
}?else?{
System.out.print("?");
}
}
//?第二行倒數第三列、倒數第二列打印直角標識
else?if?(i?==?2)?{
//?倒數
if?(k?==?behind?-?2?||?k?==?behind?-?1)?{
System.out.print("*");
}?else?{
System.out.print("?");
}
}?else?{
System.out.print("?");
}
}
//?打印最后一個*號
System.out.print("*");
}
}
你也可以用數組來打印
下面是打印出來的圖案
public class Test {
public static void main(String[] args) {
int count = 1;//用以輸出直三角的數字
for ( int i = 1; i 5 ; i ++){//行由四行組成
for(int j = 1; j = i ; j ++){//每行的列數小于等于當前行數.
System.out.print(count);//輸出出直角三角中的一個數字.
count++;數增一.
}
System.out.println();
}
}
}
運行結果為:
1
23
456
78910
private static Scanner sc;
private static ListInteger edge = new ArrayListInteger();
public static void main(String[] args)
{
System.out.println("請輸入三角形的三條邊");
sc = new Scanner(System.in);
input();
}
public static void input() {
while (edge.size() 3)
{
if (sc.hasNextInt())
{
int temp = sc.nextInt();
if (temp 0)
{
System.out.println("請重新輸入正整數");
edge.clear();
continue;
}else {
edge.add(temp);
Collections.sort(edge);//排序方便使用勾股定理
}
} else {
System.out.println("請重新輸入正整數");
edge.clear();
continue;
}
}
judge();
}
private static void judge() {
//三角形必須滿足任意兩條邊之和大于第三條邊
if (edge.get(0) + edge.get(1) edge.get(2) edge.get(0) + edge.get(2) edge.get(1) edge.get(1) + edge.get(2) edge.get(0))
{
//滿足三條邊相等為等邊三角形
if (edge.get(0) == edge.get(1) edge.get(1) == edge.get(2))
{
System.out.println("邊長為:" + edge.get(0) +","+edge.get(1)+","+edge.get(2) +"的三角形為等邊三角形");
}
//滿足a2+b2=c2為直角三角形,因為前面排序了所以斜邊對應的是edge.get(2)
else if (edge.get(0) * edge.get(0) + edge.get(1) * edge.get(1) == edge.get(2) * edge.get(2)) {
System.out.println("邊長為:" + edge.get(0) +","+edge.get(1)+","+edge.get(2) +"的三角形為直角三角形");
}
//滿足任意兩條邊相等即為等腰三角形
else if (edge.get(0) == edge.get(1) || edge.get(1) == edge.get(2) || edge.get(0) == edge.get(2)) {
System.out.println("邊長為:" + edge.get(0) +","+edge.get(1)+","+edge.get(2) +"的三角形為等腰三角形");
}
//其他的皆為斜角三角形
else {
System.out.println("邊長為:" + edge.get(0) +","+edge.get(1)+","+edge.get(2) +"的三角形為斜角三角形");
}
}
else
{
System.out.println("輸入的三條邊"+ edge.get(0) +","+edge.get(1)+","+edge.get(2) +"不能構成三角形,請重新輸入");
edge.clear();
input();
}
}
public?static?void?draw(int?num)?{
int?max?=?getMax(num);
StringBuffer?sb?=?new?StringBuffer();
for?(int?i?=?1;?i?=?num;?i++)?{
sb.setLength(0);
for(int?j=0;j(max-getMax(i));j++){
sb.append("?");
}
for(int?j=0;jgetMax(i);j++){
sb.append("*");
}
System.out.println(sb.toString());
}
}
public?static?int?getMax(int?num)?{
return?num?*?2?-?1;
}
package?test;
import?java.util.Scanner;
/**
*?@author?Yugi
*/
public?class?Tocket
{
public?static?void?main?(?String[]?args?)
{
System.out.println?("Hi,?guys,?input?a?number?for?height:?");
Scanner?scanner?=?new?Scanner?(System.in);
while?(scanner.hasNextInt?())
{
int?height?=?scanner.nextInt?();
for?(?int?i?=?0;?i??height;?i++?)
{
for?(?int?j?=?0;?j??height?-?i?-?1;?j++?)
{
System.out.print?("?");
}
for?(?int?j?=?0;?j?=?i;?j++?)
{
System.out.print?("*");
}
System.out.println?();
}
}
}
}
public class v1 {
public static void main(String[] args) {
int i=1,j;
while (i=10){
j=1;
do{
System.out.print("*");
j++;
}
while (j=i);
i++;
System.out.println();
}}}
網頁標題:直角三角形java代碼,java判斷三角形是不是直角三角形
文章分享:http://www.2m8n56k.cn/article40/hcecho.html
成都網站建設公司_創新互聯,為您提供Google、全網營銷推廣、網站制作、搜索引擎優化、網站排名、網站設計公司
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:[email protected]。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯