2

I'm writing a program for my class where I have to use a for loop to takes two numbers from the keyboard. The program should then raise the first number to the power of the second number. Use a for loop to do the calculation. I'm getting the error that inum3 is not being initialized (I understand because the loop may never enter) but I cannot figure out how to make this work. Line 25 and 28 to be specific.

import javax.swing.*;


  public class Loop2
{
  public static void main(String[] args)
  {


    int inum1, inum2, inum3, count;
    String str;

    str = JOptionPane.showInputDialog("Please Enter a Numer");
    inum1 = Integer.parseInt(str);

    str = JOptionPane.showInputDialog("Please Enter a Numer");
    inum2 = Integer.parseInt(str);


    for (count = 1; count == inum2; count+=1)
    {
     inum3 = inum3 * inum1;
    }

     JOptionPane.showMessageDialog(null, String.format ("%s to the power of %s = %s", inum1,inum2, inum3), "The Odd numbers up to" + inum1,JOptionPane.INFORMATION_MESSAGE);
}//main
  }// public

3 Answers 3

3

you need to initialize the variable inum3. As it stands right now, when your program tries to execute

inum3 = inum3 * inum1;

inum3 has no value, so it can't do the multiplication.

I think you want it to be 1 in this case.

So instead of

int inum1, inum2, inum3, count;

you can do

int inum1, inum2, inum3 = 1, count;

Sign up to request clarification or add additional context in comments.

8 Comments

I changed the value of inum3 in my declarations, the problem I am having now is that the Joptionpane that displays at the bottom of the code is displaying a "1" where I want the new value set inside the loop to display.
If I move that Pane into the for loop, it does not display at all.
add a println to make sure that inum3 is correct after the loop completes. You definitely dont want the pane to be in the loop.
Thank you for the timely responses Hvgotcodes. The print line is displaying a "1" as well. So does that mean the math is not being executed inside the loop?
right, that's what it means. it only makes sense if you put inum1 as 1.
|
2

initialize num3 to one because you cand use something to define itself.

num3 = one;

Comments

1
import javax.swing.JOptionPane;

public class Loop2 {
    public static void main(String[] args) {

        int base, exp, result = 1;
        String str;

        str = JOptionPane.showInputDialog("Please Enter a Number");
        base = Integer.parseInt(str);

        str = JOptionPane.showInputDialog("Please Enter an Exponent");
        exp = Integer.parseInt(str);

        for (int count = 0; count < exp; count++) {
            result *= base;
        }

        JOptionPane.showMessageDialog(null, String.format("%s to the power of %s = %s", base, exp, result),
                "The Odd numbers up to" + base, JOptionPane.INFORMATION_MESSAGE);
    }
}

1 Comment

Thank you so much I now understand what I was supposed to be doing!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.