Java 打印方形罐';我找不到错误
我目前正在做的工作要求我通过打印“*”作为轮廓和“.”来“绘制”一个正方形。我的代码似乎可以工作,但它仍然不接受它。我是初学者,谢谢Java 打印方形罐';我找不到错误,java,Java,我目前正在做的工作要求我通过打印“*”作为轮廓和“.”来“绘制”一个正方形。我的代码似乎可以工作,但它仍然不接受它。我是初学者,谢谢 class Main { public static void main(String args[]) { int square = 5; int line = 1; int stars = 1; int startLine = square / square; whil
class Main {
public static void main(String args[]) {
int square = 5;
int line = 1;
int stars = 1;
int startLine = square / square;
while (line <= startLine) {
while (stars <= square) { // first line prints out dots for the value of square, in this case 5
System.out.print("*");
stars = stars + 1;
}
System.out.println();
line = line + 1; //prints new line and adds 1 to line. Meaning it will move to the next section as it is now greater than startLine
}
stars = 1; //resets stars
while (line <= square - 1) {// line will keep looping until its value is greater than square - 1
while (stars <= startLine) {
System.out.print("*"); // First character to print is *
stars = stars + 1;
}
while (stars <= square - 1) {
System.out.print("."); //prints 3 dots
stars = stars + 1;
}
while (stars <= square) {
System.out.print("*"); // stars is equal to square so 1 star is printed
stars = stars + 1;
}
stars = 1;
System.out.println();
line = line + 1; // adds a value and loops around again
}
stars = 1;
while (line <= square) {
while (stars <= square) {
System.out.print("*");
stars = stars + 1;
}
System.out.println();
line = line + 1;
}
}
}
主类{
公共静态void main(字符串参数[]){
整数平方=5;
内线=1;
int星=1;
int startLine=正方形/正方形;
当(直线时,有更好的方法可以做到这一点,然后在每条直线上都有一个循环。你可以考虑的一件事是,你知道它是一个正方形,所以在你的情况下,它总是像5x5或6x6、7x7等
假设x代表正方形的宽度,y代表正方形的高度。只有当索引为*x5
,5x*
,1x*
和*x1
时,才需要星星。其他任何东西都是“
”
这里有一些伪代码
get width or height from user then set either the height or the width to the same thing that way its a square
for loop 1 to 5 //x - width
for loop 1 to 5 //y - height
if (check for *x5, 5x*, 1x* and *x1) //you can make a couple IF's if you want
print a star
else
print a dot
end if
print new line ("\n")
next
next
我希望这能有所帮助。第1步:以可读的方式格式化代码。编辑:看起来像是assylias在这个场合为您做的。您的代码似乎按照您的预期工作。您的问题是什么?您在其中面临什么问题?它正在工作..?您使用的是哪种编程结构?它是关于Java的。使用了伪代码。我不会为您解决这个问题他,因为我认为这是一个家庭作业,但只是给他指出了正确的方向,因为java中的循环和if语句与我编写的代码非常相似。