如何在java中获取具有最高编号的变量名?

时间:2016-01-12 09:33:16

标签: java math max highest

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

以下是我的代码段。

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

7 个答案:

答案 0 :(得分:3)

由于基点是一组众所周知的值,请使用枚举:

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

答案 1 :(得分: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

答案 2 :(得分:1)

将名称存储在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 ;

答案 3 :(得分: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]); 

注意:此解决方案无法很好地扩展。

答案 4 :(得分: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));

答案 5 :(得分:0)

目前编写代码的方式,您需要使用反射来获得您想要的内容。您最好使用Java Enum Types(https://docs.oracle.com/javase/tutorial/java/javaOO/enum.html),它可以同时为您提供数值和字符串表示。

答案 6 :(得分:0)

您可以将enum与私有构造函数一起使用,现在您可以将diffetent double值设置为每个基数点,但是您需要一个获取该值的公共方法。现在你只需要一个方法来找到基点的名称,我在枚举中使用静态方法。

公共课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;
    }
}

}