This is a discussion on newbie question in java within the Java Programming forums, part of the Software Development category; hi there, I need to display the sum and the average without initializing passing inputs. I can't figure it ...
| |||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
|
#1
| |||
| |||
| hi there, I need to display the sum and the average without initializing passing inputs. I can't figure it out and it's killing me. I can't seem to get the sum or average displayed without assigning the variables values. Code:
import java.io.*;
import javax.swing.*;
class Number
{
public static void main (String[] args) throws IOException
{
String inData;
int num,num2;
int sum;
int avg;
inData = JOptionPane.showInputDialog (null, "Enter an Integer Value. NOW!") ;
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!"); //always executed
}
} Last edited by latchu : 03-26-2008 at 12:43 AM. |
|
#2
| |||
| |||
| You don't have any code for calculating the sum or the average. What do you mean, you have to display them without initializing ? num and num2 must get a value somehow... |
|
#3
| |||
| |||
| Besides other errors you can make those variables static class variables so the JVM will initialize them to zero for you. |
|
#4
| |||
| |||
| I'm really new to java and I don't exactly know how to make the variables static classes. Or do anything else to be honest. I did a tutorial and it didn't help. |
|
#5
| |||
| |||
| Quote:
They need to get their value from the user entering a number and after they enter the two numbers I need to have the program display the sum and the average. |
|
#6
| |||
| |||
| Quote:
k here's a way i found: what keeps you from doing the following after num and num2 have received their values: Code:
sum= num+num2;
avg= sum/2; It's a spoiler so you have to figure out how it works yourself. And you have to show us your working code. You don't have to perform the secret dance though ;-)
__________________ cheers Aman |
|
#7
| |||
| |||
| I got that far but when I compiled.. am getting num wasn't initialized.. And I don't know how to fix that. So I erased it and posted what I had. |
|
#8
| |||
| |||
| Code: import java.io.*;
import javax.swing.*;
class Numbert
{
int num , num2;
int sum= num+num2;
int avg= sum/2;
{public static void main (String[] args) throws IOException
{ String inData;
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
}
} |
|
#9
| |||
| |||
| I figured it out.. I was defining the sum/avg before num and num2 could be assigned values by the user. import java.io.*; import javax.swing.*; class Numbert { int num , num2; {public static void main (String[] args) throws IOException { String inData; 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); int sum= num+num2; int avg= sum/2; System.out.println ("Sum of both numbers:" +sum); System.out.println ("The average is:" +avg); System.out.println ("Good-bye); //always executed } } |
|
#10
| |||
| |||
| OK, so what do you have so far to accomplish this task? You've got this working for 2 numbers. Instead of making 20 variables named num, num2, num3, num4,...etc., use an array. Then figure out how to find the sum and average of any amount of numbers, not just 20. (That will be more impressive!) |
![]() |
| Thread Tools | |
| Display Modes | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Hi Newbie here! | shifanyroma | Introductions | 2 | 08-14-2009 09:43 PM |
| newbie | pramod | Introductions | 2 | 07-08-2009 11:00 PM |
| Newbie here :) | angel_dust | Introductions | 3 | 07-05-2009 02:13 AM |
| Newbie! | taramichael | Introductions | 2 | 07-05-2009 01:22 AM |
Our Partners |