How to make a integer negative if it meets a certain criteria?

So I'm rookie to this stuff but I can usually read the program once I see it. Anyways I'm not sure how to make a integer negative if it meets a certain criteria.

So in this program I'm trying make the integer negative if its less than 50. If its greater than 50 then it stays positive then add up all the values.

Do need a while statement in here? If so how would I go about doing that?

Quote:

import java.util.Random;

import java.util.*;

public final class PartBRandomIntegers {

public static final void main(String[] args) {

int sum = 0;

int randomInt;

int result = 0;

System.out.println("Generating 10 random integers in range 0 to 99.");

Random randomGenerator = new Random();

for (int random = 1; random <= 10; ++random) {

randomInt = randomGenerator.nextInt(10);

result = (int) (Math.random() * 99);

System.out.print(result + " ");

sum = result+sum;

if (randomInt < 50)

randomInt = --;

if (randomInt > 50)

randomInt = ++;

}

System.out.println();

System.out.println("Sum = " + sum);

}

}

Re: make a integer negative?

Quote:

Originally Posted by **Lizard**

So in this program I'm trying make the integer negative if its less than 50. If its greater than 50 then it stays positive then add up all the values.

Can you give me an example please? So say the number is 33, you want it to output -33?

Re: make a integer negative?

Yes so:

-22, 56, -37, 88,....ect

Sum: 467

I want the output to be the sum of all the integers once their value has changed.

Re: make a integer negative?

Hey Lizard,

I have made a very slight edit to the code I helped you with previousally:

Code :

import java.util.Random;
import java.util.*;
public final class Sum10RandomIntegers {
public static final void main(String[] args) {
int sum = 0;
int randomInt;
int result = 0;
System.out.println("Generating 10 random integers in range 0 to 99.");
Random randomGenerator = new Random();
for (int random = 1; random <= 10; ++random) {
randomInt = randomGenerator.nextInt(10);
sum = (int) (Math.random() * 99);
if(sum < 50){
sum = -sum;
}
System.out.print(sum + " ");
result = result+sum;
}
System.out.println();
System.out.println("Result = " + result);
}
}

Example output:

Quote:

Generating 10 random integers in range 0 to 99.

-49 86 -38 84 66 -18 95 -21 -15 -48

Result = 142