本文目錄一覽:
用java程序實現三角形的輸出,要求如下
class NoParamException extends Exception{
public NoParamException(String message)
{super(message);}
}
class InputDecimalException extends Exception{}
public class HOMEWORK
{
public static float getHeight(String args[])throws NoParamException,InputDecimalException
{
float m;
if(args.length==0)
throw new NoParamException(“NoParamException occures!”);
m=Float.parseFloat(args[0]);
if((int)m!=m)throw new InputDecimalException();
return m;
}
public static void main(String args[])
{
float H=0;
try{
H=getHeight(args);
}
catch(NoParamException e){
System.out.println(“NoParamException occures,please input again!”);
}
catch(InputDecimalException e){
System.out.println(“InputDecimalException occures,please input again!”);
}
catch(Exception e){
System.out.println(“NoParamException occures,please input again!”);
}
for(int i=1;i=H;i++)
{
for(int j=0;jH-i;j++)
System.out.print(” “);
for(int k=0;k2*i-1;k++)
System.out.print(“*”);
System.out.print(“\n”);
}
}
}
可以進行兩種異常控制,一種是無參數異常,一種是輸入小數的異常
這是運行過程,記得程序中的publi class名字改過來,與文件名一樣
java打印如下數字三角形?
可利用如下代碼輸出:
package print;
public class Test {
public static void main(String[] args) {
int n = 5;
int num = -1;
for(int i = 1;i n + 1;i++){
System.out.print(i);
if(i == n){
for(int j = 1;j n;j++){
System.out.print(” “);
System.out.print(n + j);
}
}else{
for(int j = i – 1;j 0;j–){
System.out.print(” “);
int end = n * 2 – 1 + n – i;
if(num == -1){
num = end;
}
if(j == 1){
System.out.print(end);
}else{
num = num + 1;
System.out.print(num);
}
}
}
/*換行*/
System.out.println();
}
}
}
輸出結果:
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();
}
}
}
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/237286.html