簡體   English   中英

切換大小寫不起作用,而是忽略方法

[英]switch case doesn't work, ignores methods instead

我試圖使用switch語句而不是一系列的if / elses,但是當我運行代碼時,只有前兩種情況可以正常運行,而其他情況則被忽略。 我已經用if條件測試了方法,並且效果很好,但是我想使用switch語句。 (有些方法我還沒有得到注釋)

輸入:A 25
輸出:25被添加到Bag中。
輸入:F 25
輸出:袋子中有(1)25個。
輸入:S
輸出:(無)
預期的輸出:袋子中有1個數字。
輸入:米
輸出:(無)
預期輸出:袋子中的最小數量為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 + ".");
}

}

即使在不需要它們的情況下(例如“ m”,“ M”或“ S”),您總是在讀取int值。如果您鍵入M <return> 0 <return> ,它將打印出最大值,等等

看你的代碼。

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;
}

您的問題出在每次調用scan.nextInt() ,即使您選擇的選項不需要第二個參數也是如此。

諸如next()nextInt()等的Scanner方法將始終掛起控制台,等待您的輸入。 由於它總是需要兩個輸入,因此您缺少第二個參數。 這就是為什么它什么都不打印的原因。

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;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM