Hello, tell me which of the two codes is more correct, or what can be redone or added in the code.

one)

The program should display the first 55 elements of the sequence.
1 3 5 7 9 11 13 15 17 ...

First code:

public class Test { public static void main (String args[]) { int sum = 1; int count = 0; for (int i = sum; i <= 333; i += 2) { sum = i; count++; if (sum == 111) break; System.out.println (count + " элемент - " + sum); } System.out.println ("Первые " + (count - 1) + " элементов"); } } 

Second code:

 public class Test { public static void main(String args []) { int sum = 0; int count = 0; for (int i = sum; i <= 54; i++) { sum = 2 * i + 1; count++; System.out.println(count + " элемент - " + sum); } System.out.println("Первые " + count + " элементов"); } } 

2) Prompt and by this code that can be added or altered.

The program should display all non-negative elements of the sequence.
90 85 80 75 70 65 60 ...

 public class Test { public static void main(String args[]) { final int first = 90; final int second = -90; int count = 0; for(int i = first; i > second; i-=5) { count++; if(i == 0)break; System.out.println(count + " элемент - " + i); } System.out.println("Все неотрицательные элементы 90 85 80 75 70 65 60 …."); } } 

3) Prompt and by this code that can be added or altered.

The program should display the first 20 elements of the sequence.
2 4 8 16 32 64 128 ...

 public class Test { public static void main(String args[]) { final int a = 1; int sum; int count = 0; sum = (int) Math.pow(a,2); for(int i = 1; i <= 20; i++) { sum*=2; count++; System.out.println(count + " элемент - " + sum); } System.out.println("Первые " + count + " элементов"); } } 

PS I compiled everything and started the code, asking for advice on what can be completed or redone, as it will be more correct.

Tell me more on the code.

Display all the terms in the sequence 2an-1–1, where a1 = 2, which are less than 10,000.

Here I wrote the code with a for loop, it seems to work. First code

 public class Test { public static void main(String args []) { for(int a = 2, i = 1; i < a; a = 2 * a - 1,i++) { if(a <=10000) { System.out.print(a + " "); } } } } 

With a for loop, it outputs like this: 3 5 9 17 33 65 129 257 513 1025 2049 4097 8193

The second code with a while loop.

 public class Test { public static void main(String args []) { int a = 2; while(true) { if(a <= 10000) { a = 2 * a - 1; System.out.print(a); } } } } 

With a while loop, it outputs like this: 3 5 9 17 33 65 129 257 513 1025 2049 4097 8193 16385

Tell me how to write with a while loop.

Tell me by code that I can not figure it out correctly! everything seems to be logically correct

 public class Test { public static void main(String args []){ for(int a = -166, i = 1; a > 9 && a < 100; a = 2 * a + 200, i++){ System.out.println(a + " "); } } } 

Must output, but does not display the result on the screen all the two-digit terms of the sequence!

    5 answers 5

    For the first task, the second option with a few changes will fit better. The second option is slightly less effective, but it is much more readable. And there is no strange additional condition for exiting the cycle. And in general, the calculated "magic" numbers 111 and 333 are not gut.

     public class Test { public static void main(String args []){ int count = 0; for (int i = 0; i <= 54; i++) { int value = 2 * i + 1; count++; System.out.println(count + " элемент - " + value); } System.out.println("Первые " + count + " элементов"); } } 

    The second code is clearly redone from some other. Again, why do you need -90 if you explicitly check for zero inside the loop? Why inside for not to check on 0, and if to throw out?

    For the third, it would be nice to see if it were not for this.

     sum = (int) Math.pow(a,2); 

    Why are you squaring the unit in such a complicated way? Why don't you just write that

     sum = 1; 

    ?

    And the last. In the first and last task there is an explicit number counter. There is no special reason to do a special count. However, to control, and you can leave.

    And of course, in the first solution, you can apply the approach used in the third problem: do not multiply i each time, but simply increase the sum by two each time. Try it. Think about it. That's what the training task.

    UPD

     while(true) { if(a <= 10000) { 

    Here again: you have an eternal cycle. Your application will hang.

    • Thanks for the answer "cy6erGn0m" - turtles

    First, it is not desirable to use the break statement in a loop. Second, where are the numbers 333 and 111? Manual do not need to count anything.

     public class Test { public static void main(String args[]){ for(int i=0;i<55;i++) { System.out.println("Элемент №"+ i +" равен " + (i*2+1)); } System.out.println("Первые " + (i-1) + " элементов"); } } 

    The remaining examples are similar.

    • Thanks for the reply "DmitryBLR" - turtles

    1 Task

    In the second code, the extra variable sum in the part int sum=0; for(int i=sum; Well, the cycle from 1 to 55 seems more readable. Calculating the number will be 2*i-1

    2 Task

    The loop on the title to -90, but really exit on break . Move non-negativity check to loop header

    3 Task

    Again, the extra sum and a before the loop. Duplicate i and count variables. "One tablet is enough"

      In the first example, only for if(sum == 111)break; You can put a two, definitely take the second code there, although it’s not clear why in the for (int i = sum; i <= 54; i++) sum is used, it is better to write for (int i = 0; i <= 54; i++) then too.

      In the second example, it is generally not clear why final int second = -90;

      Instead of for(int i = first; i > second; i-=5) you can write for(int i = first; i > 0; i-=5) and even the condition if(i == 0)break; to remove

      In the third example, everything seems fine.

      • Thanks for the answer "korok" - turtles

      This is how it outputs, it is necessary to specify the limit of the maximum intov value so that there is no looping, everything is exactly:

       for (int i = 1, a = -166; i < 2147483647; a = 2 * a + 200, i++) { if(a > 9 && a < 100) { System.out.println(a); } }