如何反转顺序?

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

How to Reverse the order?

问题

package javaapplication4;

public class NewClass7 {
   public static void main(String[] args) {
      int i = 10, a; 
      for (int j = i; j >= 1; j--) { 
         for (a = 1; a <= i; a++)
            System.out.print(a + " + ");
         int n = 0;
         for (a = 1; a <= i; a++) { 
            n = n + a;
         }
         System.out.print(" = " + n);
         System.out.println();
         i--; 
      } 
   } 
}
英文:

如何反转顺序?

How to reverse the order of the loop and sum the even number only.

So it would look like this:

10+8+6+4+2=30

10+8+6+4=28

10+8+6=24

10+8=18

10=10

My code:


package javaapplication4;

public class NewClass7 {
   public static void main(String[] args) {
  int i=10,a; 
        for(int j=i; j&gt;=1; j--) { 
            for(a=1; a&lt;=i; a++)
                System.out.print(a + &quot; + &quot;);
            int n = 0;
           for(a = 1; a&lt;=i; a++) { 
               n = n + a;
           }
           System.out.print(&quot; = &quot;+ n);
           System.out.println();
           i--; 
       } 
    } 
} 

</details>


# 答案1
**得分**: 1

```java
难道你不应该像下面展示的这样用简单的方法解决吗?

    public class Main {
        public static void main(String[] args) {
            // 定义上限和下限
            final int MAX = 10, MIN = 2;

            for (int i = MIN; i <= MAX; i += 2) {
                int sum = 0;
                for (int j = MAX; j >= i; j -= 2) {
                    sum += j;
                    // 在每次迭代中,j 每次减少 2,如果 j 的值变成 i,打印 j 的值,后跟 '=' 和 sum 的值;
                    // 否则打印 j 的值,后跟 '+'
                    System.out.print(j == i ? (j + "=" + sum) : j + "+");
                }
                System.out.println();
            }
        }
    }

**输出:**

    10+8+6+4+2=30
    10+8+6+4=28
    10+8+6=24
    10+8=18
    10=10
英文:

Shouldn't you solve it in a simple way like the one shown below?

public class Main {
	public static void main(String[] args) {
		// Define limits
		final int MAX = 10, MIN = 2;

		for (int i = MIN; i &lt;= MAX; i += 2) {
			int sum = 0;
			for (int j = MAX; j &gt;= i; j -= 2) {
				sum += j;
				// After getting decremented by 2 in each iteration, if the value of `j` has
				// become equal to that of `i`, print the value of `j` followed by &#39;=&#39; and the
				// value of `sum`; otherwise print the value of `j` followed by &#39;+&#39;
				System.out.print(j == i ? (j + &quot;=&quot; + sum) : j + &quot;+&quot;);
			}
			System.out.println();
		}
	}
}

Output:

10+8+6+4+2=30
10+8+6+4=28
10+8+6=24
10+8=18
10=10

答案2

得分: 0

如果 x 是一个(非浮点的)数字,x % 2 == 0 仅在 x 为偶数时返回 true。% 是取模运算符,基本上是:'余数':%2 的意思是:除以 2,舍弃结果,但余数是多少?对于偶数,余数总是 0,这就是为什么这样可以运行。

要倒转一个循环,好的,这已经在你的代码中:for (int j=i; j>=1; j--) {} 是一个倒转的循环,它从 j 起始于 i 的值(在那段代码中始终为 10),进行循环,然后 j 变为 9,进行循环,依此类推... 它最后一次循环是当 j 为 1 时,因为当 j 为 0 时,j>=1 不再成立。

如果你无法通过这些信息完成作业,你需要重新开始或者向助教寻求帮助。

英文:

if x is a (non-floating-point) number, x % 2 == 0 returns true only if x is even. % is the modulo operator, basically: 'remainder': %2 means: Divide by 2, throw away the result, but what is the remainder? For even numbers, the remainder is always 0, which is why that works.

to reverse a loop, well, it's already in your code: for (int j=i; j&gt;=1; j--) {} is a reversed loop, which starts with j at whatever i is (which is always 10 in that code), loops through it, then j is 9, loops through it, and it keeps going.. the last time it loops is when j is 1, as, when j is 0, j&gt;=1 is no longer true.

If you can't finish the assignment with this information, you need to start over or ask for help from your TAs.

答案3

得分: 0

我不知道我是否理解了你的问题。

据我理解,你想要一种保留结果顺序的方法,你已经实现了,并且想要知道另一种保留顺序的方法。以下是我的解决方案。

保留结果并不需要保留计算的顺序。你可以保留存储它们的顺序。例如,你可以使用堆栈(stack)来实现,就像这样:

int n = 10;
int sum = 0;
String str = "";
LinkedList<String> result = new LinkedList<>();
str = n + "";
sum += n;
result.push(str + "=" + sum);
for (int i = n - 2; i > 0; i -= 2) {
    sum += i;
    str = str + "+" + i;
    result.push(str + "=" + sum);
}
for (String res : result) {
    System.out.println(res);
}

因此,使用这种数据结构,你可以先使用结果,然后使用一个for循环来解决它。不过,仍然有一些改进的空间。例如,你可以尝试使用StringBuffer来替代String,或者你可以使用数组来替代LinkedList。逐步改进自己,这就是编程的乐趣。

英文:

I don't know whether I understand your question.

To my understand, you want a way to reserve the result order, you did it and want to know another way to reserve it. Here is my solution.

To reserve the result not need to reserve the order of compute. You can reserve the order of store them. For example, you can use the stack.like this:

int n = 10;
int sum = 0;
String str = &quot;&quot;;
LinkedList&lt;String&gt; result = new LinkedList&lt;&gt;();
str = n + &quot;&quot;;
sum += n;
result.push(str + &quot;=&quot; + sum);
for (int i = n - 2; i &gt; 0; i-=2) {
    sum += i;
    str =str + &quot;+&quot; + i;
    result.push(str + &quot;=&quot; + sum);
}
for (String res : result) {
    System.out.println(res);
}

So use the data structure, you can use the result before, and then use one for loop to solve it. However, there's still some space for improvement. For example, you can try to use StringBuffer to replace String, or you can use array to replace LinkedList. Improve ourselves step by step. That's the fun of coding.

答案4

得分: 0

使用 `for (int i = max; i >= 1; i--)` 替代 `for (int i = 1; i <= max; i++)` 进行倒数循环。

使用 `i += 2`/`i -= 2` 以步长为 2 进行计数,而不是使用 `i++`/`i--` 以 1 为步长计数。
英文:

Instead of counting up using for (int i = 1; i &lt;= max, i++), count down using for (int i = max; i &gt;= 1, i--).

Instead of counting up/down by 1 using i++/i--, count in steps of 2 using i += 2/i -= 2.

答案5

得分: 0

这是你要求的翻译内容:

我为你找到了关于你提出的问题的代码,这个问题是关于计算 5+3+1=9 等等的。请注意,这段代码只适用于奇数,对于偶数,我们需要编写一个单独的代码。希望它有帮助,不要忘记打勾。

Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int p = n;
int sum = 0;
if (n % 2 != 0)
    for (int j = 1; j <= p; j = j + 2) {
        for (int i = n; i <= p && i > 0; i = i - 2) {
            System.out.print(i);
            if (i == 1) {
                System.out.print("=");
            } else {
                System.out.print("+");
                sum += i;
            }
        }
        System.out.println(sum + 1);
        sum = 0;
        n = n - 2;
    }

(注意:代码段中的 HTML 实体编码已被还原为正常字符。)

英文:

I got the code for you for the Problem you asked me if we have to do 5+3+1=9 and so on. mind that this code will only work for odd numbers, for even numbers we would have to make a separate code. Hope it helps don't forget to tick the mark.

Scanner sc = new Scanner(System.in);
	int n = sc.nextInt();
	int p = n;
	int sum = 0;
	if (n % 2 != 0)
		for (int j = 1; j &lt;= p; j = j + 2) {
			for (int i = n; i &lt;= p &amp;&amp; i &gt; 0; i = i - 2) {
				System.out.print(i);
				if (i == 1) {
					System.out.print(&quot;=&quot;);
				} else {
					System.out.print(&quot;+&quot;);
					sum += i;
				}
			}
			System.out.println(sum + 1);
			sum = 0;
			n = n - 2;
		}

huangapple
  • 本文由 发表于 2020年10月17日 18:53:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/64401643.html
匿名

发表评论

匿名网友

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

确定