2015-10-14 100 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; 
    } 
}