java empty string,java switch statement with empty string

这篇博客探讨了如何使用Java对一组几何对象按其左边界、下边界或面积进行排序。通过switch语句实现用户输入的选择,分别对对象的x坐标、y坐标或面积进行比较并排序。示例代码展示了如何自定义Comparator来完成这个任务。
摘要由CSDN通过智能技术生成

I have to sort objects based on their area, leftborder or lowerborder. When I want to sort them on their left- or lower border I have to say sort x and sort y. When I want to sort by area I need to say just sort. I was trying to do this via a switch method but I don't know how you can use a switch method that has an empty string in it. This is what I was trying to do:

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");

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值