Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何仅使用While循环制作长方体?_Java_Loops_While Loop - Fatal编程技术网

Java 如何仅使用While循环制作长方体?

Java 如何仅使用While循环制作长方体?,java,loops,while-loop,Java,Loops,While Loop,好的,我有一个项目要做,我需要一些帮助。我只能使用While语句,(而不是for),我需要创建一个星号框,这样用户就可以输入框的宽度和长度,如何制作框?因为我得到了顶部和底线完成,但我不知道如何做中间线,所以在它的两边和空间的A *。这是我到目前为止所做的,请记住这只是我用Java编写代码的第4个月 import java.util.Scanner; public class Client { private static int width, length = 0; //Pr

好的,我有一个项目要做,我需要一些帮助。我只能使用While语句,(而不是for),我需要创建一个星号框,这样用户就可以输入框的宽度和长度,如何制作框?因为我得到了顶部和底线完成,但我不知道如何做中间线,所以在它的两边和空间的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);
    }

}

您必须执行与在
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的框是没有意义的


另外,请注意,无需传递
宽度
长度
,因为它已经存储。

在不知道宽度的情况下,您打算如何计算长度试试这个

 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--;
    }
}

对不起,我不认为我清楚地指明了它,我需要在盒子的中间有空格,不只是说一句对不起的话,但我认为这是缺少一些东西…它将运行无限循环。@joejoethemonkey没有无限循环,
num
在外循环的每次迭代中递减,
num2
在内循环的每次迭代中,循环都将完成。