从for循环中打印出数字

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

Print out the numbers from the for loop

问题

返回的翻译内容:

返回类型为void

无输入参数

通过将当前数字与从0加到(a+b)的下一个数字相加,以空格分隔打印出计算结果的数字。

例如,如果for循环的数字是0、1、2、3、4、5、6,那么它将分别计算0+1、1+2、2+3、3+4、4+5、5+6,并打印出这些值,就像是0、1、2、3、4、5、6一样。

老实说,我真不知道怎么做,所以我不会撒谎,能有人帮我编码并解释一下吗?

public class ForFogMe
{
   public int a, b;
   public String str;

    public void addUp(){  
       
     for(a = 0; a <= 6; a ++){
         System.out.print(a);        
        }
       
       String s = Integer.toString(a);
       System.out.println();
       System.out.print(s.substring(0,2) );
       
    }
   
   public static void main(String args[]){
        
       ForFogMe me = new ForFogMe();
       me.addUp();
    }
}
英文:

The Return type is void

No input parameters

Print out the numbers calculated results separated by a space using current number add the next number from 0 to (a+b).

An example would be if the numbers for the for loop are 0,1,2,3,4,5,6 then it would add 0+1, 1+2, 2+3, 3+4, 4+5, 5+6 and print out those values just like 0,1,2,3,4,5,6.

I honestly have no clue how to do this so I'm not going to lie about it so can someone help me code it and explain or just help me with it.

public class ForFogMe
{
   public int a, b;
   public String str;

    public void addUp(){  
       
     for(a = 0; a &lt;= 6; a ++){
         System.out.print(a);        
        }
       
       String s = Integer.toString(a);
       System.out.println();
       System.out.print(s.substring(0,2) );
       
    }
   
   public static void main(String args[]){
        
       ForFogMe me = new ForFogMe();
       me.addUp();
    }
}

答案1

得分: 2

如果您只想打印从0到6的数字之和您可以简单地像这样做

    public void addUp() {
       for(a = 0; a < 6; a++) {
           System.out.print(a+(a+1) + ",");
       }
       System.out.print("\b"); // 删除最后的逗号
    }

在第一次迭代中,`a``0`,`a+1``1`,因此您打印它们的和`(a+(a+1) + ",")`,输出为 `"1,"`。它重复执行直到达到 `6`。在结束时我们有 `1,3,5,7,9,11,`,因此我使用了 `System.out.print("\b");` 来删除最后一个字符这样我们就得到了 `1,3,5,7,9,11`。
英文:

If you only want to print sum of the numbers from 0 to 6 you would do it simply like this:

public void addUp() {
   for(a = 0; a &lt; 6; a++) {
       System.out.print(a+(a+1) + &quot;,&quot;);
   }
   System.out.print(&quot;\b&quot;); // to delete last comma
}

In first iteration a is 0 a+1 is 1 so you print their sum like (a+(a+1) + &quot;,&quot;) which outputs &quot;1,&quot;. It repeats until it reaches 6. At the end we have 1,3,5,7,9,11, so I used System.out.print(&quot;\b&quot;); to delete last char, so we get 1,3,5,7,9,11

答案2

得分: 0

我相信这应该能解决问题:

public static void addUp(){
    final int[] array = {0,1,2,3,4,5,6};
    int[] result = new int[array.length-1];
	for(int i = 0; i < array.length-1; i++) {
		result[i]=array[i]+array[i+1];
	}
	result[3]=array[array.length-1];
	for(int i = 0; i < result.length; i++) {
		System.out.print(result[i]+" ");
	}
}

测试案例(数组):

0,1,2,3,4,5,6

输出:

1 3 5 6 9 11

注意:数组大小无关紧要。

英文:

I believe this should do the trick:

public static void addUp(){
    final int[] array = {0,1,2,3,4,5,6};
    int[] result = new int[array.length-1];
	for(int i = 0; i &lt; array.length-1; i++) {
		result[i]=array[i]+array[i+1];
	}
	result[3]=array[array.length-1];
	for(int i = 0; i &lt; result.length; i++) {
		System.out.print(result[i]+&quot; &quot;);
	}
	

      
   }

Test case (array):

0,1,2,3,4,5,6

Outputs:

1 3 5 6 9 11 

Note: The array size does not matter.

huangapple
  • 本文由 发表于 2020年9月24日 09:14:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/64038240.html
匿名

发表评论

匿名网友

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

确定