2017-02-02 6 views
1

のオプションを選択できませんが、ユーザーがオプションを入力するプログラムで作業しています。 1,2,3,4。問題は、選択したオプションの1つであるoddEvenZero(オプション2)が終了しないことです。プログラムは、ユーザに整数を入力するように依頼し続け、結果を表示せず、メニューも戻さない。ありがとう。あなたのしばらくサイクルでjavaユーザー入力メニューでは、まだ学校でJavaを学習している

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; 


    } 
    } 

答えて

2

あなたは1であなたの大きなナンバーワンの数字を取る必要があります。 分割を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? 
    } 
関連する問題