Java Tutorial - 5 - While Loops && Infinite Loops

preview_player
Показать описание
Java programming tutorial

Eclipse, Simple Application Development

Рекомендации по теме
Комментарии
Автор

while(true) {
System.out.println("what do you want to calculate?");
System.out.println("input 'P' for Permutation and 'C' for Combination");

choice = sc.nextLine();

if((choice == "C") || (choice == "c")) {
while(true) {
System.out.println("Preparing Combination Program...");

System.out.print("How many things are there (total): ");
n = sc.nextInt();
System.out.print("How many thing(s) is/are being chosen at a time?:");
r = sc.nextInt();
if(n < r) {
System.out.println("Error: The total amount must be larger than the amount of pick");
}

In this code I ask the user input (using the string variable named "choice"). Assuming I have imported scanner and made a Scanner object named sc. The problem I have is that the code infinitely looped until the "choice = sc.nextLine();
" part. The "if" and the other part didn't get executed. The code kept on asking for inputs even if I have already entered the input and seems to stuck on the first input part. Is there something wrong with the loop?

timecookie
welcome to shbcf.ru