Skip to main content
Code Review

Return to Answer

Commonmark migration
Source Link

You can greatly reduce the complexity of your code, by using the ternary operator:

while (n != 1){
 stepsTaken++;
 n = (n % 2 == 0) ? n / 2 : n * 3 + 1;
 System.out.print(n);
 //Your largest number and the linebreaks go here;
}

I removed the counter++ as you correctly mentioned, you can instead of counter == 9 use stepsTaken % 9 == 0

###Small explanation:

Small explanation:

this:

int i = {some value};
if(i % 2 == 0){
 i = i / 2;
}
else {
 i = i * 3 + 1;
}

can be shortened to the terary operator. It has the following syntax:

i = (condition) ? if-branch : else-branch;

That is the whole magic behind the shortened code.

You can greatly reduce the complexity of your code, by using the ternary operator:

while (n != 1){
 stepsTaken++;
 n = (n % 2 == 0) ? n / 2 : n * 3 + 1;
 System.out.print(n);
 //Your largest number and the linebreaks go here;
}

I removed the counter++ as you correctly mentioned, you can instead of counter == 9 use stepsTaken % 9 == 0

###Small explanation:

this:

int i = {some value};
if(i % 2 == 0){
 i = i / 2;
}
else {
 i = i * 3 + 1;
}

can be shortened to the terary operator. It has the following syntax:

i = (condition) ? if-branch : else-branch;

That is the whole magic behind the shortened code.

You can greatly reduce the complexity of your code, by using the ternary operator:

while (n != 1){
 stepsTaken++;
 n = (n % 2 == 0) ? n / 2 : n * 3 + 1;
 System.out.print(n);
 //Your largest number and the linebreaks go here;
}

I removed the counter++ as you correctly mentioned, you can instead of counter == 9 use stepsTaken % 9 == 0

Small explanation:

this:

int i = {some value};
if(i % 2 == 0){
 i = i / 2;
}
else {
 i = i * 3 + 1;
}

can be shortened to the terary operator. It has the following syntax:

i = (condition) ? if-branch : else-branch;

That is the whole magic behind the shortened code.

corrected bug in ternary assignment operator.
Source Link
Vogel612
  • 25.5k
  • 7
  • 59
  • 141

You can greatly reduce the complexity of your code, by using the ternary operator:

while (n != 1){
 stepsTaken++;
 n = (n % 2 == 0) ? n / 2 : n * 3 + n;1;
 System.out.print(n);
 //Your largest number and the linebreaks go here;
}

I removed the counter++ as you correctly mentioned, you can instead of counter == 9 use stepsTaken % 9 == 0

###Small explanation:

this:

int i = {some value};
if(i % 2 == 0){
 i = i / 2;
}
else {
 i = i * 3 + 1;
}

can be shortened to the terary operator. It has the following syntax:

i = (condition) ? if-branch : else-branch;

That is the whole magic behind the shortened code.

You can greatly reduce the complexity of your code, by using the ternary operator:

while (n != 1){
 stepsTaken++;
 n = (n % 2 == 0) ? n / 2 : n * 3 + n;
 System.out.print(n);
 //Your largest number and the linebreaks go here;
}

I removed the counter++ as you correctly mentioned, you can instead of counter == 9 use stepsTaken % 9 == 0

###Small explanation:

this:

int i = {some value};
if(i % 2 == 0){
 i = i / 2;
}
else {
 i = i * 3 + 1;
}

can be shortened to the terary operator. It has the following syntax:

i = (condition) ? if-branch : else-branch;

That is the whole magic behind the shortened code.

You can greatly reduce the complexity of your code, by using the ternary operator:

while (n != 1){
 stepsTaken++;
 n = (n % 2 == 0) ? n / 2 : n * 3 + 1;
 System.out.print(n);
 //Your largest number and the linebreaks go here;
}

I removed the counter++ as you correctly mentioned, you can instead of counter == 9 use stepsTaken % 9 == 0

###Small explanation:

this:

int i = {some value};
if(i % 2 == 0){
 i = i / 2;
}
else {
 i = i * 3 + 1;
}

can be shortened to the terary operator. It has the following syntax:

i = (condition) ? if-branch : else-branch;

That is the whole magic behind the shortened code.

added 329 characters in body
Source Link
Vogel612
  • 25.5k
  • 7
  • 59
  • 141

You can greatly reduce the complexity of your code, by using the ternary operator:

while (n != 1){
 stepsTaken++;
 n = (n % 2 == 0) ? n / 2 : n * 3 + n;
 System.out.print(n);
 //Your largest number and the linebreaks go here;
}

I removed the counter++ as you correctly mentioned, you can instead of counter == 9 use stepsTaken % 9 == 0

###Small explanation:

this:

int i = {some value};
if(i % 2 == 0){
 i = i / 2;
}
else {
 i = i * 3 + 1;
}

can be shortened to the terary operator. It has the following syntax:

i = (condition) ? if-branch : else-branch;

That is the whole magic behind the shortened code.

You can greatly reduce the complexity of your code, by using the ternary operator:

while (n != 1){
 stepsTaken++;
 n = (n % 2 == 0) ? n / 2 : n * 3 + n;
 System.out.print(n);
 //Your largest number and the linebreaks go here;
}

I removed the counter++ as you correctly mentioned, you can instead of counter == 9 use stepsTaken % 9 == 0

You can greatly reduce the complexity of your code, by using the ternary operator:

while (n != 1){
 stepsTaken++;
 n = (n % 2 == 0) ? n / 2 : n * 3 + n;
 System.out.print(n);
 //Your largest number and the linebreaks go here;
}

I removed the counter++ as you correctly mentioned, you can instead of counter == 9 use stepsTaken % 9 == 0

###Small explanation:

this:

int i = {some value};
if(i % 2 == 0){
 i = i / 2;
}
else {
 i = i * 3 + 1;
}

can be shortened to the terary operator. It has the following syntax:

i = (condition) ? if-branch : else-branch;

That is the whole magic behind the shortened code.

Source Link
Vogel612
  • 25.5k
  • 7
  • 59
  • 141
Loading
lang-java

AltStyle によって変換されたページ (->オリジナル) /