简体   繁体   English

for 循环产生一系列不同的数字

[英]for loop that produces series of different numbers

I have found a few similar topics to my issue, but couldn't figure out the task still, so I thought it'd be best to create my own topic.我找到了一些与我的问题类似的主题,但仍然无法弄清楚任务,所以我认为最好创建我自己的主题。

I need to write a for loop that produces the following output:我需要编写一个产生以下输出的 for 循环:

289 256 225 196 169 144 121 100 81 289 256 225 196 169 144 121 100 81

For added challenge, try to modify your code so that it does not need to use the * multiplication operator.为了增加挑战,请尝试修改您的代码,使其不需要使用 * 乘法运算符。

This is my code below, I'm stuck here so please help.这是我下面的代码,我被困在这里所以请帮忙。

public class Exercises2{
   public static void main(String[] args){
      int start = 19;
      int increment = 2;
      for(int count = 81; count <= 289; count++){


         System.out.println(count + start);
         start = increment + start;         

      }
   }
}

Below is what you need.下面是你需要的。 Notice the count+=start increment within the for loop and the start+=increment adding from the base of 17 so you increase count by 19 the first time, 21 the second, etc.注意 for 循环中的 count+=start 增量和从 17 开始的 start+=increment 增量,因此您第一次将 count 增加 19,第二次增加 21,依此类推。

Remember a for loop doesn't require a count++ it can be any valid command in the last portion or can be left out completely记住 for 循环不需要 count++,它可以是最后一部分中的任何有效命令,也可以完全省略

int start = 17;
int increment = 2;
for(int count = 81; count <= 289; count+=start){
    System.out.println(count);
    start+=increment;
}

Have you figured out the pattern for generating the numbers in the series?你有没有想出在系列中生成数字的模式? If not, the multiplication "challenge" is actually a big hint at how it's generated.如果不是,乘法“挑战”实际上是它如何产生的一个重要提示。

After that try to figure out how to write a loop that does multiplication manually and that should give you the answer you're looking for.之后尝试弄清楚如何编写一个手动执行乘法的循环,这应该会给你你正在寻找的答案。

Your main problem is that you're not incrementing count enough.你的主要问题是你增加的计数不够。 If you're going to have count go from 81 to 289 then you need to be doing more to count than just ++;如果您要将计数从 81 变为 289,那么您需要做的不仅仅是计数,而不仅仅是 ++; Just a couple changes fixes your own code.只需几处更改即可修复您自己的代码。 Change the start value to 17 and change how count is incremented to count += start.将起始值更改为 17,并将计数的递增方式更改为计数 += 开始。

int start = 17;
int increment = 2;
for(int count = 81; count <= 289; count += start){      
    System.out.println(count);
    start += increment;
}

I think there is value in doing your homework yourself and figuring it out can bring a lot of benefit and gain as a programmer.我认为自己做功课是有价值的,并且弄清楚它可以为程序员带来很多好处和收获。 But here is your answer without multiplication:但这是你没有乘法的答案:

 int start = 2;

int increment = 19;

int value = 81;

int _max = 289;

while(value <= _max)
{
    System.out.println(value);
    value += increment;
    increment += start;
}

Try this.尝试这个。

    int start=17;  
    int end=9;     
    for(int i=17;i>=9;i--)
    {
        System.out.println (i*i);

    }   

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM