-7

我需要北、南、东和西中的最高值,结果,我只需要返回北、南、东或西的名称。必须通过比较北、南、东、西的所有值来获得最大值。

下面是我的代码片段。

public String startpgm(String x){
    String result=null;
    double priority = 0;
    double North    = 1;
    double South    = 3;    
    double East     = 4;
    double West     = 5;

    System.out.println("Priority:"+Math.max(North, South)); //I am getting the max value, what i need is North or South!!
    priority= Math.max(North, South);
    result = Double.toString(priority);
    return result ;
}
4

7 回答 7

3

由于基点是一组众所周知的值,因此请使用 enum :

public enum CardinalPoint {
  NORTH,
  SOUTH,
  EAST,
  WEST
}

然后,这里是max方法的一个例子:

public static CardinalPoint max(CardinalPoint cp1, CardinalPoint cp2) {
  return cp1.ordinal() > cp2.ordinal() ? cp1 : cp2;
}

像这样使用:

public static void main(String[] args) {
  System.out.println(max(CardinalPoint.NORTH, CardinalPoint.SOUTH));
}

结果 :

NORTH
于 2016-01-12T09:42:11.900 回答
1

使用hashmapenum。这是算法的更好方法。无论如何,如果您想使用变量:

class Constants {

    public static final int priority = 0;
    public static final int North    = 1;
    public static final int South    = 3;    
    public static final int East     = 4;
    public static final int West     = 5;
  public static String getConstantName(int constVal) {
    if (constantNames == null) {
      Map<Integer, String> cNames = new HashMap<Integer, String>()
      for (Field field : MyClass.class.getDeclaredFields()){
        if ((field.getModifiers() & (Modifier.FINAL | Modifier.STATIC)) != 0) {
            && int.class == field.getType()){
          // only record final static int fields
          cNames.put((Integer)field.get(null), field.getName());
        }
      }
      constNames = cNames;
    }
    return constantNames.get(constVal);
  }
public String startpgm(String x){
    String result=null;

    System.out.println("Priority:"+Math.max(North, South)); //I am getting the max value, what i need is North or South!!
    priority= Math.max(North, South);
    result = Integer.toString(priority);
    return Constants.getConstantName(result) ;
}
}

谢谢:

https://stackoverflow.com/a/6838811/1979882

于 2016-01-12T09:35:45.887 回答
1

将名称存储在 a 中Map,然后检索键值的名称。

String result=null;
double priority = 0;
double North    = 1;
double South    = 3;    
double East     = 4;
double West     = 5;

Map<Double, String> names = new HashMap();
names.put(North, "North");
names.put(South, "South");
names.put(East, "East");
names.put(West, "West");

System.out.println("Priority:"+Math.max(North, South));
priority= Math.max(North, South);
result = names.get(priority);

return result ;
于 2016-01-12T09:38:00.787 回答
1

这是假设只有 4 个变量要跟踪。

String[] sValue = {"North","South","East","West"};
int[]    iValue = {1,2,3,4,5}

index = Math.max(North, South);
System.out.println("Priority:"+sValue[index]); 

注意:此解决方案不能很好地扩展。

于 2016-01-12T09:40:28.050 回答
1

如果您需要命名实体作为方向,您可能希望使用枚举。

enum Direction { North, South, East, West }

您可以使用 将每个方向与一个数字相关联EnumMap

Map<Direction, Double> dirMap = new EnumMap<>(Direction.class);
dirMap.put(Direction.North, 1.0);
dirMap.put(Direction.South, 3.0);
dirMap.put(Direction.East, 4.0);
dirMap.put(Direction.West, 5.0);

你可以找到北和南的最大值,如下所示:

private static Direction findMax(List<Direction> directions, Map<Direction, Double> dirMap) {
    Direction best = null;
    Double bestValue = null;
    for (Direction d : directions) {
        double dValue = dirMap.get(d);
        if (bestValue==null || bestValue < dValue) {
            best = d;
            bestValue = dValue;
        }
    }
    return best;
}

...
System.out.println("max of north and south is "+findMax(Arrays.asList(Direction.North, Direction.South), dirMap));
于 2016-01-12T09:43:56.553 回答
0

当前编写代码的方式,您需要使用反射来获得所需的内容。您最好使用 Java 枚举类型 ( https://docs.oracle.com/javase/tutorial/java/javaOO/enum.html ),它可以同时为您提供数值及其字符串表示形式。

于 2016-01-12T09:37:25.557 回答
0

您可以将枚举与私有构造函数一起使用,现在您可以为每个基点分配一个不同的双精度值,但您需要一个获取该值的公共方法。现在你只需要一个方法来查找基点的名称,我在枚举中使用了一个静态方法。

公共类 Startpgm {

public static void main(String[] args) {


    System.out.println("priority: "+ 
            CardinalPoints.nameCardinalPoint(Math.max(CardinalPoints.NORTH.getPriority(), CardinalPoints.SOUTH.getPriority())));


}

enum CardinalPoints {
    NORTH(1d), SOUTH(3d), EAST(4d), WEST(5d);

    double priority;

    private CardinalPoints (double priority){
        this.priority= priority;
    }
    public double getPriority(){
        return priority;
    }
    public static String nameCardinalPoint( double priority){
        String nameCardinalPoint = "";
        for (CardinalPoints cardinalPoint : CardinalPoints.values()) {
            if(cardinalPoint.getPriority() == priority){
                nameCardinalPoint = cardinalPoint.name();
            }
        }
        return nameCardinalPoint;
    }
}

}

于 2016-01-12T10:29:07.057 回答