Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Noobie Needs Help
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Caceres




PostPosted: Tue Jul 04, 2006 5:42 pm   Post subject: Noobie Needs Help

I can't get this program to work. And I've SERIOUSLY been working on it for about 7 hours now. It's seriously getting to me.
It's supposed to do:
if n = 3
Then it'll be: 3 + 4 + 5 +6 = 18. The sum of integers shuold be 18, but i can't get this to work with any of the numbers. Please help. Crying or Very sad

code:
/* This program's purpose is to read an integer value for n and then sums
the integers from n to 2*n if n is positive, or from 2*n to n if n is
negative. This program's code will be using only "for" loops. */

import TerminalIO.KeyboardReader;

public class ass26a {
   public static void main(String[] args) {
      KeyboardReader reader = new KeyboardReader();
      int n=0;
      int two_n=0;           
     
      System.out.println("This program will sum the integers from entered value to entered value * 2.");
      System.out.println("Enter an integer value (positive or negative):");
      n = reader.readInt();
     
      if(n>=0)     
      for(;n<=two_n;++n) {     
              }
       
          System.out.println("The sum of integers is " + SOMETHING HERE + "."); 
  }



ass26a.java
 Description:

Download
 Filename:  ass26a.java
 Filesize:  796 Bytes
 Downloaded:  106 Time(s)

Sponsor
Sponsor
Sponsor
sponsor
Caceres




PostPosted: Tue Jul 04, 2006 5:43 pm   Post subject: (No subject)

F.Y.I: I've been doing Java for about 6 days now. I know I still suck, but hopefully getting this program to work will help me. Thank you again.
~Caceres
wtd




PostPosted: Tue Jul 04, 2006 5:56 pm   Post subject: (No subject)

Work through the problem without writing code.

You get a number from the user.

You then loop from that number to that number multiplied by two.

Each time through that loop you add that value to some accumulator.

Or, you could create a recursive method that would do this quite nicely.
Caceres




PostPosted: Tue Jul 04, 2006 6:10 pm   Post subject: (No subject)

wtd wrote:
Work through the problem without writing code.

You get a number from the user.

You then loop from that number to that number multiplied by two.

Each time through that loop you add that value to some accumulator.

Or, you could create a recursive method that would do this quite nicely.


I know, this. And thanks for the reply.
And this is the part that i'm mainly having problems with. How do I keep on adding a value to the "accumlator."
I kknow I need some kind of code..But i don't know it.
Thanks.
~Caceres
Caceres




PostPosted: Tue Jul 04, 2006 6:12 pm   Post subject: (No subject)

wtd wrote:
Work through the problem without writing code.

You get a number from the user.

You then loop from that number to that number multiplied by two.

Each time through that loop you add that value to some accumulator.

Or, you could create a recursive method that would do this quite nicely.


Wait nvm. I don't think you clearly understand it *sorry if I'm mistaken).
But the user enters a number "n".
And I basically multiply the number by 2 which is for example 6...12.
Then i take ALL the numbers in between including the 2 numbers and add themtogether.

S it would be 6 + 7 + 8 + 9 + 10 + 11 + 12
And then i need to get the value of all of those numbers.
~Caceres
cool dude




PostPosted: Tue Jul 04, 2006 7:25 pm   Post subject: (No subject)

Caceres wrote:
wtd wrote:
Work through the problem without writing code.

You get a number from the user.

You then loop from that number to that number multiplied by two.

Each time through that loop you add that value to some accumulator.

Or, you could create a recursive method that would do this quite nicely.


Wait nvm. I don't think you clearly understand it *sorry if I'm mistaken).
But the user enters a number "n".
And I basically multiply the number by 2 which is for example 6...12.
Then i take ALL the numbers in between including the 2 numbers and add themtogether.

S it would be 6 + 7 + 8 + 9 + 10 + 11 + 12
And then i need to get the value of all of those numbers.
~Caceres


no actually wtd is correct! you still don't understand how to make a counted loop (for loop). you will need to learn how to do that. also why in the world are you making your for loop go up to two_n when the value of that is 0. it will never work. to make it increase to double the number just multiply n by 2. also you need an accumulator like wtd said so that u can add the sums up.
Caceres




PostPosted: Tue Jul 04, 2006 7:39 pm   Post subject: (No subject)

How do i make an accumulator? Confused
Thanks.
~Caceres
cool dude




PostPosted: Tue Jul 04, 2006 7:40 pm   Post subject: (No subject)

Caceres wrote:
How do i make an accumulator? Confused
Thanks.
~Caceres


i think you don't know wat an accumulator is.
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Tue Jul 04, 2006 7:57 pm   Post subject: (No subject)

An accumulator is just some variable that accumulates.
Caceres




PostPosted: Tue Jul 04, 2006 8:07 pm   Post subject: (No subject)

Ok, this is my newest code. But, the "sum of integers" is equal to 0. There must still be a little problem.
And..Am i doing the accumator right?
Sum = sum + n?

code:
/* This program's purpose is to read an integer value for n and then sums
the integers from n to 2*n if n is positive, or from 2*n to n if n is
negative. This program's code will be using only "for" loops. */

import TerminalIO.KeyboardReader;

public class ass26a {
   public static void main(String[] args) {
      KeyboardReader reader = new KeyboardReader();
      int n=0;
      int two_n=n*2; 
      int sum=0;         
     
      System.out.println("This program will sum the integers from entered value to entered value * 2.");
      System.out.println("Enter an integer value (positive or negative):");
      n = reader.readInt();
     
      if(n>=0)     
      for(sum=0;n<=two_n;++n) {
               sum = sum + n;
              }
            
       
          System.out.println("The sum of integers is " + sum + "."); 
  }

cool dude




PostPosted: Tue Jul 04, 2006 8:14 pm   Post subject: (No subject)

you have one problem and if its fixed your program would work. your problem as i stated before is that you don't know how to use/make for loops. Also why do you have that two_n variable? its not needed. in your for loop you can just make it go up to n * 2.
Caceres




PostPosted: Tue Jul 04, 2006 8:20 pm   Post subject: (No subject)

Nevermind. Someone helped me figure it out. Thanks anywyas.
~Caceres

Close this thread please.
Shocked
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 12 Posts ]
Jump to:   


Style:  
Search: