+ 1

Java: If Else Statement. 11.2 Congratulations (I need help)

//Explanation is in the code. import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); double score = scanner.nextDouble(); // your code goes here double minimum = 3.5; if (double score > double minimum) { System.out.println("You passed!"); } else { System.out.println("Sorry"); /*Objective: If input is greater then 3.5, print:"You passsed!" If input is not greater then 3.5, print: "Sorry" I do not know why this code won't work. Yes, this is part of the Java course but the "You Passed" part is something I added in to make it more interesting. If you know what the issue is, please point it out and give the corrected code with explanation. The issue shouln't be that complicated so thanks in advance for the help. Sorry if I am asking for too much.*/ } } }

17th Jan 2021, 10:06 PM
Muaaz Ijaz
Muaaz Ijaz - avatar
4 Respuestas
0
Try removing the "double score" and "double minimum" inside the if statements. Your code should be like this: if(score>minimum){ .... }
17th Jan 2021, 10:26 PM
Hisham YUM 🇲🇦
Hisham YUM 🇲🇦 - avatar
+ 4
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); double score = scanner.nextDouble(); // your code goes here double average = 3.5; if(score > average){ System.out.println("Congratulations"); } else{ System.out.println("Sorry"); } } } // EXPLANATION The statement "double score = scanner.nextDouble();" means that the variable score will be user input which must be entered or typed in the terminal. When you hard code the score variable by defining it to a value, you invalidate the use of the scanner object. For example writing: score = 3.6; in this code is wrong.
2nd Sep 2021, 7:02 PM
Future Programmer
Future Programmer - avatar
0
can somebody explain to me why this is wrong, please? import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); double score = scanner.nextDouble(); // your code goes here double score = 3.5; if (double score is > 3.5) {System.out.println("Congratulations")}; else{System.out.println("sorry")}; } } I copied it exactly this code that's in the lesson public class Program { public static void main(String[] args) { int age = 14; if (age < 16) { System.out.println("Too Young"); } else { System.out.println("Welcome!"); } } }
20th Jan 2022, 1:04 AM
Juliette Dorce
0
import java.util.Scanner; public class Prac1 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); double score =4.5 scanner.nextDouble(); // your code goes here double value = score; System.out.println(value); if (score > 3.5) { System.out.println("Congratulations"); } else { System.out.println("sorry"); } } }
16th May 2022, 12:58 PM
Sen Sabu