This program is a game of "Roulette", the player must bet on red or black, pair or not pair, the problem is this, the player always wins if he writes string = "Red" Black "" Pair "Unpaired", for example, the player wrote that puts on Red, and black fell out, but the player won anyway, I understand the reason, but I can’t fix it because I don’t know how, I’m familiar with java for 3 days. The problem is in this block of code:

if (stavka.equals("Красное")) { System.out.println("Ставка сыграла, вы победили!Выиграшь составляет:" + summa * 2); } else if (stavka.equals("Черное")) { System.out.println("Ставка сыграла, вы победили!Выиграшь составляет:" + summa * 2); } else if (stavka.equals("Не четное")) { System.out.println("Ставка сыграла, вы победили!Выиграшь составляет:" + summa * 2); } else if (stavka.equals("Четное")) { System.out.println("Ставка сыграла, вы победили!Выиграшь составляет:" + summa * 2); } else 

A player wins if he writes any of the options listed above, but it must be done so that he would win only if his bet coincides with what fell out.

 import java.util.Scanner; import java.util.Random; public class Bender { static boolean playAgain = true; public static void main(String[] args) { while (playAgain) { Game(); } } public static void Game() { Scanner scanner = new Scanner(System.in); int summa; String stavka; System.out.println("Здравствуйте в рулетке, на что ставите?Красное или Черное?Парное или не Парное?"); stavka = scanner.next(); System.out.println("Какую сумму ставите?"); summa = scanner.nextInt(); for (int i = 0; i < 1; i++) { int b; String[] colour = {"Черное.", "Красное."}; Random random = new Random(); Random random1 = new Random(); int select = random.nextInt(colour.length); b = (random1.nextInt(36)); if (b == 0) { System.out.print(""); } else if ((b & 1) == 0) { System.out.print(" Выпало четное" + " "); } else { System.out.print("Выпало нечетное" + " "); } System.out.println(b + " " + colour[select]); } if (stavka.equals("Красное")) { System.out.println("Ставка сыграла, вы победили!Выиграшь составляет:" + summa * 2); } else if (stavka.equals("Черное")) { System.out.println("Ставка сыграла, вы победили!Выиграшь составляет:" + summa * 2); } else if (stavka.equals("Не четное")) { System.out.println("Ставка сыграла, вы победили!Выиграшь составляет:" + summa * 2); } else if (stavka.equals("Четное")) { System.out.println("Ставка сыграла, вы победили!Выиграшь составляет:" + summa * 2); } else playAgain = false; System.out.println("Вы проиграли,делаем ставку еще раз?"); String otvet; otvet = scanner.next(); if (otvet.equals("Да")) { System.out.println("Продолжаем!"); playAgain = true; } if (otvet.equals("Нет")) { System.out.println("Жаль(До Встречи!"); } } } 
  • And where do you compare with what fell out? - JVic
  • Use the logical operator And && : stavka.equals("Чётное") && (b&1==0) - vp_arth

1 answer 1

I was going to place the full version in the last question, but once a new question has appeared, I will write here.

  1. For correct comparison, you need to consider the encoding of the data coming from System.in . In my example, I used the CP866 to work from the Windows console.
  2. stavka should be stavka with colour[select] ( colors[colorIndex] in my code) and with "Четное" and "Нечетное" depending on the value of b ( number ). It is possible to compare parity by analogy with colors by creating an array with odd / even ( parity ).
  3. Apparently, you still need to compare stavka with b (if you can bet on a specific number), but this is not in the source code.

Full code:

 public static void main(String[] args) { String[] colors = { "Черное", "Красное" }; String[] parity = { "Четное", "Нечетное" }; Random random = new Random(); Scanner scanner = new Scanner(System.in, "CP866"); boolean isPlaying = true; while (isPlaying) { System.out.println("Сделайте вашу ставку. Вы можете поставить на Красное или Черное, на Четное или Нечетное."); String stavka = scanner.next(); System.out.println("Какую сумму ставите?"); int summa = scanner.nextInt(); int colorIndex = random.nextInt(colors.length); int number = random.nextInt(36); System.out.println("Выпало " + parity[number % 2] + " " + number + " " + colors[colorIndex]); if (stavka.equals(colors[colorIndex]) || stavka.equals(parity[number % 2])) { System.out.println("Ставка сыграла, вы победили! Выигрыш составляет: " + summa * 2); } else { System.out.println("Вы проиграли"); } System.out.println("Делаем ставку еще раз?"); String otvet = scanner.next(); if (otvet.equals("Да")) { System.out.println("Продолжаем!"); } else { System.out.println("Жаль. До встречи!"); isPlaying = false; } } } 

The check for the correctness of the stavka value occurs in this line:

 if (stavka.equals(colors[colorIndex]) || stavka.equals(parity[number % 2])) 
  • In any case, the bet is not correct, even if I put black and black falls out, it still writes the bet is not played ( - Roman
  • @Roman decide on the encoding used in the console from which you run the application. Perhaps, instead of CP866 you need to use Windows-1251 or UTF-8 . The code itself works correctly (if, of course, to write in the console strictly as specified, that is, Черное , not черное ). - Regent
  • Thanks, it helped! Everything worked as it should. If it’s not difficult, please explain how the encoding affected the work of the program, why after I changed the CP866 to utf-8 did it all work? - Roman
  • @Roman on health. Because in each console one or another encoding is used. Somewhere somewhere, somewhere else. In the Windows console, for example, comes CP866 . In the NetBeans console, Windows-1251 . You have some other console in general UTF-8 . At this point, who (from the creators of the consoles) is ready for anything? We have to adapt to a specific console for correct processing of the entered Russian letters. And if the answer suits you, do not forget to mark it as appropriate (checkmark to the left of the answer). - Regent