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: (Java) Help with alternating negative and positive numbers.

  1. #1
    Junior Member
    Join Date
    Dec 2013
    Posts
    1
    My Mood
    Yeehaw
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question (Java) Help with alternating negative and positive numbers.

    Hello coders, I'm new to this forum and especially new to programming.

    But my program objective is to display an array of 200 integers, with the numbers(element) starting at 5, increasing by 5, and negate the even numbers. For instance: 5, -10, 15, -20 and so on.

    I'm having problems negating and displaying the negative numbers
    I believe I'm suppose to use modulus, but I have no clue how

    What I have so far:
    public class Arrays2 {
     	public static void main(String [ ] commandlineArguments){
     
          //Declare & Instatiate an Array of 200 integers
             Integer[ ] arrayOf200Integers = new Integer[200];
     
          //initialize the array
             Integer powersOf2 = new Integer(5);	
             for(int i=0;i<arrayOf200Integers.length;i++){
                arrayOf200Integers[i] = powersOf2;
             	//add again by 5
                powersOf2 = powersOf2 + 5;
             }
             //Display Array
             Integer numerator = new Integer(0);
    				for(int i=0;i<arrayOf200Integers.length;i++){
                numerator = i;
                System.out.println("index = " +numerator + "," + "element =" + arrayOf200Integers[i]);
    			}	      	 
          }
       }
    My output:
    index = 0,element =5
    index = 1,element =10
    index = 2,element =15
    index = 3,element =20
    index = 4,element =25
    ETC.


  2. #2
    Junior Member
    Join Date
    Dec 2013
    Posts
    3
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: (Java) Help with alternating negative and positive numbers.

    Hi check this code :
    ...
    Last edited by copeg; December 15th, 2013 at 09:55 AM. Reason: Removed spoonfed code

  3. The Following User Says Thank You to sam965 For This Useful Post:

    fcknDan (December 15th, 2013)

  4. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,792
    My Mood
    Mellow
    Thanks
    175
    Thanked 626 Times in 613 Posts

    Default Re: (Java) Help with alternating negative and positive numbers.

    sam965: Please don't provide solutions. Please read this info for new forum users.

  5. #4
    Member
    Join Date
    Sep 2013
    Posts
    68
    My Mood
    Confused
    Thanks
    3
    Thanked 7 Times in 7 Posts

    Default Re: (Java) Help with alternating negative and positive numbers.

    Before inserting any integer to arrayOf200Integers check whether it is an even number or not, if it's an even number then change it sign to negative and then add it to arrayOf200Integers.

Similar Threads

  1. catch negative numbers
    By garry in forum Exceptions
    Replies: 6
    Last Post: November 8th, 2013, 09:05 PM
  2. Java chunk negative number problem
    By Bingo90 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 17th, 2013, 01:24 PM
  3. Have trouble dealing with negative numbers.
    By Arick Cheng in forum What's Wrong With My Code?
    Replies: 6
    Last Post: July 29th, 2012, 07:41 AM
  4. Method returning negative numbers seemingly at random
    By NcAdams in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 24th, 2012, 09:53 AM
  5. This java program is adding up in a negative way....
    By seaofFire in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 23rd, 2012, 04:58 PM

Tags for this Thread