Přidat otázku mezi oblíbenéZasílat nové odpovědi e-mailemVyřešeno Cyklus for problém

String farba[] = new String[pocet];

for (int i = 1; i <= pocet; i++) {
System.out.println("Zadaj " + i + ". farbu: ");
farba[i] = scan.nextLine();
}

PREČO MI TO VYPÍŠE TOTO:

Kolko farieb chces zadat: (čaká) 3

Zadaj 1. farbu: (nečaká)
Zadaj 2. farbu: (čaká)

Prečo mi vypíše dva riadky naraz?

Předmět Autor Datum
Zadávání toho počtu je kde?
Wikan 18.01.2014 22:21
Wikan
public static void main(String[] args) { Scanner scan = new Scanner(System.in); String farby[] = {"b…
Nighthorse 18.01.2014 22:31
Nighthorse
http://stackoverflow.com/questions/13102045/skippi ng-nextline-after-use-nextint
Wikan 18.01.2014 22:35
Wikan
díky :) poslední
Nighthorse 18.01.2014 22:43
Nighthorse

public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String farby[] = {"biela", "modra", "cervena"};
System.out.print("Kolko farieb chces zadat: ");
int pocet = scan.nextInt();
String farba[] = new String[pocet];

for (int i = 0; i < pocet; i++) {
System.out.print("Zadaj " + (i+1) + ". farbu: ");
farba[i] = scan.nextLine();
System.out.println();
}
}

Kolko farieb chces zadat: (čaká) 3

Zadaj 1. farbu: (nečaká)
Zadaj 2. farbu: (čaká)

Prečo mi vypíše dva riadky naraz?

Zpět do poradny Odpovědět na původní otázku Nahoru