java用户输入菜单无法选择选项

时间:2017-02-02 15:12:26

标签: java

仍然在学校学习Java,但我正在开发一个用户输入选项的程序。 1,2,3,4。问题是我选择的其中一个选项,oddEvenZero(选项2),并没有结束。程序一直要求用户输入一个整数,并且不显示结果,也不会带回菜单。感谢。

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;


  }
  }

1 个答案:

答案 0 :(得分:2)

while 周期中,您应该逐个取大数字。 将除法提醒设置为10将为您提供数字的最后一位数字。但是你的下一个数字应该是不一样的,但是这个数字的除法的整数部分为10.另外你传递给方法的变量在外面是不可用的,所以如果你想打印它们 - 直接在你的方法中做,或者以某种方式返回它们(你最需要它的一些对象)。试试这个:

  private static int optionOne(int integer, int evenCount, int oddCount, int zeroCount)
  {
      int test = integer;
      int temp = test; 
      do 
      {
          test = temp % 10;
          temp = temp / 10;

          System.out.println("Current temp number: " + temp);
          System.out.println("Current test digit: " + test);
          if (test==0)
          {
             zeroCount++;
          }
          else if (test%2==0)
          {
             evenCount++;
          }
          else
          {
             oddCount++;
          }

      } while (temp > 0 );
      System.out.printf("Even: %d Odd: %d Zero: %d", evenCount, oddCount, zeroCount);
      return integer % 10; // do you really need it?
  }