Bob42
02-06-2008, 02:22 AM
I have a program that I'm working on. What it does is take 2 integers and multiplies them together. The problem is that if the second integer is negative, a bunch of errors are displayed. I need to modify this method so that the second integer can be a negative number, but that's what I'm stuck on. I'm hoping that someone can lead me into the right direction.
import javax.swing.JOptionPane; // import class JOptionPane
public class App0644 {
public static void main(String[] args)
{
int x;
int y;
int result;
x = Integer.parseInt( JOptionPane.showInputDialog("Enter first integer:"));
y = Integer.parseInt( JOptionPane.showInputDialog("Enter second integer:"));
result = mystery(x, y);
System.out.println("Result: "+result);
System.exit(0); // terminate application with window
}
public static int mystery(int a, int b)
{
if (b == 1)
return a;
else
{
int result = 0;
result = a + mystery(a, b - 1);
return result;
}
}
}
import javax.swing.JOptionPane; // import class JOptionPane
public class App0644 {
public static void main(String[] args)
{
int x;
int y;
int result;
x = Integer.parseInt( JOptionPane.showInputDialog("Enter first integer:"));
y = Integer.parseInt( JOptionPane.showInputDialog("Enter second integer:"));
result = mystery(x, y);
System.out.println("Result: "+result);
System.exit(0); // terminate application with window
}
public static int mystery(int a, int b)
{
if (b == 1)
return a;
else
{
int result = 0;
result = a + mystery(a, b - 1);
return result;
}
}
}