如何使用For循环反转一个整数

huangapple go评论132阅读模式
英文:

How to Reverse an Int with For Loops

问题

我在反转这段代码时遇到了问题我正在尝试实现以下内容

目前我已经有了以下代码但是我似乎无法理解第三个for循环应该是怎么样的

public static void main(String[] args) {

    Scanner input = new Scanner(System.in); //获取用户输入

    int rows;
    int number = 0;
    int i = 0;
    rows = input.nextInt(); //从控制台获取用户输入

    while (rows <= 0) {
        System.out.println("INVALID");
        rows = input.nextInt();
    }

    for (int c = 1; c <= rows; c++) {

        for (i = 0; i < c; i++) {
            System.out.print(++number + " ");
        }

        for (int j = c; j < rows; j++) {
            System.out.print("* * ");
        }

        for(i = 0; i < c; i++) {
            System.out.print(number + " ");
            //number--;
        }

        System.out.println();   
    }
}
英文:

i am having trouble with reversing this code. what i am trying to have as

this is what i have so far but i can't seem to wrap my head around how the third for loop is supposed to be

public static void main(String[] args) {

    Scanner input = new Scanner(System.in); //gets the users input

    int rows;
    int number = 0;
    int i = 0;
    rows = input.nextInt(); //takes the users input from console 

    
    while (rows &lt;= 0) {
        System.out.println(&quot;INVALID&quot;);
        rows = input.nextInt();
    }
  
    
    for (int c = 1; c &lt;= rows; c++) {
    	
            for (i = 0; i &lt; c; i++) {
            	System.out.print(++number + &quot; &quot;);
            }        
            
            for (int j = c; j &lt; rows; j++) {
            	System.out.print(&quot;* * &quot;);
            }
            
            for(i = 0; i &lt; c; i++) {
            	System.out.print(number + &quot; &quot;);
            	//number--;
            }

            System.out.println();   
    }

答案1

得分: 1

在运行最后一个循环之前,您应该将 number 存储在某个临时变量中:

int temp = number;

for(i = 0; i < c; i++) {
    System.out.print(temp-- + " ");
}
英文:

Before running your last loop you should store number in some temp variable:

int temp = number;

for(i = 0; i &lt; c; i++) {
    System.out.print(temp-- + &quot; &quot;);
}

答案2

得分: 1

正如我在评论中所说,你需要将 number 减少,但同时需要跟踪每行中的最高值,以便在下一次迭代中将其用作起始值。类似以下的代码应该可以工作:

public static void main(String[] args) {
  int rows;
  int number = 0;
  int highestValue = 0;
  int i = 0;
  rows = 5; 

  for (int c = 1; c <= rows; c++) {
      number = highestValue; // 将 number 重置为上一行中的最高值
      for (i = 0; i < c; i++) {
          System.out.print(++number + " ");
      }
      highestValue = number; // 设置本行中的最高值

      for (int j = c; j < rows; j++) {
          System.out.print("* * ");
      }

      for(i = 0; i < c; i++) {
          System.out.print(number-- + " "); // 减少 number
      }

      System.out.println();   
  }
}
英文:

As I said in the comment, you need to decrement the number but at the same time need to keep track of the highest values in a line to use it as a starting value in the next iteration. Something like this should work:

public static void main(String[] args) {
  int rows;
  int number = 0;
  int highestValue = 0;
  int i = 0;
  rows = 5; 

  for (int c = 1; c &lt;= rows; c++) {
      number = highestValue; // reset number to the highest value from previous line
      for (i = 0; i &lt; c; i++) {
          System.out.print(++number + &quot; &quot;);
      }
      highestValue = number; // setting the highest value in line

      for (int j = c; j &lt; rows; j++) {
          System.out.print(&quot;* * &quot;);
      }

      for(i = 0; i &lt; c; i++) {
          System.out.print(number-- + &quot; &quot;); // decrementing 
      }

      System.out.println();   
  }

答案3

得分: 0

你是否必须自己实现这个,否则有大量处理数组的库。

你需要执行以下步骤:

  • 找到一种方法以单行方式读取输入(整数),并将它们存储在某种容器中(可以是数组或列表)。
    • 你可能需要分隔出单个整数(例如,"1 2 3" 是 3 个整数,你希望将它们反转,而 "12 3" 只有 2 个整数,你只希望反转 2)。
    • 你需要确保你的输入有效(例如,用户可能输入 "1 a b c")。
  • 你需要翻转容器内的整数,或者更好的方法是将原始容器以相反顺序复制。为此,你只需要遍历容器的元素,并将它们以相反顺序添加到目标容器中。
for (Integer in : inputList) {
    outputList.addFirst(in);
}

如果你只想打印这些整数,无需将它们存储在列表中,你可以直接按相反顺序遍历容器。

英文:

Do you have to implement this yourself, because otherwise there are tons of libraries handling arrays.

The steps you have to take are:

  • Find a way to read the input (integers) in a single line and store them in some kind of container (either an array, or a list).
    • You may have to isolate what a single integer is (e.g. "1 2 3" is 3 integers, which you want to reverse, while "12 3" is just 2, and you only want to reverse 2).
    • You need to ensure that your input is valid (e.g. a user may enter "1 a b c")
  • You need to flip the integers within the container or better copy the original container in reverse order. For this, you only need to iterate over the container's elements and add them to the target container in reverse order
for (Integer in : inputList) {
    outputList.addFirst(in);
}

If you only want to print the integers, you don't have to store them in a list, you could just iterate over the container in reverse order.

答案4

得分: 0

以下是翻译好的代码部分:

public static void main( String[] args ) 
{
    Scanner input = new Scanner(System.in); // 获取用户输入

    int rows;
    int number = 0;
    int i = 0;

    rows = input.nextInt(); // 从控制台获取用户输入

    while (rows <= 0) {
        System.out.println("INVALID");
        rows = input.nextInt();
    }

    for (int c = 1; c <= rows; c++) {

        for (i = 0; i < c; i++) {
            System.out.print(++number + " ");
        }        

        for (int j = c; j < rows; j++) {
            System.out.print("* * ");
        }

        for(i = 0; i < c; i++) {
            System.out.print(number-- + " ");
        }

        System.out.println();   
    }
}

注意:代码中的 HTML 转义字符已经被正确处理。

英文:

It seems to bit a pattern program, you can add the number-- in you sysout

 public static void main( String[] args ) 
	{
		Scanner input = new Scanner(System.in); //gets the users input

	    int rows;
	    int number = 0;
	    int i = 0;
	    
	    rows = input.nextInt(); //takes the users input from console 


	    while (rows &lt;= 0) {
	        System.out.println(&quot;INVALID&quot;);
	        rows = input.nextInt();
	    }


	    for (int c = 1; c &lt;= rows; c++) {

	            for (i = 0; i &lt; c; i++) {
	                System.out.print(++number + &quot; &quot;);
	            }        

	            for (int j = c; j &lt; rows; j++) {
	                System.out.print(&quot;* * &quot;);
	            }

	            for(i = 0; i &lt; c; i++) {
	                System.out.print(number-- + &quot; &quot;);
	                //number--;
	            }

	            System.out.println();   
	    }
	}   

如何使用For循环反转一个整数 Such type of pattern you can create through collections

huangapple
  • 本文由 发表于 2020年5月4日 17:29:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/61589032.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定