T001_UT001_0012

来源:互联网 发布:eviews软件中文版下载 编辑:程序博客网 时间:2024/05/19 21:42

编写一个程序从标准输入一个大于0的整数n,并打印输出在标准输出设备上打印如下信息:

1
2
3
4
5
6
7
8
input n:
5
=====
*
**
***
****
*****

1
2
3
4
5
6
input n:
3
===
*
**
***
如果输入的内容不符合要求,则提示“Error Input”,程序退出。如下所示:

1
2
3
input n:
0
Error Input
1
2
3
input n:
-1
Error Input
1
2
3
input n:
abc
Error Input

注意:

  1. 最后没有换行。
  2. "input n:"后有一个换行
  3. 输入内容后存在一个换行

import java.util.Scanner;public class T001_UT001_0012{public static void main(String[] args){        Scanner njp_input = new Scanner(System.in);try{        System.out.println("input n:");        int njp_n=njp_input.nextInt();            if ( njp_n <= 0)                System.out.print("Error Input");            else{for(int i=1;i<=njp_n;i++) System.out.print("=");System.out.println();for(int i=1;i<=njp_n-1;i++)            {            for(int j=1;j<=i;j++) System.out.print("*");            System.out.println();            }for(int j=1;j<=njp_n;j++) System.out.print("*");}}catch(Exception e){System.out.print("Error Input");}}}


0 0