Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

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

1. 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?

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);
}

}  Reply With Quote

3. Re: make a integer negative? 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?  Reply With Quote

4. 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.  Reply With Quote

5. Re: make a integer negative?

Hey Lizard,

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

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:

Generating 10 random integers in range 0 to 99.
-49 86 -38 84 66 -18 95 -21 -15 -48
Result = 142  Reply With Quote

6. The Following User Says Thank You to JavaPF For This Useful Post:

Lizard (May 14th, 2009)