I'm currently trying to make a little bruteforcer for small numbers in java. We type an integer in the console and then with java Random we try to guess the number. The problem is in my while loop I always get the same random number.
How can I update this number everytime I run through the while loop? Is there a short way to do this?
import java.util.Scanner;
import java.util.Random;
public class Bruteforce{
public static void main(String[]args){
Scanner input = new Scanner(System.in);
System.out.println("Enter integer here: ");
int x = input.nextInt();
int length = (int)(Math.log10(x)+1);
Random rand = new Random();
int y = rand.nextInt(1000)+0;
while(x != y){
System.out.println(y);
}
}
}
Aucun commentaire:
Enregistrer un commentaire