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


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

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

  1. #1
    Junior Member
    Join Date
    May 2009
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts

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

    }
    Last edited by Deep_4; November 7th, 2012 at 10:46 PM.


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default 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?
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  3. #3
    Junior Member
    Join Date
    May 2009
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default 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.

  4. #4
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default 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
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

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

    Lizard (May 14th, 2009)

Similar Threads

  1. How to create directory in Java?
    By JavaPF in forum Java Programming Tutorials
    Replies: 2
    Last Post: December 24th, 2011, 12:53 PM
  2. Typecasting of double variable to integer
    By JavaPF in forum Java Programming Tutorials
    Replies: 2
    Last Post: December 5th, 2010, 03:41 AM
  3. Best beginners project in Java
    By Fendaril in forum Java Theory & Questions
    Replies: 3
    Last Post: February 10th, 2009, 08:52 AM
  4. How to check that console input should be integer only?
    By Konnor in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: February 2nd, 2009, 05:37 AM
  5. Java algorithm for bank program to connect database
    By araujo3rd in forum Algorithms & Recursion
    Replies: 1
    Last Post: December 10th, 2008, 01:34 PM