java空心矩形代码 java空心正方形

JAVA编程:编写一个applet,读取一个矩形的边长,然后输入一个空心矩形import java.awt.*;
import javax.swing.*;
public class drawRech extends JApplet {
public void paint(Graphics g){
super.paint(g);
this.setBackground(Color.WHITE);
String width = JOptionPane.showInputDialog("Enter the width");
String height = JOptionPane.showInputDialog("Enter the height");
int wid = 0;
int heig = 0;
try{
wid = Integer.parseInt(width);
heig = Integer.parseInt(height);
}
catch(NumberFormatException e){
JOptionPane.showMessageDialog(null, "Wrong input");
}
g.drawRect(100, 100, wid, heig);
}
}
已添加异常处理,测试成功
画空心矩形的代码是什么?要java的 。以下是代码java空心矩形代码,可以参考一下java空心矩形代码,希望对您有帮助 。
============================================
public class PrintStars {
private static final int LENGTH=10;
private static final int WIDTH=20;
public static void main(String[] args) {
for (int i = 0; iLENGTH; i) {
if(i==0||i==LENGTH-1){
for (int j = 0; jWIDTH; j) {
System.out.print("* ");
}
}else {
for (int j = 0; jWIDTH; j) {
if(j==0||j==WIDTH-1){
System.out.print("* ");
}else {
System.out.print("");
}
}
}
System.out.println();
}
}
}
java二维数组打印空心矩形String ss[][]={
{"*", "*", "*", "*", "*", "*", "*"},
{"*", " ", " ", " ", " ", " ", " ", " ", " ", " ", "*", "},
{"*", " ", " ", " ", " ", " ", " ", " ", " ", " ", "*", "},
{"*", " ", " ", " ", " ", " ", " ", " ", " ", " ", "*", "}
};
for(int i=0; iss.length; i){
for(int j=0; jss[i].length; j){
System.out.print(ss[i][j]);
}
System.out.println();
}
////////////////////////////////////////////////////////Second
private static void test1( ) {
;
//调用方法传入数组以及行数列数
String[][] strArr=getArray(20,8);
for(int i=0;istrArr.length;i){
for(int j=0;jstrArr[i].length;j){
System.out.print(strArr[i][j]);
}
System.out.println();
}
}
static String[][] getArray(int k, int l) {
String[][] strArr=new String[k][l];
for(int i=0;ik;i){
for(int j=0;jl;j){
if( (i==0 || i==k-1) || (j==0 || j==l-1) ){
strArr[i][j]="*";
}else{
strArr[i][j]=" ";
}
}
}
return strArr;
}
JAVA输出n行n列的空心矩形(要求使用嵌套循环),当n=5时public class RectangleDemo{
public static void main(String[] args){
for (int i=1;i=5 ;i){//定义行数为5
for (int j=1;j=5 ;j){//定义列数为5
if (i==1||j==1||i==5||j==5){//如果符合条件为行数为1或5,列数为1或5的元素就打印*
System.out.print("* ");
}
else{
System.out.print("");//否则就打印空格
}
}
System.out.println();
}
}
}
【java空心矩形代码 java空心正方形】java空心矩形代码的介绍就聊到这里吧 , 感谢你花时间阅读本站内容,更多关于java空心正方形、java空心矩形代码的信息别忘了在本站进行查找喔 。

    推荐阅读