#Problem with counting...

1 messages ยท Page 1 of 1 (latest)

stone fossilBOT
#

<@&987246399047479336> please have a look, thanks.

stone fossilBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

frank topaz
sudden glacier
#

The other alternative is using IntStream

frank topaz
#

what you can do is count from 0 to 5

#

subtract 3

#

and take the absolute value

#
int value = 4;
while (true) {
    System.out.println(Math.abs(value - 3));
    value = (value + 1) % 6;
}
#

just some fun little math tricks

sudden glacier
#

The output is incorrect for that code

frank topaz
#

that doesn't work exactly, 1 sec

#
int value = 0;
while (true) {
    System.out.println(1 + value);
    value = (value + 1) % 3;
}
stone fossilBOT
frank topaz
#

there

#

so you start at 0, print 1

#

go up to 1, print 2

#

go up to 2, print 3

#

then (2 + 1) % 3 is 0

#

and we're back to the start

#

err wait thats not right either

#

sorry im an idiot

#

but modulo math is usually the most fun way to go

#

that is your toy to play with

lofty pivot
#

this question is really simple with if statements, but... i decided to do with modulo just for the fun

#

TL;DR:

final int mod = 9; // Change this number

// Don't change anything below

final int length = mod * 2 - 1;
int value = length - mod;

while(true) {
    System.out.print(Math.abs((value * 2 - length + 1)/2) + 1);
    value = (value + 1) % (length - 1);
}
#

Detailed explanation:
@frank topaz your dream came true my friend

final int mod = 9; // Change this number

// Don't change anything below

// This will result in the "length" of the sequence
// I.E.: 3 => 12321 => 5 numbers
final int length = mod * 2 - 1;

// If 0: start descending
// If length - mod: start ascending
int value = length - mod;

while(true) {

    // That's tricky, let's get step by step:

    //  1: (value * 2 - length + 1)
    //  - "- length + 1" would be I.E. (mod = 3): - 5 + 1 = -4
    //  - The sequence I.E. (mod = 3): -4, -2, 0, 2, 4

    //  2: Math.abs
    //  - Just the absolute value (removing the "-")
    //  - The sequence I.E. (mod = 3): 4, 2, 0, 2, 4

    //  3: /2
    //  - Dividing by two
    //  - The sequence I.E. (mod = 3): 2, 1, 0, 1, 2

    //  4: + 1
    //  - The final touch to make everything work correctly
    //  - The sequence I.E. (mod = 3): 3, 2, 1, 2, 3

    System.out.print(Math.abs((value * 2 - length + 1)/2) + 1);

    // Why (length - 1) instead of just length?
    // Because it would duplicate the last number
    // I.E. (mod = 3): 1232112321 (see the "11")
    // This "-1" stops the cycle EARLY this happens
    value = (value + 1) % (length - 1);
}
smoky rain
small coyote
#

try this::

#

import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int length_of_series =sc.nextInt();
int begin = 0;
for(int i=0;i<length_of_series;i++){

     if(begin<=3){
         begin++;
         
         for(int a=0;a<3;a++){
             if(begin ==4){break;
             }
     System.out.println(begin);
        begin = begin +1;
    
         }
     }
     
     
     if(begin>3){
         begin =3;
         for(int b =0;b<2;b++){
           begin = begin-1;
         System.out.println(begin);
         }
     
     }
  }
}

}

#

length of series based on input ::

#

if length of series is 1 output be::12321

#

if length of series is 2 output be:123212321

#

@still cedar

still cedar
#

nice

small coyote
#

๐Ÿ‘

stone fossilBOT
# small coyote import java.util.*; public class Main { public static void main(String[] arg...

Detected code, here are some useful tools:

Formatted code
import java.util. * ;

public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int length_of_series = sc.nextInt();
    int begin = 0;
    for (int i = 0; i < length_of_series; i++) {
      if (begin <= 3) {
        begin++;
        for (int a = 0; a < 3; a++) {
          if (begin == 4) {
            break ;
          }
          System.out.println(begin);
          begin = begin + 1;
        }
      }
      if (begin > 3) {
        begin = 3;
        for (int b = 0; b < 2; b++) {
          begin = begin - 1;
          System.out.println(begin);
        }
      }
    }
  }
}
stone fossilBOT
stone fossilBOT
#

Closed the thread due to inactivity.

If your question was not resolved yet, feel free to just post a message to reopen it, or create a new thread. But try to improve the quality of your question to make it easier to help you ๐Ÿ‘