好的,我有一个项目要做,我需要一些帮助。我只能使用While语句,(不是),我需要创建一个星号框,因此用户可以输入框的宽度和长度,如何制作框?因为我完成了顶线和底线,但我不知道如何做中线,所以两边都是a *,中间是空格。以下是我到目前为止的情况,请记住这只是我用Java编写的第4个月。
import java.util.Scanner;
public class Client {
private static int width, length = 0;
//Program 2
public static void input(){
Scanner console = new Scanner(System.in);
System.out.print("What is the width? ");
width = console.nextInt();
System.out.print("What is the length? ");
length = console.nextInt();
}
public static void topBottom(int width){
while(width >= 1){
System.out.print("x");
width--;
}
}
public static void length(int length){ //this is mainly where i need help
int num = length - 2; //num is for the spaces in between the two * in the middle
while(length >= 3){ //I have no idea how to create the box effect
System.out.println("x");
while(num >= 0){
System.out.print(" ");
num--;
}
length--;
}
}
public static void main(String[] args) {
input();
topBottom(width);
System.out.println("");
length(length);
topBottom(width);
}
}
答案 0 :(得分:0)
你必须在length()
做同样的事情,即做一个减去2的循环,但对于每一行:
public static void width(int width){
int num = width - 2;
System.out.print("x"); // Print the leftmost 'x'
while(num > 0){ // Repeat for each column
System.out.print(" "); // Print a space
num--;
}
System.out.println("x"); // Print the rightmost 'x' and change line
}
public static void main(String[] args) {
input();
topBottom(width); // Print header
System.out.println();
int rows = length - 2;
while (rows > 0) { // Repeat for each of the rows, minus 2 (top and bottom)
width(width);
rows--;
}
topBottom(width); // Print footer
System.out.println();
}
您还将宽度和长度概念混淆了。 假设您总是想要打印第一行和最后一行,即打印小于2x2的盒子是没有意义的。
此外,请注意,由于已经存储了width
和length
,因此无需通过。{/ p>
答案 1 :(得分:0)
如何在不知道宽度的情况下计划长度尝试此
public static void length(int length,int width){ //this is mainly where i need help
int num = length - 2; //num is for the spaces in between the two * in the middle
int num2= width-2;
while(num >= 0){ //I have no idea how to create the box effect
System.out.print("x");
while(num2 >= 0){
System.out.print(" ");
num2--;
}
System.out.print("x");
System.out.println();
num2=width-2;
num--;
}
}