java 打印矩形框_在JAVA中打印带符号的矩形

我一直负责打印矩形的任务。用户将输入长度,宽度和他们选择打印的符号。由此矩形将被输出。这部分任务已完成。我的下一个任务是从负值打印一个矩形,只打印矩形的轮廓(内部区域为空白)。我很难创建一个在这种情况下使用的算法。

任何帮助,将不胜感激。

干杯。

import java.util.Scanner;

public class Rectangle

{

public static void main(String[] args)

{

boolean loopExecuted;

String userContinue = "NO";

// Loop to ask to do another rectangle

do

{

// Variables

int length;

int width;

String symbol;

char symbolz;

int xLength;

int yWidth;

// Name Scanner

Scanner input = new Scanner(System.in);

// What program will do

System.out.println("This program will print a rectangle with the symbol of your choice. If you enter a positve integer the rectangle"

+ "will be filled, if you enter a negative integer, it will print the outline of the rectangle.\n");

// Ask for user input and check for validity

do

{

System.out.println("Please enter the length of the rectangle that is less than or equal to -10 and less than or equal to 10.");

length = input.nextInt();

}

while (length < -10 || length > 10 || length == 0);

do

{

System.out.println("Please enter the width of the rectangle that is less than or equal to -10 and less than or equal to 10.");

width = input.nextInt();

}

while (width < -10 || width > 10 || width == 0);

System.out.println("Please enter the symbol to be used for the rectangle");

symbol = input.next();

symbolz = symbol.charAt(0);

System.out.println("You have entered the following values for length, width, and symbol: " + length + " ," + width + " ," + symbolz +"\n");

// Algorithm to print filled in rectangle.

for (yWidth = 1; yWidth <= width; yWidth++) {

for (xLength = 1; xLength <= length; xLength++) {

System.out.print(symbolz);

}

System.out.println(" ");

}

// Algorithm to print outline of rectangle

//TODO

for(yWidth = 1; yWidth >= width; yWidth--){

for (xLength = 1; xLength >= length; xLength--){

System.out.print(symbolz);

}

System.out.println(" ");

}

// Repeat the program

loopExecuted = false;

do

{

if (!loopExecuted)

{

System.out.println("\n\nWould you like to continue? Please either Yes or No");

}

else

{

System.out.println("Please enter a valid response (Yes / No)");

}

userContinue = input.next();

userContinue = userContinue.toUpperCase();

loopExecuted = true;

}

while (!"YES".equals(userContinue) && !"NO".equals(userContinue));

// Case Insensitive

userContinue = userContinue.toUpperCase();

}

while (userContinue.equals("YES"));

}

}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值