189 8069 5689

打印矩形java代码 用java打印正方形

求java 输出正方形(用*号)的代码!

public static void main(String[] args) {

创新互联公司是一家专注于网站制作、成都网站建设与策划设计,渭滨网站建设哪家好?创新互联公司做网站,专注于网站建设10年,网设计领域的专业建站公司;建站业务涵盖:渭滨等地区。渭滨做网站价格咨询:18982081108

Scanner s = new Scanner(System.in);

System.out.println("请输入正方形的大小:");

int line = s.nextInt();; // 正方形的边长

for (int i = 1; i = line; i++) { // 行

for (int j = 1; j = line; j++) { // 列

if (i == 1 || i == line) { // 如果是第一行或者是最后一行则输出星号

System.out.print("* ");

} else if (j == 1 || j == line) { // 如果是第一列或者是最后一列则输出星号

System.out.print("* ");

} else {

System.out.print(" "); // 如果不是第一行、最后一行、第一列、最后一列则输出空格

}

}

System.out.print("\n"); // 换行

}

}

用java打印一个矩形

你好,是这样吗?import java.util.Scanner;

public class Demo1 {

public static void main(String agrs[]) {

Scanner input = new Scanner(System.in);

System.out.println("请输入行数");

int count = input.nextInt();

int num[] = new int[count];

for (int i = 0; i num.length; i++) {

System.out.print("*");

}

System.out.println("");

for (int j = 0; j num.length - 2; j++) { for (int i = 0; i num.length; i++) {

if (i == 0 || i == num.length - 1) {

System.out.print("*");

} else {

System.out.print(" ");

}

}

System.out.println(" ");

}

for (int i = 0; i num.length; i++) {

System.out.print("*");

}

}

}

有JAVA编写程序 打印一个矩形!

public class RectangleDemo {

private static int M = 20;

private static int N = 18;

private int m;// 长度

private int n;// 宽度

RectangleDemo(int m, int n) {

if (m 0) this.m = m;

else this.m = M;

if (n 0) this.n = n;

else this.n = N;

}

public void print() {

for (int i = 0; i n; i++) {

if (i == 0 || i == n - 1) {

for (int j = 0; j m; j++) {

if (j == 0 || j == m - 1)

System.out.print("|");

else

System.out.print("-");

}

} else {

for (int j = 0; j m; j++) {

if (j == 0 || j == m - 1)

System.out.print("|");

else

System.out.print(" ");

}

}

System.out.println();

}

}

public static void main(String[] a) {

RectangleDemo re = new RectangleDemo(20, 8);

re.print();

}

}

结果

要想以‘*’为边 把输出改成 * 就可以了

用Java软件打印矩形,行为6行,列为3列,5列,8列?

什么意思是画出一个矩形还是用*输出一个矩形?

import java.io.*;

import java.util.Scanner;

class test

{

public static void main (String[] args)

{

System.out.println("三列矩形");

for (int i = 0; i 6; i++)

{

    printStar(3);

}

System.out.println("五列矩形");

for (int i = 0; i 6; i++)

{

    printStar(5);

}

System.out.println("八列矩形");

for (int i = 0; i 6; i++)

{

    printStar(8);

}

}

public static void printStar(int column)

{

    for(int j = 0; j column; j++)

    {

        System.out.print("* ");

    }

    System.out.println("");

}

}


当前名称:打印矩形java代码 用java打印正方形
转载源于:http://cdxtjz.cn/article/doiegpo.html

其他资讯