在主函数中的调用Scanner 方法需要抛出异常或者声明异常。个人有点懒,就略过了{:soso_e113:}。程序不足之处,大家多多交流。- import java.util.Scanner;
- /*
- *@author 作者E-mail:
- *@version 创建时间:2013-10-21 下午7:43:29
- *@explain 说明:输入高度,输出对应高度的菱形,例如输入9.
- */
- class Demo10{
- public int lingXing(int n,int q,int z){
- // n=9; //更改n的值来决定菱形。
-
- //int q=2; //更改q的值来决定每行打印多少菱形
- //int z=1; //更改z的值来决定打印多少行
-
-
- for(int v=0;v<z;v++){
- int m=0;
- int t=0;
- m=n/2;
- t=n/2+2;
- for (int i = 0; i < n; i++) {
- for(int c=0;c<q;c++){
- for (int j = 1; j < n+1; j++) {
- if(i<n/2+1){
- if(j>m && j<t){
- System.out.print("*");
- }
- else{
- System.out.print("-");
- }
- }
- else {
- if(j>(-m) && (j-1)<(n-(-m))){
- System.out.print("*");
- }
- else {
- System.out.print("-");
- }
- }
- }
-
- }
- m--;t++;
- System.out.println();
- }
- //System.out.println("ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo");
- }
-
- return n;
- }
- }
- public class Example10 {
- public static void main(String[] args) {
- Scanner sc1 = new Scanner(System.in);
-
- Demo10 d = new Demo10();
-
- System.out.println("输入菱形高度:");
- int j = sc1.nextInt();
- while(j<0||j%2==0){
- if(j<0||j%2==0){
- System.out.println("输入大于0的奇数:");
- j = sc1.nextInt();
- }}
- System.out.println("输入每行菱形个数:");
- int k = sc1.nextInt();
- while(k<0){
- if(k<0){
- System.out.println("输入大于0的整数:");
- k = sc1.nextInt();
- }}
- System.out.println("输入打印行数:");
- int l = sc1.nextInt();
- while(l<0){
- if(l<0){
- System.out.println("输入大于0的整数:");
- l = sc1.nextInt();
- }}
-
-
- d.lingXing(j,k,l);
-
- }
- }
复制代码 |