0

Code coach for loop problem - Board Game Players

I'm solving this problem and it technically works (results are correct), but the excercise is still telling me something is wrong. What should I change? Problem: Deck dealer that deals 7 cards - 5x Good Guy, 2x Bad Guy. Write a program that will take the bad guys numbers as input and output all the roles accordingly. My solution is: import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner read = new Scanner(System.in); int firstBadGuy = read.nextInt(); int secondBadGuy = read.nextInt(); for(int x=1; x<=7; x++) { if(x==firstBadGuy || x==secondBadGuy) { System.out.println("Bad Guy"); }else{ System.out.println("Good Guy"); } } } }

30th Dec 2020, 12:37 PM
Anna
Anna - avatar
13 Answers
+ 3
You're all overthinking this... The reason that doesn't work is simply because the "Guy" in the strings are capitalized. The answer is case sensitive. Try it again with "Bad guy" and "Good guy".
28th Jun 2021, 5:44 PM
Lyrenide
Lyrenide - avatar
+ 3
Anna Ofcourse, your code will be correct but you should write the code based on constraints and conditions by the problem. . for example: if in your problem display the output is " Good guy! " but in your code display the result is " Good Guy " . the out put format is different here. there may be a chance to didn't pass the test cases. are you got it..? I am not sure .. . .(or) can you show the complete description of your problem..?
30th Dec 2020, 12:51 PM
NavyaSri
NavyaSri - avatar
+ 2
Yeah, but I checked like milion times. :D Maybe there's some other way I should go, so the code is "correct" (even though it works on every case)...
30th Dec 2020, 1:19 PM
Anna
Anna - avatar
+ 2
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner read = new Scanner(System.in); int firstBadGuy = read.nextInt(); int secondBadGuy = read.nextInt(); //your code goes here for(int i = 1; i <= 7; i++) { if(i == firstBadGuy || i == secondBadGuy ) { System.out.println("Bad guy"); } else { System.out.println("Good guy"); } } } }
7th Jan 2021, 12:02 AM
Ghufran Ismail
Ghufran Ismail - avatar
0
anyone solve astonomy Board game
27th Dec 2021, 3:55 AM
Chandrika Chalakapur
Chandrika Chalakapur - avatar
0
I hate the case sensitive answers. why do they have to be so annoying?
29th Jan 2022, 5:58 PM
Momin Barlas
Momin Barlas - avatar
0
A board game company creates new board games every year. While all the games have different rules, they also are all similar in that they each have a name and a play() method. We need to create 3 different games - Monopoly, Chess and Battleships. In the play() method Monopoly should print “Buy all property.”, Battleships - “Sink all ships.”, and Chess - “Kill the enemy king.” Complete the code by implementing the getName() and play() methods inherited from abstract Game class.
21st Nov 2022, 8:37 AM
Camilo Alarcon
Camilo Alarcon - avatar
0
This code doesnt work please help
21st Nov 2022, 8:37 AM
Camilo Alarcon
Camilo Alarcon - avatar
0
class Main { public static void main(String[] args) { //no toques este código Monopoly monopoly = new Monopoly(); Chess chess = new Chess(); Battleships battleships = new Battleships(); monopoly.play(); chess.play(); battleships.play(); } } abstract class Game { abstract String getName(); public String name; abstract void play(); } class Monopoly extends Game { //da el nombre "Monopoly" al juego String getName() { return name; } // el método play method debe imprimir "Buy all property." void play() { System.out.println("Buy all property"); } } class Chess extends Game { //da el nombre "Chess" al juego String getName() { return name; } // el método play debe imprimir "Kill the enemy king" void play() { System.out.println("Kill the enemy king"); } } class Battleships extends Game { //da el nombre "Battleships"
21st Nov 2022, 8:38 AM
Camilo Alarcon
Camilo Alarcon - avatar
0
class Main { public static void main(String[] args) { //do not touch this code Monopoly monopoly = new Monopoly(); Chess chess = new Chess(); Battleships battleships = new Battleships(); monopoly.play(); chess.play(); battleships.play(); } } abstract class Game { public String name; abstract String getName(); abstract void play(); } class Monopoly extends Game { //give "Monopoly" name to game String getName() { return name; } // play method should print "Buy all property." void play() { System.out.println("Buy all property"); } } class Chess extends Game { //give "Chess" name to game String getName() { return name; } // play method should print "Kill the enemy king." void play() { System.out.println("Kill the enemy king"); } } class Battleships extends Game { //give "Battleships" name to game String getName() { return name; } // play method should print "Sink all ships." void play() { System.out.println("Sink all ships"); } }
26th Jan 2023, 2:09 AM
Komal Mankar
Komal Mankar - avatar
- 1
change the x to i it will works
7th Jan 2021, 12:01 AM
Ghufran Ismail
Ghufran Ismail - avatar
- 1
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner read = new Scanner(System.in); int firstBadGuy = read.nextInt(); int secondBadGuy = read.nextInt(); for(int x=1; x<=7; x++){ if (x== firstBadGuy || x== secondBadGuy) { System.out.println("Bad guy"); } else { System.out.println("Good guy"); } } } } This exact same code worked for me.
16th Feb 2021, 4:36 AM
Nina Rus
- 1
Yes, I actually got that while back and nearly screamed with frustration. :D But thank you. :)
3rd Jul 2021, 11:05 AM
Anna
Anna - avatar