#Output is wrong
1 messages · Page 1 of 1 (latest)
<@&987246399047479336> please have a look, thanks.
While you are waiting for getting help, here are some tips to improve your experience:
If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.
Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.
Show your code
It looks like the 22/7 is integer division, so the decimal there is being lost. Is r also an int by chance?
When you divide ints in Java, the result is also an int (Java just scraps the decimal if there is one).
I have declared r as an double
Oh okay.
I'm not sure what the current output is but I'm assuming the issue is with the 22/7. Can you try making one of those a double value to see if it keeps the decimal? Like this:java carea=(22d/7)*r*r; System.out.println(" Area of circle = " + carea);(The d suffix makes the number 22 a double value instead of just an int.)
Ah I see.
It means I need to initialise d=22;
I entered r as 7 and the output came as 147 whereas the ans is 154
Can you send the new code, if you changed it?
That's okay.
Sure!
Ok
You didn't use the discussed double cast. I dont think you're following what he meant by integer division.
Also you're told to use 22/7 and not the predefined Math.PI? (3.1415926....)
I used 22/7 only to calculate
Why not use Math.PI?
is the output still wrong ?
Its definitely wrong because 22/7 = 3
Integer division truncates any decimals (which would be obtained with modulus (%) )
You need to cast it to a double or use a double literal such as 22.0
or 22d
22.0 / 7 or 22d / 7 will convert both operands to doubles and perform 22.0 / 7.0 which gives a floating point answer
In this I will need to declare d as a double
no if u place a d behind an int, it will be treated as a double
? d is the literal syntax to denote a double
if you're referring to the top of the file, that is where imports go, yes, but why do you need the math library
Also java.math.* are math classes like BigInteger. java.lang.Math* is the math constants
int/double division examples:
double r = 4;
//Incorrect calculations
System.out.println(22/7 * r * r); //wrong
System.out.println((double)(22/7) * r * r); //wrong
// The following are correct division
System.out.println(22d/7 * r * r);
System.out.println(22.0/7 * r * r);
System.out.println(22/7d * r * r);
System.out.println(22/7.0 * r * r);
System.out.println((double) 22/7 * r * r);
System.out.println(22/(double)7 * r * r);
System.out.println((double) 22/7 * r * r);```
Ok I will try any of these
Thank you
For helping
If still prblm appears what to do ?
Can I do double r=in.nextDouble();
Thank you
Thank you it worked
Closed the thread.
Hello
My output is again coming wrong for another question
Not the area & volume one
My program is to calculate relative velocity and time taken to cross
The output coming was velocity as 0.0 and time as infinity
Here is the code
we cant if u dont share ur code properly
the image is not readable on mobile for example
Ok
It looks like the default label does not apply to any statement.
Lemme type
It applies to whole switch block
The default label you mean? It doesn't seem to be applying to anything. I figured that would be a compilation error.
Ah, nevermind. It's not.
No
It's not a compilation error
I suppose there's special syntax for switch blocks that lets you write a default or case label at the very end.
No there are no special syntax
I don't understand what you mean.
In my 2 yr experience I have only studied switch(variable),case, default,break syntax
For switch case prgms
In other prgms it's showing correct output by adding default:
{
case '1' :
speed=Math.abs(s1-s2);
break;
case '2' :
speed=s1+s2;
break;
default:
} ```
U shouldnt compare an int to a char. '1' is a char and will be converted to the ascii value
Ok
Which version of java are you using?
The latest one
Then there is another switch that you can use
Can you give me the another switch
Also may I ask, what is the value of speed if default?
I put speed as 50
For train 1
And for train 2 it's 40
Should I give question
?
I mean, is speed already set to something?
switch(choice){
case '1' -> speed = Math.abs(s1-s2);
case '2' -> speed = s1+s2;
default -> {}
}
Or better :
speed = switch(choice){
case '1' -> Math.abs(s1-s2);
case '2' -> s1+s2;
default -> speed;
}
@fossil pumice
I am back
I got my prblm
I have resolved
Thank you for helping me
Actually I had declared choice as an integer
Closed the thread due to inactivity.
If your question was not resolved yet, feel free to just post a message to reopen it, or create a new thread. But try to improve the quality of your question to make it easier to help you 👍