whileループからForループへコードを変換しようとしましたが、目的の出力が得られませんでした。whileループからコード全体をJavaのForループに変換する
Forループのコードは次のとおりです。
public static void diamond1() {
System.out.println("Diamond Height: " + DIAMOND_SIZE);
System.out.println("Output for: For Loop");
int noOfRows = DIAMOND_SIZE;
int md=noOfRows%2;
//Getting midRow of the diamond
int midRow = (noOfRows)/2;
//Printing upper half of the diamond
for (int i = noOfRows; i >= 0;i=(i-2))
{
//Printing i spaces at the beginning of each row
for (int j = 1; j <= i-md; j++) {
System.out.print(" ");
}
//Printing j *'s at the end of each row
for (int j = 1; j <= (noOfRows+1-md); j++) {
if (i-md==0 && j==midRow+1) {
System.out.print("o ");
}
else {
System.out.print("* ");
}
}
System.out.println();
}
//Printing lower half of the diamond
for (int i = 2; i <= noOfRows;i=(i+2)) {
//Printing i spaces at the beginning of each row
for (int j = 1; j <= i; j++) {
System.out.print(" ");
}
//Printing j *'s at the end of each row
for (int j=0; j <= (noOfRows); j++) {
System.out.print("* ");
}
System.out.println();
}
}
私が得た出力された:
* * *
* o *
* * * *
必要出力は次のとおりです。
*
* o *
*
私が持っていた元whileループ:
public static void diamond2() {
System.out.println("");
System.out.println("Output for: While loop");
int noOfRows = DIAMOND_SIZE;
int md=noOfRows%2;
//Getting midRow of the diamond
int midRow = (noOfRows)/2;
int i = noOfRows;
while(i >= 0){
//Printing i spaces at the beginning of each row
int j = 1;
while(j <= i-md){
if(i-md==0)break;
System.out.print(" ");
j++;
}
//Printing j *'s at the end of each row
while(j <= (noOfRows+1-md)){
if (i-md==0 && j==midRow+1) {
System.out.print("o ");
}
else {
System.out.print("* ");
}
j++;
}
System.out.println();
i=(i-2);
}
i = 2;
while(i <= noOfRows){
//Printing i spaces at the beginning of each row
int j = 1;
while(j <= i){
System.out.print(" ");
j++;
}
//Printing j *'s at the end of each row
while(j <= (noOfRows+1-md)){
System.out.print("* ");
j++;
}
System.out.println();
i=(i+2);
}
}
私がここで間違っていることを誰かが理解する手助けができますか?
デバッガがお手伝いします。 –
@AndyTurnerこれはばかげているかもしれませんが、どうすればデバッグできますか? – donk2017
元々持っていたwhileループは何ですか? –