Witam. Otóż napisałem prosty programik który miał generować hasła. Niestety zamiast wyświetlać całych haseł wyświetla tylko jedną literę. A poza tym nie chce generować znaków bo wywala jakiś błąd. Może ktoś wie gdzie jest błąd ?!
import java.util.*;
public class Main {
static Random rand = new Random();
static char alfabet_small[] = {'a','b','c','d','e','f','g','h','i','j','k','l',
'm','n','o','p','r','s','t','u','w','x','y','z'};
static char alfabet_big[] = {'A','B','C','D','E','F','G','H','I','J','K','L',
'M','N','O','P','R','S','T','U','W','X','Y','Z'};
static char chars[] = {'!','@','#','$','%','^','&','*','<','>','?','Q'};
static int al_s;
static int al_b;
static int ch;
static int max_char;
static char pass;
public static void rand_al_s() {
for(int i = 0; i < al_s; i++){
int r;
r = rand.nextInt((24)+1);
pass = alfabet_small[r];
}
}
public static void rand_al_b() {
for(int i = 0; i < al_b; i++){
int r;
r = rand.nextInt((24)+1);
pass = alfabet_big[r];
}
}
/*public static void rand_ch() {
for(int i = 0; i < ch; i++){
int r;
r = rand.nextInt((12)+1);
pass = chars[r];
}
}*/
@SuppressWarnings("resource")
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Podaj dlugosc hasla");
max_char = input.nextInt();
al_s = rand.nextInt((max_char)+1);
al_b = rand.nextInt((max_char - al_s)+1);
ch = max_char-(al_s+al_b);
rand_al_s();
rand_al_b();
//rand_ch();
System.out.println("Twoje haslo to: " + pass);
}
}
A, i jeszcze jedno pytanie czy lepiej użyć klasy math czy rand.