带有空字符串的java switch语句

带有空字符串的java switch语句,java,string,switch-statement,Java,String,Switch Statement,我必须根据对象的面积、左边框或小写字母对对象进行排序。当我想在左边或下边框对它们进行排序时,我必须说sort x和sort y。当我想按区域排序时,我需要说只是排序。我试图通过一个switch方法来实现这一点,但我不知道如何使用一个包含空字符串的switch方法。这就是我想做的: case "sort": System.out.println("On what do you want to sort?"); String choice = scanner.nextLine(); sw

我必须根据对象的面积、左边框或小写字母对对象进行排序。当我想在左边或下边框对它们进行排序时,我必须说sort x和sort y。当我想按区域排序时,我需要说只是排序。我试图通过一个switch方法来实现这一点,但我不知道如何使用一个包含空字符串的switch方法。这就是我想做的:

case "sort":
  System.out.println("On what do you want to sort?");
  String choice = scanner.nextLine();
  switch (choice) {
    case "x":
      Arrays.sort(g, 0, lastPos, new Comparator < Geometric > () {
        @Override
        public int compare(Geometric o1, Geometric o2) {
          if (o1.leftBorder() < o2.leftBorder()) {
            return -1;
          } else if (o1.leftBorder() > o2.leftBorder()) {
            return 1;
          } else {
            return 0;
          }
        }

      });
      break;
    case "y":
      Arrays.sort(g, 0, lastPos, new Comparator < Geometric > () {
        @Override
        public int compare(Geometric o1, Geometric o2) {
          if (o1.bottomBorder() < o2.bottomBorder()) {
            return -1;
          } else if (o1.bottomBorder() > o2.bottomBorder()) {
            return 1;
          } else {
            return 0;
          }
        }

      });
      break;
    case (""):
      Arrays.sort(g, 0, lastPos, new Comparator < Geometric > () {
        @Override
        public int compare(Geometric o1, Geometric o2) {
          if (o1.area() < o2.area()) {
            return -1;
          } else if (o1.area() > o2.area()) {
            return 1;
          } else {
            return 0;
          }
        }

      });
      break;
    default:
      System.out.println("test1");
  }
案例“排序”:
System.out.println(“您想要排序什么?”);
字符串选择=scanner.nextLine();
开关(选择){
案例“x”:
排序(g,0,lastPos,新的比较器(){
@凌驾
公共整数比较(几何o1,几何o2){
如果(o1.leftBorder()o2.leftBorder()){
返回1;
}否则{
返回0;
}
}
});
打破
案例“y”:
排序(g,0,lastPos,新的比较器(){
@凌驾
公共整数比较(几何o1,几何o2){
如果(o1.bottomBorder()o2.bottomBorder()){
返回1;
}否则{
返回0;
}
}
});
打破
案例(“”):
排序(g,0,lastPos,新的比较器(){
@凌驾
公共整数比较(几何o1,几何o2){
如果(o1.area()o2.area()){
返回1;
}否则{
返回0;
}
}
});
打破
违约:
System.out.println(“test1”);
}

订购代码(比如使用工厂作为比较器)的更好方法是,但只要坚持使用您的代码,我认为枚举就可以解决您的问题

public enum CHOICES {
    X, Y, AREA
}

private static CHOICES getChoice(String choice) {
    if (choice == null || choice.trim().length() == 0) {
        return CHOICES.AREA; 
    }
    try {
        return CHOICES.valueOf(choice.toUpperCase());   
    } catch (Exception e) {
        // instead to check the value, 
        // if the input fir on, if not just catch the exception and return null
        return null;
    }       
}
然后你可以像下面那样更换你的开关

        //String choice = scanner.nextLine();
    CHOICES choice = getChoice(scanner.nextLine());     
    switch (choice) {       
    case X:
        //sort by X
        break;
    case Y:
        //sort by Y
        break;
    case AREA:
        //sort by area
        break;
    default:
        System.out.println("test1");
    }

最好的方法是对代码进行排序(比如使用一个工厂作为比较器),但只要坚持使用您的代码,我认为枚举就可以解决您的问题

public enum CHOICES {
    X, Y, AREA
}

private static CHOICES getChoice(String choice) {
    if (choice == null || choice.trim().length() == 0) {
        return CHOICES.AREA; 
    }
    try {
        return CHOICES.valueOf(choice.toUpperCase());   
    } catch (Exception e) {
        // instead to check the value, 
        // if the input fir on, if not just catch the exception and return null
        return null;
    }       
}
然后你可以像下面那样更换你的开关

        //String choice = scanner.nextLine();
    CHOICES choice = getChoice(scanner.nextLine());     
    switch (choice) {       
    case X:
        //sort by X
        break;
    case Y:
        //sort by Y
        break;
    case AREA:
        //sort by area
        break;
    default:
        System.out.println("test1");
    }

代码有点凌乱,但是空字符串开关分支是可以的。开关是可以的,尽管我去掉了括号。那么,您真正的问题是什么?一些错误、一些奇怪的行为或……?为了获得更好的代码,我建议使用枚举并在其中编写方法,也许您可以将
choice
转换为char。然后将其转换为int并比较字符代码。也许你得到的是像回车或换行之类的东西。。代码有点凌乱,但是空字符串开关分支是可以的。开关是可以的,尽管我去掉了括号。那么,您真正的问题是什么?一些错误、一些奇怪的行为或……?为了获得更好的代码,我建议使用枚举并在其中编写方法,也许您可以将
choice
转换为char。然后将其转换为int并比较字符代码。也许你得到的是像回车或换行之类的东西。。