+ 1

Who can help me with this java program please. What is the value of the variable q and r?

public class Program { public static void main(String[] args) { int p = 1; int q = 2; int r = 3; if(r > p + q) { r = 2 * p } else if(r < p + q) { r = 2 * q } System.out.printIn("r"); else if{ q = 2 * r System.out.println("q"); } } }

23rd Feb 2017, 6:46 AM
Ukemeobong Akpan
12 Respostas
+ 24
wait... it will show many errors (most probably) here is the corrected code â˜ș & value of q & r will be 6 & 3 respectively public class Program { public static void main(String[] args) { int p = 1; int q = 2; int r = 3; if(r > p + q) { //this condition is false bcz 3 is not > 1+2 r = 2 * p ; } else if(r < p + q) { //this condition is also false r = 2 * q ; } else { System.out.println("r = "+r); q = 2 * r ; // q is 6 & r = 3 System.out.println("q ="+q); } } }
2nd Dec 2017, 3:07 AM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 2
It will show compile time error due to break of else if chain
30th Nov 2017, 1:56 PM
Ajay Verma
Ajay Verma - avatar
+ 1
don't u think it will give the error of dangling else
23rd Feb 2017, 6:53 AM
Vaibhav Tandon
Vaibhav Tandon - avatar
+ 1
When you want to print the value of any variable, you must not write it in "". Replace "r" by r in print statement & same for q.
23rd Feb 2017, 8:29 AM
Reshma A. Jivani
Reshma A. Jivani - avatar
+ 1
@SuspiciousUser Thank you for the correction. I really appreciate. Cheers
23rd Feb 2017, 10:47 AM
Ukemeobong Akpan
+ 1
Correct! I meant the same :-) @SuspiciousUser
23rd Feb 2017, 11:06 AM
Reshma A. Jivani
Reshma A. Jivani - avatar
+ 1
@reshma cheers thank you.
23rd Feb 2017, 11:12 AM
Ukemeobong Akpan
0
Yes it is given an error, what will be the correct code for it please? I know I have made a mistake somewhere.
23rd Feb 2017, 7:08 AM
Ukemeobong Akpan
0
Write print statement in loop
23rd Feb 2017, 7:16 AM
Reshma A. Jivani
Reshma A. Jivani - avatar
0
@reshma please can you show me what you mean because I have tried it and it didn't work.
23rd Feb 2017, 7:32 AM
Ukemeobong Akpan