10 Replies - 329 Views - Last Post: 17 May 2014 - 11:23 AM Rate Topic: -----

#1 Gsussavz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 03-April 14

User types: 5. Result:15. ie (1+2+3+4+5)

Posted 17 May 2014 - 08:44 AM

[size="5"][size="4"]
import javax.swing.JOptionPane;
import java.util.Scanner;
class apples {
    public static void main(String[]args){
        String snum = JOptionPane.showInputDialog(null," Type number in to find answer.      EX: if number is 2, the answer is 3. (1+2)","Problem");
        int num = Interger.parseInt(snum);
        Testnum = 0 ;
        
                 
        if (num > Testnum) {
            (num + num--); 
        }
        
        System.out.print(num);
    }
}

Line 11 is "not a statement".

Is This A Good Question/Topic? 0
  • +

Replies To: User types: 5. Result:15. ie (1+2+3+4+5)

#2 Peter O  Icon User is offline

  • D.I.C Head

Reputation: 83
  • View blog
  • Posts: 196
  • Joined: 19-October 13

Re: User types: 5. Result:15. ie (1+2+3+4+5)

Posted 17 May 2014 - 08:51 AM

You add two numbers but you don't do anything with the result. Java doesn't allow that.
Was This Post Helpful? 0
  • +
  • -

#3 Gsussavz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 03-April 14

Re: User types: 5. Result:15. ie (1+2+3+4+5)

Posted 17 May 2014 - 08:54 AM

What should I do instead?
Was This Post Helpful? 0
  • +
  • -

#4 astonecipher  Icon User is offline

  • D.I.C.
  • member icon

Reputation: 768
  • View blog
  • Posts: 3,358
  • Joined: 03-December 12

Re: User types: 5. Result:15. ie (1+2+3+4+5)

Posted 17 May 2014 - 09:02 AM

Assign them to a variable. Use the variable later in your code.
Was This Post Helpful? 1
  • +
  • -

#5 Peter O  Icon User is offline

  • D.I.C Head

Reputation: 83
  • View blog
  • Posts: 196
  • Joined: 19-October 13

Re: User types: 5. Result:15. ie (1+2+3+4+5)

Posted 17 May 2014 - 09:04 AM

It depends on what the purpose of line 11 is. What do you want it to do?
Was This Post Helpful? 1
  • +
  • -

#6 Gsussavz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 03-April 14

Re: User types: 5. Result:15. ie (1+2+3+4+5)

Posted 17 May 2014 - 09:08 AM

I want to add the number to itself-1, ex: 5+ 5-1
each time I want to subtracts another one, so eventually it comes out to be 5+4+3+2+1
Was This Post Helpful? 0
  • +
  • -

#7 astonecipher  Icon User is offline

  • D.I.C.
  • member icon

Reputation: 768
  • View blog
  • Posts: 3,358
  • Joined: 03-December 12

Re: User types: 5. Result:15. ie (1+2+3+4+5)

Posted 17 May 2014 - 09:59 AM

Is this supposed to be a math problem or a string problem?
Was This Post Helpful? 1
  • +
  • -

#8 Gsussavz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 03-April 14

Re: User types: 5. Result:15. ie (1+2+3+4+5)

Posted 17 May 2014 - 10:17 AM

math
Was This Post Helpful? 0
  • +
  • -

#9 astonecipher  Icon User is offline

  • D.I.C.
  • member icon

Reputation: 768
  • View blog
  • Posts: 3,358
  • Joined: 03-December 12

Re: User types: 5. Result:15. ie (1+2+3+4+5)

Posted 17 May 2014 - 10:33 AM

So you need a loop to iterate through the input sum of n. And then keep subtracting until the number equals 1. Am I understanding?
Was This Post Helpful? 1
  • +
  • -

#10 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10786
  • View blog
  • Posts: 40,175
  • Joined: 27-December 08

Re: User types: 5. Result:15. ie (1+2+3+4+5)

Posted 17 May 2014 - 10:51 AM

I want to note as well that the sum you are calculating has a closed form solution- n(n+1)/2. So if n = 5, then 5(6)/2 = 15.
Was This Post Helpful? 2
  • +
  • -

#11 Gsussavz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 03-April 14

Re: User types: 5. Result:15. ie (1+2+3+4+5)

Posted 17 May 2014 - 11:23 AM

Thank you!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1