New course! Every coder should learn Generative AI!
Try a free lesson0
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"); } } } }
14 Answers
+ 4
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".
+ 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..?
+ 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)...
+ 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");
}
}
}
}
+ 1
This code doesnt work please help
+ 1
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"
+ 1
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");
}
}
0
anyone solve astonomy Board game
0
I hate the case sensitive answers. why do they have to be so annoying?
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.
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 {
String name;
abstract String getName();
abstract void play();
}
class Monopoly extends Game {
//give "Monopoly" name to game
String getName() {
name = "Monopoly";
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() {
name = "Chess";
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() {
name = "Battleships ";
return name;
}
// play method should print "Sink all ships."
void play() {
System.out.println("Sink all ships.");
}
}
- 1
change the x to i
it will works
- 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.
- 1
Yes, I actually got that while back and nearly screamed with frustration. :D But thank you. :)