Welcome to Dream.In.Code
Become a Java Expert!

Join 150,367 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,795 people online right now. Registration is fast and FREE... Join Now!




Multiplication sequence

 
Reply to this topicStart new topic

Multiplication sequence

CrazyJ
7 Feb, 2008 - 09:37 AM
Post #1

D.I.C Head
**

Joined: 15 Oct, 2007
Posts: 51


My Contributions
This program was posted earlier and I thought I had it figured out, so I thanked everyone and thought that was it. But... I am still having a problem...

The below program is supposed to take input for integer value "n" and then output an iteration from 1 to n where each digit in the sequence is multiplied by -2 and then summed. So, if I enter 3 the output should be: 1, -2, 4...and the sum would be 3. The trouble is that right now if I enter 3, I only get 1, -2 and if I enter any number between 4 and 15 I only get 1, -2, 4, -8. I know that I am missing something simple, I just don't know what. Can someone please tell me what I'm doing wrong?
CODE

        int n;
//        ======
        System.out.println("This program will take input for the value n");
        System.out.println("and output the sequence.");
        System.out.println("=======================================");
//        ==============================================================
        Scanner scannerObject = new Scanner(System.in);
//        ===============================================
        System.out.println("Please input an integer: ");
        n = scannerObject.nextInt();
        System.out.println("Integer entered = " +n);
//        ==========================================
        int ii;
        int formula = 1 *-2;
        for(ii = 1;ii <= n;ii = formula * ii++)
        {
      
            System.out.println(ii);
            
        }


I really want to learn something here, and put this behind me! So please reply soon? Thanks.
User is offlineProfile CardPM
+Quote Post

Nayana
RE: Multiplication Sequence
7 Feb, 2008 - 10:01 AM
Post #2

DIC Hawk - 나야나 नयन:
Group Icon

Joined: 14 Nov, 2007
Posts: 824



Thanked: 5 times
Dream Kudos: 175
My Contributions
You cannot store a formula in a variable. You should put it in a function, or calculate it in the loop body.

You should also use a different variable for counting the iterations, and another one for the value.

Try it, post your attempt and someone will help you more.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 02:01PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month