您的位置:

java输出三角形,java输出三角形汉字

本文目录一览:

用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();

}

}

}