java


how to print an integer (of another function) from main function?


I have just begin learning java. Could any one tell me how to print int c from main function?
Here's my code:
class code {
public static void main (String[] args) {
sum(44.1, 55.6);
product(34.1, 1.1);
System.out.print(c);
}
public static double sum(double a, double b) {
double c=a+b;
return c;
}
public static void product(double a, double b) {
double c=a*b;
System.out.print(c);
}
}
Why c is not coming in main function?
I get this error:
C:\Users\BN Com\Documents\JCreator Pro\MyProjects\square
System.out.print(c);
^
symbol: variable c
location: class code
1 error
It looks like this:
c is a local variable in sum. You cannot refer to it outside this method. You could, however, store the returned value in a local variable in main and use it from there. E.g.:
public static void main(String[] args) {
int s = sum(44.1, 55.6);
System.out.println(s);
}
You didn't declare the variable c in main function. try this
System.out.print(sum(44.1,55.6));
First, you need to understand the meaning of variable scope. In this case, your variable c is declared only inside your sum function. You are not able to access it outside that function. Same goes for c in the product function. If you wish to print the value, you have two options.
The first one, printing from inside the function, as you did with product. Then, when you call that function in main, the value will be printed.
public static void main(String[] args)
{
product(1,2); //This will print the value
}
Second one, return the value as you did with sum, but assign it to a variable on main and then print it. You may also call the function inside System.out.prinln().
public static void main(String[] args)
{
//Print directly
System.out.println(sum(1,2));
//Assign to a variable and print. Note that this c is no the same
//variable you have inside your functions.
double c= sum(1,2);
System.out.println(c);
}

Related Links

How to see all tables in my h2 database at localhost:8082?
JODA api convert date between timezones
some characters are saving into text file instead of generated random number
Exception in thread main error in array program
Plotting an audio signal using jfreechart (Amplitude vs time)
File Array of Strings
No Such Bean definition Found -Exception
can't predicted the class with Weka (using java code )
Creating a numeric pattern in a loop
Determining the element that occurred the most in O(n) time and O(1) space
Android onCreateOptionsMenu is not displaying
Difference between class locking and object locking in Java
Compiling Scala before / alongside Java with Gradle
Change the default Button-Label on android Keyboard
I can't open XML file with saxParser in Android
Cobertura 2.0.3 - unable to instrument

Categories

HOME
sendgrid
yii2
arduino-uno
log4j
client
variables
deployment
minimum-spanning-tree
mean-stack
read-eval-print-loop
q
adb
paw-app
sharepoint-designer
google-project-tango
ravendb
mouse
quicklook
modx-revolution
u-sql
windows-server-2012
quartz-scheduler
transformation
text-rendering
beyondcompare
samoa
grails3
autoconf
google-search-api
mustache.php
pass-by-reference
kudan
buck
gsmcomm
ecto
fog
openoffice.org
stacked
automake
azure-ml
user-controls
trim
particles.js
texmaker
ibpy
firebase-admin
unixodbc
thin
wso2carbon
long-polling
outlook-2013
hibernate-tools
komodoedit
logparser
clean-architecture
typescript1.8
return-value
fancybox-2
mplayer
recursive-datastructures
setter
epson
objective-c-swift-bridge
nested-sets
log4c
file-writing
firebaseui
lttng
sigabrt
xpath-1.0
natvis
rtbkit
ios8-today-widget
jms2
sdhc
spidermonkey
sframe
ibaction
mono-embedding
census
bridge.net
formatjs
dia
offloading
typekit
c3
tablelayout
angularjs-ng-click
preferences
terminfo
openexr
poller
braille
dealloc
meteor-velocity
geos
dexterity
gridcontrol
smtp-auth
jelly
ocunit
clipper
pvrtc
jquery-knob
broken-links
onsubmit
cufon
nsdatecomponents
invite
javax.script
locate
gdlib
fluent-interface
jquery-ui-droppable
boost-smart-ptr
privilege
mediarss
3gp

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile