Java BMICalculator does not work as expected
I am learning to code in Java and I cannot figure out why this won't work. I will add screenshots It keeps returning NaN I think this means: not a number ... but it is a number? The Screenshots:Java code and What it returns
java
add a comment |
I am learning to code in Java and I cannot figure out why this won't work. I will add screenshots It keeps returning NaN I think this means: not a number ... but it is a number? The Screenshots:Java code and What it returns
java
1
Please do not post code as images.
– CS_noob
Nov 25 '18 at 3:53
I will copy and paste it next time instead, but using screenshots is easier. (-_-)
– Anon5
Nov 26 '18 at 5:15
add a comment |
I am learning to code in Java and I cannot figure out why this won't work. I will add screenshots It keeps returning NaN I think this means: not a number ... but it is a number? The Screenshots:Java code and What it returns
java
I am learning to code in Java and I cannot figure out why this won't work. I will add screenshots It keeps returning NaN I think this means: not a number ... but it is a number? The Screenshots:Java code and What it returns
java
java
asked Nov 25 '18 at 3:41
Anon5Anon5
32
32
1
Please do not post code as images.
– CS_noob
Nov 25 '18 at 3:53
I will copy and paste it next time instead, but using screenshots is easier. (-_-)
– Anon5
Nov 26 '18 at 5:15
add a comment |
1
Please do not post code as images.
– CS_noob
Nov 25 '18 at 3:53
I will copy and paste it next time instead, but using screenshots is easier. (-_-)
– Anon5
Nov 26 '18 at 5:15
1
1
Please do not post code as images.
– CS_noob
Nov 25 '18 at 3:53
Please do not post code as images.
– CS_noob
Nov 25 '18 at 3:53
I will copy and paste it next time instead, but using screenshots is easier. (-_-)
– Anon5
Nov 26 '18 at 5:15
I will copy and paste it next time instead, but using screenshots is easier. (-_-)
– Anon5
Nov 26 '18 at 5:15
add a comment |
4 Answers
4
active
oldest
votes
As per your program , you are calculating BMI way before expecting user input. Hence you are getting that output, i.e. you are calculating BMI for 0 value.
The actual way is calculate BMI i.e. execute BMI statement after accepting user input values.
add a comment |
- At the beginning the value of
hm = 0
after evaluatingline 6 ,7 and 8
- Later when you use
hm
to calculateBMI
you are doing a divide by 0 (becausehm = 0
), which results inBMI = NaN
which java uses to refer to (+)(-)infinity or indeterminate type of results. - Later you think that you're calculating the
BMI
by assigning values tohm
andhf
but no, you're just assigning values them and nothing else and then you just print it which results in whatever is inBMI
being printed which isNaN
I'd advise you to go through an introductory programming course.
add a comment |
Java is a programming language without lazy evaluation, so you shouldn't write "how the value are calculated" before they are assigned with the real value.
You are just allowed to assign value then calculate them.
add a comment |
place the following statements
double hm = (hm*12 + hi)/39.37;
double wkg = wp/2.205;
double BMI = wkg/(hm*hm);
after the statement
wp = keyboard.nextInt();
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53464462%2fjava-bmicalculator-does-not-work-as-expected%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
As per your program , you are calculating BMI way before expecting user input. Hence you are getting that output, i.e. you are calculating BMI for 0 value.
The actual way is calculate BMI i.e. execute BMI statement after accepting user input values.
add a comment |
As per your program , you are calculating BMI way before expecting user input. Hence you are getting that output, i.e. you are calculating BMI for 0 value.
The actual way is calculate BMI i.e. execute BMI statement after accepting user input values.
add a comment |
As per your program , you are calculating BMI way before expecting user input. Hence you are getting that output, i.e. you are calculating BMI for 0 value.
The actual way is calculate BMI i.e. execute BMI statement after accepting user input values.
As per your program , you are calculating BMI way before expecting user input. Hence you are getting that output, i.e. you are calculating BMI for 0 value.
The actual way is calculate BMI i.e. execute BMI statement after accepting user input values.
answered Nov 25 '18 at 3:55
CS_noobCS_noob
4571311
4571311
add a comment |
add a comment |
- At the beginning the value of
hm = 0
after evaluatingline 6 ,7 and 8
- Later when you use
hm
to calculateBMI
you are doing a divide by 0 (becausehm = 0
), which results inBMI = NaN
which java uses to refer to (+)(-)infinity or indeterminate type of results. - Later you think that you're calculating the
BMI
by assigning values tohm
andhf
but no, you're just assigning values them and nothing else and then you just print it which results in whatever is inBMI
being printed which isNaN
I'd advise you to go through an introductory programming course.
add a comment |
- At the beginning the value of
hm = 0
after evaluatingline 6 ,7 and 8
- Later when you use
hm
to calculateBMI
you are doing a divide by 0 (becausehm = 0
), which results inBMI = NaN
which java uses to refer to (+)(-)infinity or indeterminate type of results. - Later you think that you're calculating the
BMI
by assigning values tohm
andhf
but no, you're just assigning values them and nothing else and then you just print it which results in whatever is inBMI
being printed which isNaN
I'd advise you to go through an introductory programming course.
add a comment |
- At the beginning the value of
hm = 0
after evaluatingline 6 ,7 and 8
- Later when you use
hm
to calculateBMI
you are doing a divide by 0 (becausehm = 0
), which results inBMI = NaN
which java uses to refer to (+)(-)infinity or indeterminate type of results. - Later you think that you're calculating the
BMI
by assigning values tohm
andhf
but no, you're just assigning values them and nothing else and then you just print it which results in whatever is inBMI
being printed which isNaN
I'd advise you to go through an introductory programming course.
- At the beginning the value of
hm = 0
after evaluatingline 6 ,7 and 8
- Later when you use
hm
to calculateBMI
you are doing a divide by 0 (becausehm = 0
), which results inBMI = NaN
which java uses to refer to (+)(-)infinity or indeterminate type of results. - Later you think that you're calculating the
BMI
by assigning values tohm
andhf
but no, you're just assigning values them and nothing else and then you just print it which results in whatever is inBMI
being printed which isNaN
I'd advise you to go through an introductory programming course.
answered Nov 25 '18 at 3:58
RyotsuRyotsu
565314
565314
add a comment |
add a comment |
Java is a programming language without lazy evaluation, so you shouldn't write "how the value are calculated" before they are assigned with the real value.
You are just allowed to assign value then calculate them.
add a comment |
Java is a programming language without lazy evaluation, so you shouldn't write "how the value are calculated" before they are assigned with the real value.
You are just allowed to assign value then calculate them.
add a comment |
Java is a programming language without lazy evaluation, so you shouldn't write "how the value are calculated" before they are assigned with the real value.
You are just allowed to assign value then calculate them.
Java is a programming language without lazy evaluation, so you shouldn't write "how the value are calculated" before they are assigned with the real value.
You are just allowed to assign value then calculate them.
answered Nov 25 '18 at 4:09
Geno ChenGeno Chen
2,3646925
2,3646925
add a comment |
add a comment |
place the following statements
double hm = (hm*12 + hi)/39.37;
double wkg = wp/2.205;
double BMI = wkg/(hm*hm);
after the statement
wp = keyboard.nextInt();
add a comment |
place the following statements
double hm = (hm*12 + hi)/39.37;
double wkg = wp/2.205;
double BMI = wkg/(hm*hm);
after the statement
wp = keyboard.nextInt();
add a comment |
place the following statements
double hm = (hm*12 + hi)/39.37;
double wkg = wp/2.205;
double BMI = wkg/(hm*hm);
after the statement
wp = keyboard.nextInt();
place the following statements
double hm = (hm*12 + hi)/39.37;
double wkg = wp/2.205;
double BMI = wkg/(hm*hm);
after the statement
wp = keyboard.nextInt();
edited Nov 25 '18 at 15:23
LundinCast
2,73641323
2,73641323
answered Nov 25 '18 at 4:00
Priyadeep DattaPriyadeep Datta
192
192
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53464462%2fjava-bmicalculator-does-not-work-as-expected%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Please do not post code as images.
– CS_noob
Nov 25 '18 at 3:53
I will copy and paste it next time instead, but using screenshots is easier. (-_-)
– Anon5
Nov 26 '18 at 5:15