noch lernen Java in der Schule, aber ich arbeite an einem Programm, wo der Benutzer eine Option eingibt. 1,2,3,4. Das Problem ist, dass eine der Optionen, die ich auswähle, oddEvenZero (Option 2), nicht endet. Das Programm fordert den Benutzer auf, eine ganze Zahl einzugeben und zeigt die Ergebnisse nicht an und bringt auch das Menü nicht zurück. Vielen Dank.Java Benutzereingabemenü kann keine Option wählen
import java.util.Scanner;
public class IntFun
{
public static void main(String[] args)
{
int option;
int integer;
int evenCount = 0, oddCount = 0, zeroCount = 0;
int optionOne;
Scanner kb = new Scanner(System.in);
System.out.println("Welcome to Integer Fun.");
System.out.println("Please enter a non-negative integer: ");
integer = kb.nextInt();
kb.nextLine();
while((integer < 0))
{
System.out.println("I am sorry that is not a non-negative integer.");
System.out.println("");
System.out.println("Please enter a non-negative integer: ");
integer = kb.nextInt();
}
option = displayMenu(kb);
while (option != 4)
{
switch (option)
{
case 1:
System.out.println("Option 1");
break;
case 2:
optionOne(integer, evenCount, oddCount, zeroCount);
System.out.printf("Even: %d Odd: %d Zero: %d", evenCount, oddCount, zeroCount);
break;
case 3:
System.out.println("Option 3");
break;
}
option = displayMenu(kb);
}
}
private static int displayMenu(Scanner kb)
{
int option = 0;
while (option != 1 && option != 2 && option != 3 && option != 4)
{
System.out.println("\t\t1. Enter a new number\n\t\t2. Print the number of odd digits, even digits and zeros in the integer\n\t\t3. Print the sum of the digits of the integer\n\t\t4. Quit the program");
option = kb.nextInt();
kb.nextLine();
if (!(option == 1 || option == 2 || option == 3 || option == 4))
System.out.println("Invalid choice");
}
return option;
}
private static int optionOne(int integer, int evenCount, int oddCount, int zeroCount)
{
while (integer > 0)
{
integer = integer % 10;
if (integer==0)
{
zeroCount++;
}
else if (integer%2==0)
{
evenCount++;
}
else
{
oddCount++;
}
}
return integer % 10;
}
}