Я думаю, что вам просто нужно использовать while
l oop, чтобы добиться цели, и просто добавить переменную count
, чтобы отобразить, сколько попыток вам понадобилось, чтобы заставить ГСЧ угадать ваш номер. Вот решение, основанное на опубликованном вами коде.
import java.util.Scanner;
import java.util.Random;
public class RoughNums{
public static void main(String[]args){
Scanner reader = new Scanner(System.in);
Random rand = new Random();
int rantInt = rand.nextInt(101);
int pick;
while(true) {
System.out.println("Hello friend! Pick a number between 1 - 100: ");
pick = Integer.parseInt(reader.nextLine());
if(pick > 100){
System.out.println("Oops, number too big! Please try again!\n");
} else if(pick < 1){
System.out.println("Oops, number too small! Please try again!\n");
}
else {
System.out.println("Let's see if I can get the same number too!");
break;}
}
int count = 0;
while (rantInt != pick){
count++;
rantInt = rand.nextInt(101);
}
System.out.println( pick + " was found using " + count + " tries.");
}
}