Nested-if-Statement / Challenge | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Nested-if-Statement / Challenge

Hi all, following code below does not solve the problem and I do not know, why not. Can someone please help? Question: You are an administrator at a football club who must categorize already played games on the team's website. The given program takes 3 inputs: 1. match status - which checks if the match is suspended ("true") or not suspended ("false") 2. your team's score 3. opposing team's score. Complete the program so that if the match is suspended (the 1st input is "true"), it will output "Suspended". If the match is not suspended ( the1st output is false), the following statuses should be set depending on the match result: "Won", "Lost" and "Draw". Sample Input false 3 2 Sample Output Won My Code import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner read = new Scanner(System.in); boolean isSuspended = read.nextBoolean(); int ourScore = read.nextInt(); int theirScore = read.nextInt(); // your code goes here if (isSuspended = true) { System.out.println("Suspended"); } else{ if (ourScore > theirScore) { System.out.println("Won"); } else { if (ourScore == theirScore) { System.out.println("Draw"); } else { System.out.println("Lost"); } } } } }

8th Jan 2021, 7:05 PM
Johannes Bartsch
Johannes Bartsch - avatar
6 Answers
+ 1
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner read = new Scanner(System.in); boolean isSuspended = read.nextBoolean(); int ourScore = read.nextInt(); int theirScore = read.nextInt(); // your code goes here if (isSuspended == true) { System.out.println("Suspended"); } else if (ourScore>theirScore){ System.out.println("Won"); } else if(ourScore<theirScore){ System.out.println("Lost"); } else System.out.println("Draw"); } } this is the correct program, you used if (isSuspended = true) { System.out.println("Suspended"); } you should have written "==" beacuse "=" is an assignament operator
26th Mar 2021, 11:35 AM
Andrea Franchini
Andrea Franchini - avatar
+ 1
The sales manager decided to give a gift card to the customers whose purchases total more than 15000. On top of this, the customers whose total purchase is above 30000 will receive a second gift card. You are given a program, which takes the purchase amount as input, and print "Gift card" if it is above 15000. Task Complete the code to print "Gift card" again if the purchase is above 30000. Sample Input 36000 Sample Output Gift card Gift card
12th Mar 2023, 7:30 AM
Pawan Lokhande
Pawan Lokhande - avatar
0
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner read = new Scanner(System.in); boolean Sus = read.nextBoolean(); int our = read.nextInt(); int their = read.nextInt(); // your code goes here if (Sus == true) { System.out.println("Suspended"); } else { if (our > their) { System.out.println("Win"); } else if ( our < their) { System.out.println("Lost"); } else if ( our == their) { System.out.println("Draw"); } } } } This also dont work can anyone tell why
21st Jul 2021, 4:15 AM
Crious
Crious - avatar
0
if (isSuspended == false){ if(ourScore>theirScore){ System.out.println("Won"); }else{ if(ourScore<theirScore){ System.out.println("Lost"); } else{ System.out.println("Draw"); } } } else{ System.out.println("Suspended"); }
1st Aug 2021, 4:59 PM
Siddhartha Das
Siddhartha Das - avatar
- 1
This also works import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner read = new Scanner(System.in); boolean isSuspended = read.nextBoolean(); int ourScore = read.nextInt(); int theirScore = read.nextInt(); // your code goes here if (isSuspended == true){ System.out.println("Suspended"); } else{ if (ourScore > theirScore){ System.out.println("Won"); } else if (ourScore < theirScore){ System.out.println("Lost"); } else{ System.out.println("Draw"); } } } }
27th May 2021, 2:20 PM
Abhishek Biswas
Abhishek Biswas - avatar
- 4
The line after the comment, you used an assignment operator =, instead of a comparsion operator, which is == Also i want to suggest using the else if statement instead of nesting if statements inside else statements.
8th Jan 2021, 7:32 PM
Qwerty Uiop
Qwerty Uiop - avatar