Results 1 to 2 of 2
- 03-19-2008, 09:45 PM #1
Member
- Join Date
- Mar 2008
- Posts
- 1
- Rep Power
- 0
I need help fixing my code.. or non code?
import java.io.*;
import javax.swing.*;
class Numbert
{public static void main (String[] args) throws IOException
{ String inData;
int num ,num2;
int sum= num+num2;
int avg= sum/2;
inData = JOptionPane.showInputDialog (null, "Enter an Integer Value.") ;
num = Integer.parseInt ( inData); // convert inData to int
inData = JOptionPane.showInputDialog (null, "Enter another Integer Value.");
num2 = Integer.parseInt ( inData);
System.out.println ("Sum of both numbers:" +sum);
System.out.println ("The average is:" +avg);
System.out.println ("Good-bye for now, suckahfish!"); //always executed
}
}
-----------------
I need to display the sum and the average without initializing num and num2. I can't figure it out and it's killing me. I need to let the user enter any number they want and get the sum and averages displayed for them. I'm in college and this is the first Java assignment my professor has given us. Any help is appreciated, I really want to get familiar with this language.
- 03-19-2008, 10:12 PM #2
Member
- Join Date
- Jan 2008
- Posts
- 12
- Rep Power
- 0
It should look something like this:
The problem was you were trying to intialize sum and avg with the variables num and num2 before you had values there.Java Code:public class numbert { public static void main(String[] args) { String inData; int num ,num2; int sum; int avg; inData = JOptionPane.showInputDialog (null, "Enter an Integer Value.") ; num = Integer.parseInt (inData); // convert inData to int inData = JOptionPane.showInputDialog (null, "Enter another Integer Value."); num2 = Integer.parseInt (inData); sum=num + num2; avg= sum/2; System.out.println ("Sum of both numbers:" +sum); System.out.println ("The average is:" +avg); System.out.println ("Good-bye for now, suckahfish!"); //always executed } }
Similar Threads
-
Pls some one to help mi wit this code
By _nik_ in forum New To JavaReplies: 3Last Post: 02-10-2008, 02:02 AM -
I need help on my code
By jason27131 in forum New To JavaReplies: 4Last Post: 07-28-2007, 04:23 AM -
tic tac toe code
By zoe in forum New To JavaReplies: 1Last Post: 07-23-2007, 04:36 PM -
Need help with my code.
By stormviper in forum New To JavaReplies: 0Last Post: 07-12-2007, 03:18 PM -
Generating Code Automatically Using Custom code Template In Eclipse
By JavaForums in forum EclipseReplies: 1Last Post: 04-26-2007, 03:52 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks