所以我是一个java newb,上周开始并且一直在读书。在一章的最后一本书中,它提出了制作一个将输出
的程序的挑战*
**
***
我试图得到它,以便它输出的行数为args(这是正确的吗?)。我遇到的问题是程序只会输出一页星号:
***************...
代码是
public class Rektifier {
public static void main(String[] args) {
int Lines = 1; // amount of lines i want
int stars = 0; //asterisk i want
int X = 0; //Counter for the asterisks for loop
while (Lines <= Integer.parseInt(args[0])){ //I think I found this on stack overflow, it is to search through the arguments for integers
Lines =+ 1; // increase by 1 for the while loop
stars =+ 1; // do this to increase by 1 for the amount of stars i want, could be any number
for (X = 0; X >= stars; X =+ 1){ // forloop, if x is less than stars it should add * to the end of the line. Then increase x by 1 to add another star until x is equal to star then it should continue
System.out.print("*");
}
System.out.println(); //creates a new line to add on too
}
}
}
答案 0 :(得分:0)
你有两个循环。 外部 while()
循环使用Lines
作为其控制变量。这个循环决定每行打印多少个 - 因此Lines
应该取值:1,然后是2,然后是3等......(就像现在一样不是因为一个小错字:所以你应该用Lines=+1
替换Lines+=1
)
您的内部 for
循环使用X
作为其控制变量。此循环实际打印Lines
变量所指示的星号数,因此您应将其更正为以下形式:
for (X = 0; X < Lines; X++)
在这种情况下,您的变量stars
不是必需的
(顺便说一句。不要用大写字母开始变量名称;这完全有效但违反传统)。
答案 1 :(得分:0)
或者您可以简单地使用for
循环。您已嵌套2次迭代。对于这种情况,For
循环更好。当执行次数取决于某些可变条件时,请使用while
。
public static void main(String[] args) throws IOException {
int lines = 3;
// lines loop
for(int i=0; i<lines; i++){
// stars loop
for(int j=0; j<=i; j++) {
System.out.print("*");
}
System.out.println();
}
}
答案 2 :(得分:0)
对于这个简单的学习示例,您可以执行字符串操作以获得所需的结果
public static void main(String[] args) {
int lineCount = Integer.parseInt(args[0]));
String stars = "";
for (int index = 0; index < lineCount; ++index) {
stars += "*";
System.out.println(stars);
}
}
这将为arg = 10生成以下输出:
*
**
***
****
*****
******
*******
********
*********
**********
每次迭代时,星星串都会在现有恒星上附加一颗星。
答案 3 :(得分:0)
这里有答案,但他们并没有指出代码中的实际错误。我将指出代码中的错误。首先是:
您需要做的是首先考虑伪代码的样子。它看起来应该是这样的。
for (line_number ... number of lines):
for(0...line_number)
print star
print newline.
这里是您的代码中的错误,其中包含注释:
public class Rektifier {
public static void main(String[] args) {
int Lines = 1;
int stars = 0; //This is not useful. The number of stars will always be equal to the number of lines. Why not just use the number of lines?
int X = 0; //Would rather that you utilize the for loop for declaring X, it is only used there.
while (Lines <= Integer.parseInt(args[0])){
Lines =+ 1; // Increment at the end of the while loop. Otherwise, you print two stars on the first line.
stars =+ 1; // Again not used.
for (X = 0; X >= stars; X =+ 1){ // your for loop is wrong; it reads like this. for x = 0 and x is bigger than stars, do blah. then you don't actually increment X because it should be X += 1 not X =+ 1.
System.out.print("*");
}
System.out.println(); //creates a new line to add on too
}
}
}
这是使用您正在使用的相同结构的代码的工作实现。
public static void main(String[] args) {
int lines = 1;
while (lines <= Integer.parseInt(args[0])){
for (int x = 0; x < lines; x += 1){
System.out.print("*");
}
lines += 1;
System.out.println();
}
}