help me with this please

  • (2 Pages)
  • +
  • 1
  • 2

29 Replies - 1248 Views - Last Post: 28 March 2010 - 08:06 PM Rate Topic: -----

#1 Guest_Kyle*


Reputation:

help me with this please

Posted 27 March 2010 - 12:44 PM

don't even know where to start with this... I am supposed to create a program that sums the negative odd integers up to 99 using a for loop

i have the following to start with, but don't even know where to start, can someone help me get started in the right direction??

public class SumIntegers
{
// Main method
 public static void main(String[] args)
 {
     Sum Integers sum = new SumIntegers();
     sum.runProgram();
 }

 private void runProgram()
 {
   int x;
   int y =0;
   for (x = 1; x<= 100;x = x + 1)
   {
     y = y + x;
     }
JOptionPane.showMessageDialog(null, "The answer is: " + y, "Results", JOptionPane.PLAIN_MESSAGE); }
}



Is This A Good Question/Topic? 0

Replies To: help me with this please

#2 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 486
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: help me with this please

Posted 27 March 2010 - 12:47 PM

Since you are counting down to -99 and only want to count the odd numbers, start at -1 increment by -2 each time:
for (x = -1; x > -100;x = x - 2)
Was This Post Helpful? 0
  • +
  • -

#3 Guest_Kyle*


Reputation:

Re: help me with this please

Posted 27 March 2010 - 02:21 PM

help, still don't really know where to go from here!

public class SumIntegers
{
// Main method
 public static void main(String[] args)
 {
     Sum Integers sum = new SumIntegers();
     sum.runProgram();
 }

 private void runProgram()
 {
   int x=0;
   int y =0;
   for (x = -1; x > -100;x = x - 2)  
   {
     y = y + x;
     }
JOptionPane.showMessageDialog(null, "The answer is: "  + y, "Results", JOptionPane.PLAIN_MESSAGE); }
}


Was This Post Helpful? 0

#4 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 486
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: help me with this please

Posted 27 March 2010 - 02:27 PM

No space here:
Sum Integers sum = new SumIntegers(); => SumIntegers sum = new SumIntegers();

Did you remember to import javax.swing.JOptionPane;?

It runs as private, but I would make runProgram public: public void runProgram()
Was This Post Helpful? 0
  • +
  • -

#5 Guest_Kyle*


Reputation:

Re: help me with this please

Posted 27 March 2010 - 02:32 PM

still won't compile what am i doing wrong?

here is my code now

public class SumIntegers
import javax.swing.JOptionPane;

{
// Main method
 public static void main(String[] args)
 {
     SumIntegers sum = new SumIntegers();
     sum.runProgram();
 }

 public void runProgram()  
 {
   int x=0;
   int y =0;
   for (x = -1; x > -100;x = x - 2)  
   {
     y = y + x;
     }
JOptionPane.showMessageDialog(null, "The answer is: "  + y, "Results", JOptionPane.PLAIN_MESSAGE); }
}



Was This Post Helpful? 0

#6 macosxnerd101  Icon User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12186
  • View blog
  • Posts: 45,250
  • Joined: 27-December 08

Re: help me with this please

Posted 27 March 2010 - 02:43 PM

You need to import before declaring your class. So:
import javax.swing.JOptionPane; 

public class SumIntegers {
  //define class here
}


Was This Post Helpful? 0
  • +
  • -

#7 Guest_Kyle*


Reputation:

Re: help me with this please

Posted 27 March 2010 - 03:03 PM

View Postmacosxnerd101, on 27 March 2010 - 01:43 PM, said:

You need to import before declaring your class. So:
import javax.swing.JOptionPane; 

public class SumIntegers {
  //define class here
}




ok fixed that, but the answer is supposed to be 5050, but I get something different, please help me!
Was This Post Helpful? 0

#8 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 486
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: help me with this please

Posted 27 March 2010 - 03:40 PM

Are you sure? I just did it with a calculator (yes, I'm bored today) and got -2500
Was This Post Helpful? 0
  • +
  • -

#9 Guest_Kyle*


Reputation:

Re: help me with this please

Posted 27 March 2010 - 04:06 PM

ok, now all I have to do is Convert the program into one using a while loop instead of a for loop.
Was This Post Helpful? 0

#10 Guest_Kyle*


Reputation:

Re: help me with this please

Posted 27 March 2010 - 04:16 PM

I need to convert this program to use two while loops instead of the for loops. I need a test at the top, and I need to print the results using a JoptionPane, and then reset my variables, and do the test at the bottom and display results. So, my program will have two while loops, one with the test at the top, and one with the test at the bottom.

how do I do this?

import javax.swing.JOptionPane;
public class SumIntegers
{
// Main method
 public static void main(String[] args)
 {
     SumIntegers sum = new SumIntegers();
     sum.runProgram();
 }

 private void runProgram()
 {
   int x;
   int y =0;
   for (x = 1; x<= 100;x = x + 1)
   {
     y = y + x;
     }
JOptionPane.showMessageDialog(null, "The answer is: " + y, "Results", JOptionPane.PLAIN_MESSAGE); }
}

Was This Post Helpful? 0

#11 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 486
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: help me with this please

Posted 27 March 2010 - 04:25 PM

If you have a for loop: for(int x = 0; x < 10; x++){}, you can make an equivalent while loop like this:
int x = 0;
while(x < 10){
   //code
   x++;
}

Was This Post Helpful? 0
  • +
  • -

#12 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 486
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: help me with this please

Posted 27 March 2010 - 04:31 PM

Please don't open duplicate topics :)
Was This Post Helpful? 0
  • +
  • -

#13 Guest_Kyle*


Reputation:

Re: help me with this please

Posted 27 March 2010 - 04:51 PM

ok, tried that why is my code not working?

thanks again for all of your help

import javax.swing.JOptionPane;
public class SumIntegers
{
// Main method
 public static void main(String[] args)
 {
     SumIntegers sum = new SumIntegers();
     sum.runProgram();
 }

 private void runProgram()
 {
   int x;
   int y =0;
   while (x = 1 || x<= 100) {
	   x = x + 1 }
   }
}
   {
     y = y + x;
     }
JOptionPane.showMessageDialog(null, "The answer is: " + y, "Results", JOptionPane.PLAIN_MESSAGE); }
}



Was This Post Helpful? 0

#14 macosxnerd101  Icon User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12186
  • View blog
  • Posts: 45,250
  • Joined: 27-December 08

Re: help me with this please

Posted 27 March 2010 - 05:02 PM

Proper indentations should make your extraneous braces more apparrent. I've commented to point out all your errors.
private void runProgram() { 
   int x; //you do not initialize x, which will give you an error

   int y =0; 
 
   //here, I think you mean x == 1
   //as x = 1 assigns 1 to x
   while (x = 1 || x<= 100) { 

           x = x + 1  //missing semi-colon
   }//end while 
}//end runProgram 
} //end class

//extraneous code block that will get
//class, interface or enum expected error
   { 
     y = y + x; 
     } 


Was This Post Helpful? 0
  • +
  • -

#15 Guest_Kyle*


Reputation:

Re: help me with this please

Posted 27 March 2010 - 05:17 PM

still don't know why I am getting an error

import javax.swing.JOptionPane;
public class SumIntegers
{
// Main method
 public static void main(String[] args)
 {
     SumIntegers sum = new SumIntegers();
     sum.runProgram();
 }

 private void runProgram()
 {
   int x;
   int y =0;
   while (x == 1 || x<= 100) {
	   x = x + 1; 
   }
	   
	   
		     y = y + x; 
		     } 

 
JOptionPane.showMessageDialog(null, "The answer is: " + y, "Results", JOptionPane.PLAIN_MESSAGE); 
}
}


Was This Post Helpful? 0

  • (2 Pages)
  • +
  • 1
  • 2