2015-10-14 144 views
-1

我想使用switch語句而不是一系列if/elses,但是當我運行代碼時只有前兩種情況正常運行而其他則被忽略。我用if條件測試過這些方法,它工作正常,但我想使用switch語句。 (一些方法我還沒有得到,因此評論)switch case does not work,instead of methods instead

輸入:A 25
輸出:25被添加到袋。
輸入:F 25
輸出:袋中有(1)25個。
輸入:S
輸出:(無)
預期輸出:Bag中有1個數字。
輸入:M
輸出:(無)
預期輸出:在袋的最小數爲25

import java.util.Scanner; 

public class Bag { 

int index = 0; 
int[] array = new int[50]; 

public static void main(String[] args) { 
    int x = 0; 
    Bag bag = new Bag(); 

    Scanner scan = new Scanner(System.in); 

    while (x == 0) { 
    System.out.print("Add(A), Delete(D), Find(F), Size(S), Min(m), Max(M), List(L), Quit(Q) >> "); 
    char c = scan.next().charAt(0); 
    int y = scan.nextInt(); 
    switch(c) { 
    case 'A': bag.Add(y); 
    break; 
    //case 'D': bag.Delete(y); 
    //break; 
    case 'F': bag.Find(y); 
    break; 
    case 'S': bag.Size(); 
    break; 
    case 'm': bag.Min(); 
    break; 
    case 'M': bag.Max(); 
    break; 
    /*case 'L': bag.List(); 
    break; 
    case 'Q': bag.Quit();*/ 
    } 

} 
} 
public void Add(int y) { 
    array[index] = y; 
    index++; 
    System.out.println(" " + y + " is added to the Bag. "); 
} 
    public void Find(int y) 
    { 
    int count = 0; 
    for (int i = 0; i < array.length; i++) { 
     if (array[i] == y) { 
      count++; 
     } 
     } 
     System.out.println(" There is (" + count + ") " + y 
       + " in the Bag."); 

      } 
public void Size() { 
    System.out.println(" There are " + index + " numbers in the Bag."); 
} 

public void Min() { 
    int min = array[0]; 
    for (int i = 1; i < index; i++) { 
     if(min > array[i]) { 
      min = array[i]; 
     } 
    } 
    System.out.println(" The minimum number in the Bag is " + min + "."); 
} 
public void Max() { 
    int max = array[0]; 
    for (int i = 1; i < index; i++) { 
     if(max < array[i]) { 
      max = array[i]; 
     } 
    } 
    System.out.println(" The minimum number in the Bag is " + max + "."); 
} 

} 
+0

您的輸入是什麼?爲什麼它不「工作」? –

+0

控制檯中有什麼錯誤? – brso05

+1

當我輸入任何字母(在本例中爲m,M或S)時,請不要回傳任何東西,但是當我輸入A或F以及一個數字時,它可以正常工作。請輸入輸入,輸出和期望輸出 – Rehman

回答

0

看看你的代碼。

char c = scan.next().charAt(0); //Here you first expect char (program waiting for a input) 
int y = scan.nextInt(); //And here you expect int (program waiting for a input) 

我這樣的事情應該工作。

System.out.print("Add(A), Delete(D), Find(F), Size(S), Min(m), Max(M), List(L), Quit(Q) >> "); 
char c = scan.next().charAt(0); 
switch (c) { 
    case 'A': 
     int y = scan.nextInt(); // here you need next input 
     bag.Add(y); 
     break; 
    case 'F': 
     int y = scan.nextInt(); // here you need next input 
     bag.Find(y); 
     break; 
    case 'S': 
     bag.Size(); 
     break; 
    case 'm': 
     bag.Min(); 
     break; 
    case 'M': 
     bag.Max(); 
     break; 
} 
+0

這兩種情況下的相同變量名稱都會導致編譯時間錯誤。如果'F'中的int y被改變爲其他變量名稱,這個代碼將是一個可能的解決方案。 – Gobinath

+0

這正是發生了什麼,並將第二個變量改爲z似乎已經工作。謝謝大家,我現在更瞭解這一點! – imaginedrragon

2

你常讀一個int,即使在不需要的情況下他們,像 「M」, 「M」 或「S。如果你輸入M ,將打印的最大等

0

你的問題在於您的來電scan.nextInt()每次,即使你選擇的選項並不需要第二個參數。

Scanner方法如next(),nextInt()等,將始終掛起控制檯等待您的輸入。由於它總是需要兩個輸入,所以你錯過了第二個參數。這就是爲什麼它沒有打印任何東西。

while (x == 0) { 
    System.out.print("Add(A), Delete(D), Find(F), Size(S), Min(m), Max(M), List(L), Quit(Q) >> "); 
    char c = scan.next().charAt(0); 
    int y = 0; 
    switch (c) { 
     case 'A': 
      y = scan.nextInt(); 
      bag.Add(y); 
      break; 
     //case 'D': bag.Delete(y); 
     //break; 
     case 'F': 
      y = scan.nextInt(); 
      bag.Find(y); 
      break; 
     case 'S': 
      bag.Size(); 
      break; 
     case 'm': 
      bag.Min(); 
      break; 
     case 'M': 
      bag.Max(); 
      break; 
    } 
}