• (2 Pages)
• 1
• 2

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

• D.I.C Lover

Reputation: 486
• 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)

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); }
}

```

### #4 erik.price

• D.I.C Lover

Reputation: 486
• 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()

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); }
}

```

### #6 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12242
• Posts: 45,328
• 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
}

```

Reputation:

## Re: help me with this please

Posted 27 March 2010 - 03:03 PM

macosxnerd101, 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
}

```

### #8 erik.price

• D.I.C Lover

Reputation: 486
• 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

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.

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); }
}
```

### #11 erik.price

• D.I.C Lover

Reputation: 486
• 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++;
}
```

### #12 erik.price

• D.I.C Lover

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

## Re: help me with this please

Posted 27 March 2010 - 04:31 PM

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); }
}

```

### #14 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12242
• Posts: 45,328
• 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;
}

```

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);
}
}

```