Thread: A tiny question

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Posts
    2
    Rep Power
    0

    A tiny question


    hello
    i am a beginer in java world and i have this question

    i want a code to do this

    1- prompt to enter a number
    2-number not less than 1
    3- the output needs to be like this -2+4-6+8-10+12-14+16-18+20 to the number i entered and stops

    thanks in advance
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    What have you tried so far? Post your code and the error messages you are getting or your questions.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Posts
    2
    Rep Power
    0
    ok
    this is what i did

    Code:
    
    public static void main(String[] args){
    
    Scanner num = new Scanner (System.in);
    int highestnum;
    int count = 0;
    System.out.println("Enter the number :") ;
    highestnum = num.nextInt();
    
    
    while(count<highestnum)
    {
    count= count+2;
    
    System.out.print(" "+count);
    }
    i cant fuigre out how to put - and + before the output one at a time

    plz help
  6. #4
  7. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    how to put - and + before the output one at a time
    Your code puts a " " before the input one at a time now.
    You need to change that so it puts either + or a - each time.
    First a - then a + then a -

    In the loop you need a way to decide which character to put out this time around.
    How can the loop decide which one to put out?
    One way would be to use the value of count. If count is even output one if odd output the other.

IMN logo majestic logo threadwatch logo seochat tools logo