The program must count the number of attempts it took the user to guess the number. And at the end to report how many attempts there should also prompt the user that he was mistaken in the sign if the program made a positive number, and the user entered a negative one. And vice versa.

The program should make an integer from the interval from [−10; 10], excluding zero. At the same time, try to ensure that the distribution of random numbers generated by the program is uniform (that is, in the case of zero falling out, you cannot simply replace it with some other suitable number, for example, 1, because then 1 will be twice as likely as the remaining numbers).

import java.util.Scanner; import java.lang.Math; public class Main { public static void main(String[] args) { double prog, user; prog = Math.floor(Math.random() * (10 - 10) + 10); System.out.println("Я загадала число от -10 до 10, отгадайте его."); do { System.out.print("Вводите ваше число: "); Scanner input = new Scanner(System.in); user = input.nextDouble(); if(user == prog) { System.out.println("Вы угадали!"); } else { if ( user == Math.floor(user) && user > -10 && user < 10) { System.out.print("Вы не угадали! "); if( prog < user ) { System.out.println("Моё число меньше."); } else { System.out.println("Моё число больше."); } } else { System.out.println("Вы ввели не подходящее число!"); } } } while( user != prog ); System.out.println("До свиданья!"); } } 

Tell me what's wrong in the loop that counts the number of attempts and displays their result on the screen in the user-entered, I can not understand what is wrong?

Displays the wrong result.

Here I did not alter the result of the program

 import java.util.Scanner; import java.lang.Math; public class Main { public static void main(String[] args) { double prog, user; prog = Math.floor(Math.random() * 20 - 10); System.out.println("Я загадала число от -10 до 10, отгадайте его."); do { System.out.print("Вводите ваше число: "); Scanner input = new Scanner(System.in); user = input.nextDouble(); { for(int count = 0;count < user;count++){ if(user == prog) { System.out.println("Вы угадали!"); } else { if ( user == Math.floor(user) && user > -10 && user < 10) { System.out.print("Вы не угадали! "); if( prog < user ) { System.out.println("Моё число меньше."); } else { System.out.println("Моё число больше."); } } else { System.out.println("Вы ввели не подходящее число!"); } } System.out.println("Количество попыток " + count); } } } while( user != prog ); System.out.println("До свиданья!"); } } 

cycle

 for(int count = 0;count < user;count++) 

How can I fix it?

  • 2
    And what's the problem? - strbb
  • In order not to replace 0 with a known known number, it is necessary to make the guessing as a separate function. function getRandomInt () {prog = Math.floor (Math.random () * 21) - 10; return prog; } We guess an integer from 0 to 20 inclusive. And check when returning, if prog = 0, then guess again. Then the odds will be equal for all numbers (I wrote on a javascript just so that you understand the idea of ​​how to bypass the conceived zero). - Grimon
  • Tell me how to add code to the program? According to the task at the top? - turtles

2 answers 2

Careful reading for nextDouble ()!

What will happen if you enter not a number, but a letter, a point, a sign ???

Input errors must be properly processed, and set the counter of attempts that excites you before the cycle to zero and after System.out.print ("Enter your number:"); increment it, and along with "Goodbye!" print it out.

    I do not rummage in Java, I will assume:

     //prog = Math.floor(Math.random() * (10 - 10) + 10); // " * (10 - 10)" - это круто)) do { prog = Math.floor(Math.random() * 21) - 10; } while (prog == 0); 
    • The idea is just what he needs. - Grimon
    • Tell me how to continue to write the program. The program should count the number of attempts that the user took to guess the number. And at the end to report how many attempts there should also prompt the user that he was mistaken in the sign if the program made a positive number, and the user entered a negative one. And vice versa. - turtles
    • one
      @turtles, I don’t shave so precisely) In general, if you accepted the answers, you would have answered more often) - Sh4dow