2014-01-05 59 views
1

您好,
我是一個java新手,我有這個代碼的問題。我想如果是在環什麼的問題..循環中的一些麻煩

public static void main(String[] args) { 
    try { 
     Scanner scn = new Scanner(System.in); 

     int z = 0; 
     String input; 
     int[] ABArray = new int[2]; 

     while (z == 0) { 
      System.out.println("Input X to terminate."); 
      System.out.print("Input: "); 
      input = scn.nextLine().toLowerCase(); 

      for (int i = 0; i < input.length(); i++) { 
       char AB = input.charAt(i); 

       ABArray[AB - 'a']++; 
      } 

      if (ABArray[0] == 0 || ABArray[1] == 0) { 
       System.out.println("Not Equal"); 
       System.out.println(""); 

      } else if (ABArray[0] == ABArray[1]) { 
       System.out.println("Equal"); 
       System.out.println(""); 
      } else if (ABArray[0] != ABArray[1]) { 
       System.out.println("Not Equal"); 
       if (ABArray[0] > ABArray[1]) { 
        System.out.println("The number of A is greater than B."); 
       } else if (ABArray[0] < ABArray[1]) { 
        System.out.println("The number of B is greater than A."); 
       } 
       System.out.println(""); 
      } 
     } 
    } catch (ArrayIndexOutOfBoundsException X) { } //Terminates the program 

} 

問題是這樣的
I/O

Input:      
ABABAB 

Output:        
Equal 

Input:       
AABBB 

Output:        
Not Equal 
The number of B is greater than A. 

Input:         
AABB //It is equal. 

Output:      
Not Equal //It says not. 
The number of B is greater than A. 

正如你看到的,問題是當我在第一個輸入時等於A和B,它說相等,當我輸入不等於A和B時,但在第三個輸入等於A和B時它說不等於。

問題已解決。 感謝您的幫助。

+0

什麼是你的程序的目的是什麼? – ADTC

+0

@ADTC檢查A和B的數量是否相等。 – user3122474

+0

你如何做到這一點? (算法)*請用這個信息擴大你的問題* – ADTC

回答

1

每當您開始在while循環內工作時,您必須將ABArray中的所有值設置爲零。在第三次啓動while循環(使用AABB輸入)時,您仍然保留循環的上一次運行留下的值 - 5位於數組的0索引元素中,而6位於1索引元素中的數組,因此程序會給你錯誤的輸出。

0

您如何簡單地將輸入讀入到字符串中,然後遍歷它,計算您感興趣的每個字符的出現次數(這裏是'a'和'b'),檢查它們的計數是否相等?因此,這適用於例如:

public static void main(String[] args) { 
     try { 
      Scanner scanner = new Scanner(System.in); 
      String input; 

      while (true) { 
       System.out.println("Input X to terminate."); 
       System.out.print("Input: "); 
       input = scanner.nextLine().toLowerCase(); 
       if (input.equals("X")) { 
        break; 
       } else { 
        int countA = 0; 
        int countB = 0; 
        for (int i = 0; i < input.length(); i++) { 
         if (input.charAt(i) == 'a') { 
          countA++; 
         } else if (input.charAt(i) == 'b') { 
          countB++; 
         } 
        } 
        if (countA == countB) { 
         System.out.println("Equal!"); 
        } else { 
         System.out.println("Not equal!"); 
        } 
       } 
      } 
     } catch (ArrayIndexOutOfBoundsException e) // Terminates the program 
     { 

     } 

    } 
0

您可以使用此代碼:

public static void main(String[] args) 
{ 
    try 
    { 
     Scanner scn = new Scanner(System.in); 

     int z=0; 
     String input; 
     int[] ABArray = null; 

     while(z==0) 
     { 
      ABArray = new int[2]; 
      System.out.println("Input X to terminate."); 
      System.out.print("Input: "); 
      input=scn.nextLine().toLowerCase(); 

      for (int i = 0; i < input.length(); i++) 
      { 
       char AB=input.charAt(i); 

       ABArray[AB-'a']++; 

      } 

      if(ABArray[0]==0||ABArray[1]==0) 
      { 
       System.out.println("Not Equal"); 
       System.out.println(""); 

      } 
      else if(ABArray[0]==ABArray[1]) 
      { 
       System.out.println("Equal"); 
       System.out.println(""); 
      } 
      else if(ABArray[0]!=ABArray[1]) 
      { 
       System.out.println("Not Equal"); 
       if(ABArray[0]>ABArray[1]) 
       { 
        System.out.println("The number of A is greater than B."); 
       } 
       else if(ABArray[0]<ABArray[1]) 
       { 
        System.out.println("The number of B is greater than A."); 
       } 
       System.out.println(""); 
      } 
     } 
    } 
    catch(ArrayIndexOutOfBoundsException X) //Terminates the program 
    { 
     X.printStackTrace(); 
    } 

}