本文目錄一覽:
- 1、java怎麼打出三角形
- 2、Java 如何通過選擇三角形類型,輸出圖形?
- 3、java 編程 三角形
- 4、用java程序寫一個三角形
- 5、java 如何判斷能否構成三角形?
- 6、java判斷三角形類型
java怎麼打出三角形
用Java代碼中的for循環可以列印出各種三角形,便於熟悉for循環的特性,即外環循環執行一次,內循環執行N次。
工具:
電腦軟體
for循環
三角形
方法:
1、列印定點在左下的直角三角形;
2、列印定點在左上的直角三角形;
3、列印定點在右下的直角三角形,這裡三角形的形狀是由*所佔的位置決定的;
4、列印定點在左下的直角三角形,這裡三角形的形狀是由*所佔的位置決定的;
5、列印定點在正上方的直角三角形;
6、列印定點在正下方的直角三角形。
Java 如何通過選擇三角形類型,輸出圖形?
package Test1;
import java.util.Scanner;
public class B {
static void dengyao(int i) {
for (int j = 0; j i; j++) {
for (int i1 = 0; i1 j; i1++) {
System.out.print(“* “);
}
System.out.println(“* “);
}
for (int j = 0; j i – 1; j++) {
for (int i1 = i – 2; i1 j; i1–) {
System.out.print(“* “);
}
System.out.println(“* “);
}
}
static void dengbian(int i) {
for (int i2 = 0; i2 i; i2++) {
for (int i1 = i; i1 i2; i1–) {
System.out.print(” “);
}
for (int i1 = 0; i1 i2; i1++) {
System.out.print(“* “);
}
System.out.println(“* “);
}
}
static void dengyaozhijiao(int i) {
for (int i2 = 0; i2 i; i2++) {
for (int i1 = 0; i1 i2; i1++) {
System.out.print(“* “);
}
System.out.println(“* “);
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println(“請輸入要列印的三角形”);
String s = scanner.nextLine();
int i;
while (true) {
System.out.println(“請輸入要列印的行數”);
try {
i = scanner.nextInt();
} catch (Exception e) {
System.out.println(“輸入錯誤,請重新輸入”);
i = scanner.nextInt();
continue;
}
if (s.equals(“等腰三角形”)) {
dengyao(i);
} else if (s.equals(“等腰直角三角形”)) {
dengyaozhijiao(i);
} else if (s.equals(“等邊三角形”)) {
dengbian(i);
} else if (s.equals(“退出”)) {
scanner.close();
return;
} else {
System.out.println(“輸入錯誤,請重新輸入”);
s = scanner.nextLine();
continue;
}
System.out.println(“列印完畢,是否繼續輸入,輸入要列印的三角形以繼續輸入”);
System.out.println(“輸入\”退出\”退出”);
s = scanner.nextLine();
}
}
}
java 編程 三角形
//java編程:輸入三角形的三邊,並輸出,同時判斷這三邊能否構成三角形,
public class Triangle2
{
private double sideA,sideB,sideC;//外部不能改變這些變數的值,只能在類中使用方法來修改和獲得這些變數的值
public void setSide(double sideA,double sideB,double sideC)
{
this.sideA=sideA;//成員變數被局部變數隱藏,需要使用this關鍵字使用被隱藏的成員變數
this.sideB=sideB;
this.sideC=sideC;
}
public double getSideA()
{
return sideA;
}
public double getSideB()
{
return sideB;
}
public double getSideC()
{
return sideC;
}
public boolean isOrNotTrangle()//判斷三邊能否構成三角形
{
if(sideA+sideBsideCsideA+sideCsideBsideB+sideCsideA)
{
return true;
}
else
{
return false;
}
}
}
class Example1
{
public static void main(String args[])
{
double sideA,sideB,sideC;
Triangle2 triangle=new Triangle2();
triangle.setSide(7.2,8.3,9.6);
sideA=triangle.getSideA();
sideB=triangle.getSideB();
sideC=triangle.getSideC();
System.out.println(“輸入的三角形的三邊為:”+sideA+” “+sideB+” “+sideC);
boolean isOrNotTrangle=triangle.isOrNotTrangle();
if(isOrNotTrangle==true)
{
System.out.println(“這三邊可以構成三角形”);
}
else
{
System.out.println(“這三邊不可以構成三角形”);
}
}
}
用java程序寫一個三角形
public class DaoSanJiao { // 定義一個倒三角的類,有主方法和 輸出倒等腰三角形方法
public static void main(String[] args) { // 定義主方法,程序從這裡開始
printDengyao(10); // 調用輸出倒三角形的方法,*數為10, 即高度(層)也為10
// 10可以換成任何整型值
}
public static void printDengyao(int x) { // 定義一個輸出倒三角的方法
for (int i = 0; i x; i++) { // 要輸出的整體(全部多少行)用這個for循環控制
System.out.println(); // 輸出一行*後跳到下一行
for (int j = 0; j i + 1; j++) { // 這個循環用來輸出空格,以達到輸出倒等腰三角形的效果
System.out.print(” “);
}
for (int j = i; j x; j++) { // 這個循環用來輸出*,他的數目有傳入的參數x決定
System.out.print(“* “); // 如:i=0時即第一行,輸出x個「*」
}
}
}
}
java 如何判斷能否構成三角形?
import java.io.*;
class sanj{
public static int a,b,c;
public static void main(String arg[]) throws IOException{
try{
BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in)); //接收鍵值
System.out.println(“輸入三邊值,每個值輸入後回車”);
System.out.println(“請輸入:”);
a=Integer.valueOf(stdin.readLine());
b=Integer.valueOf(stdin.readLine());
c=Integer.valueOf(stdin.readLine());
}catch(IOException e){
System.out.println(“出現異常!”);
System.exit(0);
}
if(a+bc || a+cb || b+ca){
System.out.println(“你輸入的不能構成三角形”);
}
if(a==b || a==c || b==c)
{
if(a==b b==c) System.out.println(“等邊三角形”);
else System.out.println(“等腰三角形”);
}
if(a^2+b^2==c^2||a^2+c^2==b^2||b^2+c^2==z^2)
{
System.out.println(“直角三角形”);
if(a^2+b^2==c^2) {
System.out.println(“直角三角形面積”+a*b/2);
}
if(a^2+c^2==b^2) {
System.out.println(“直角三角形面積”+a*c/2);
}
if(b^2+c^2==z^2) {
System.out.println(“直角三角形面積”+c*b/2);
}
}else
System.out.println(“一般三角形”);
System.out.println(「y一般三角形周長是「+(a+b+c));}
}
java判斷三角形類型
package demo;
import java.util.Scanner;
public class Triangle
{
/**
* 等腰
*/
public static final int ISOSCELES = 1;
/**
* 等邊
*/
public static final int EQUILATERAL = 2;
/**
* 直角
*/
public static final int RIGHTANGLE = 4;
/**
* 普通
*/
public static final int GENERAL = 6;
/**
* 不構成三角形
*/
public static final int NOTHING = 7;
double a;
double b;
double c;
public Triangle ( double a, double b, double c )
{
this.a = a;
this.b = b;
this.c = c;
}
public static int isWhichOne ( Triangle triangle )
{
double a = triangle.a;
double b = triangle.b;
double c = triangle.c;
if (a + b c a + c b b + c a Math.abs (a – b) c Math.abs (a – c) b Math.abs (b – c) a)
{
if (a == b a == c)
{
return Triangle.EQUILATERAL | Triangle.ISOSCELES;
}
else if (a * a + b * b == c * c || a * a + c * c == b * b || c * c + b * b == a * a)
{
if (a == b || a == c || b == c)
{
return Triangle.RIGHTANGLE | Triangle.ISOSCELES;
}
return Triangle.RIGHTANGLE;
}
else if (a == b || a == c || b == c)
{
return Triangle.ISOSCELES;
}
else
{
return Triangle.GENERAL;
}
}
else
{
return Triangle.NOTHING;
}
}
public static double perimeter ( Triangle triangle )
{
return triangle.a + triangle.b + triangle.c;
}
public static double area ( Triangle triangle )
{
double a = triangle.a;
double b = triangle.b;
double c = triangle.c;
return 1.d / 4 * Math.sqrt (( a + b + c ) * ( a + b – c ) * ( a + c – b ) * ( b + c – a ));
}
public static void translate ( Triangle triangle )
{
int type = isWhichOne (triangle);
switch (type)
{
case Triangle.ISOSCELES:
System.out.println (“等腰三角形”);
break;
case ( Triangle.EQUILATERAL | Triangle.ISOSCELES ):
System.out.println (“等邊三角形 + 等腰三角形”);
break;
case Triangle.GENERAL:
System.out.println (“普通三角形”);
break;
case ( Triangle.RIGHTANGLE | Triangle.ISOSCELES ):
System.out.println (“直角三角形 + 等腰三角形”);
break;
case Triangle.RIGHTANGLE:
System.out.println (“直角三角形”);
break;
default:
System.out.println (“不構成三角形”);
break;
}
if (type != Triangle.NOTHING)
{
System.out.println (“周長是:” + perimeter (triangle));
System.out.println (“面積是:” + area (triangle));
}
}
public static void main ( String[] args )
{
System.out.println (“輸入構成三角形的三邊長:(空格隔開)\n退出按 (n/N), 否則繼續: “);
String error = “輸入有誤,請重新輸入: “;
String reg = “^\\d+\\s+\\d+\\s+\\d+$”;
Scanner scanner = new Scanner (System.in);
while (scanner.hasNextLine ())
{
String line = scanner.nextLine ().trim ();
if (“n”.equalsIgnoreCase (line))
{
break;
}
if (!line.matches (reg))
{
System.err.println (error);
}
else
{
try
{
String[] input = line.split (“\\s+”);
int a = Integer.parseInt (input[0]);
int b = Integer.parseInt (input[1]);
int c = Integer.parseInt (input[2]);
Triangle triangle = new Triangle (a, b, c);
translate (triangle);
}
catch (NumberFormatException e)
{
System.err.println (error);
}
System.out.println (“退出按 (n/N), 否則繼續: “);
}
}
scanner.close ();
}
}
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/300518.html