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?