在Java中将整个代码从while循环转换为For循环
我曾尝试将代码从while循环转换为For循环,但我没有得到所需的输出 For循环代码为:在Java中将整个代码从while循环转换为For循环,java,for-loop,while-loop,Java,For Loop,While Loop,我曾尝试将代码从while循环转换为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=noOf
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 *
*
我最初的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);
}
}
publicstaticvoiddiamond2(){
System.out.println(“”);
System.out.println(“While循环的输出”);
int noOfRows=钻石大小;
int md=noOfRows%2;
//得到钻石的中排
int midRow=(noOfRows)/2;
int i=无窗;
而(i>=0){
//打印每行开头的空格
int j=1;
(j而不是比较循环,你应该在调试时关注代码出错的地方。这可以帮助你确定问题所在,然后你可以与原始代码进行比较
对我来说,快速查看下菱形中的最后一个while循环表明您有以下内容
//Printing j *'s at the end of each row
while(j <= (noOfRows+1-md)){
//在每行末尾打印j*
虽然(j当您在while循环中使用j
时,您将其用作原始while循环的变量。在for循环中,您在嵌套for循环中使用j
,这会使j
计数减少。要修复此问题,请添加一个全局int变量,并在顶部菱形循环的末尾向其值添加2。在底部循环在第一个嵌套循环的末尾将其设置为j
+1。下面是for循环的完成代码的外观:
int count = 0;
//Printing upper half of the diamond
for (int i = noOfRows; i >= 0; i-=2) {
//Printing i spaces at the beginning of each row
for (int j = 1; j <= i-md; j++) {
if(i-md==0) {
break;
}
System.out.print(" ");
}
//Printing j *'s at the end of each row
for (int j = noOfRows - count; j <= (noOfRows+1-md); j++) {
if (i-md==0 && j==midRow+1) {
System.out.print("o ");
}
else {
System.out.print("* ");
}
}
System.out.println();
count += 2;
}
//Printing lower half of the diamond
count = 0;
for (int i = 2; i <= noOfRows; i+=2) {
//Printing i spaces at the beginning of each row
for (int j = 1; j <= i; j++) {
System.out.print(" ");
count = j+1;
}
//Printing j *'s at the end of each row
for (int j = count; j <= (noOfRows); j++) {
System.out.print("* ");
}
System.out.println();
}
int count=0;
//打印钻石的上半部分
对于(int i=noOfRows;i>=0;i-=2){
//打印每行开头的空格
for(int j=1;j我认为仅仅将while循环更改为for循环并不是我们想要的。无论如何,有一种直接的方法可以实现您想要的
Java中的for循环有三个部分:
- 初始值
- 每次迭代前的条件
- 每次迭代后的操作
我们使用它在三个步骤中将while
转换为for
在第一步中,将所有while
循环替换为只填充条件的for
循环。例如,for(;j)调试器将在这里帮助您。@AndyTurner我知道这听起来可能很傻,但我如何调试它?您最初使用的while循环是什么?@donk2017 Use System.out.println()例如,检查您正在使用的IDE的教程。@DaveK。我已经编辑了OP以包含原始的while循环。我也尝试过调试,但无法找出哪里出了问题。
//Printing j *'s at the end of each row
for (int j=0; j <= (noOfRows); j++) {
int count = 0;
//Printing upper half of the diamond
for (int i = noOfRows; i >= 0; i-=2) {
//Printing i spaces at the beginning of each row
for (int j = 1; j <= i-md; j++) {
if(i-md==0) {
break;
}
System.out.print(" ");
}
//Printing j *'s at the end of each row
for (int j = noOfRows - count; j <= (noOfRows+1-md); j++) {
if (i-md==0 && j==midRow+1) {
System.out.print("o ");
}
else {
System.out.print("* ");
}
}
System.out.println();
count += 2;
}
//Printing lower half of the diamond
count = 0;
for (int i = 2; i <= noOfRows; i+=2) {
//Printing i spaces at the beginning of each row
for (int j = 1; j <= i; j++) {
System.out.print(" ");
count = j+1;
}
//Printing j *'s at the end of each row
for (int j = count; j <= (noOfRows); j++) {
System.out.print("* ");
}
System.out.println();
}
public static void diamond2() {
System.out.println("Solved by Stackoverflow.com. Have fun with it.");
System.out.println("Output for: For loops");
int noOfRows = DIAMOND_SIZE;
int md = noOfRows % 2;
// Getting midRow of the diamond
int midRow = (noOfRows) / 2;
int i = noOfRows;
for (; i >= 0; i = (i - 2)) {
// Printing i spaces at the beginning of each row
int j = 1;
for (; j <= i - md; j++) {
if (i - md == 0)
break;
System.out.print(" ");
}
// Printing j *'s at the end of each row
for (; j <= (noOfRows + 1 - md); j++) {
if (i - md == 0 && j == midRow + 1) {
System.out.print("o ");
} else {
System.out.print("* ");
}
}
System.out.println();
}
for (i = 2; i <= noOfRows; i = (i + 2)) {
// Printing i spaces at the beginning of each row
int j = 1;
for (; j <= i; j++) {
System.out.print(" ");
}
// Printing j *'s at the end of each row
for (; j <= (noOfRows + 1 - md); j++) {
System.out.print("* ");
j++;
}
System.out.println();
}
}