提问者:小点点

比较3个值,找出max,mid,min并打印最终结果


也许我比较错误,我有强烈的感觉,我犯了一个很简单的错误。 我以前用C++编程,现在学习使用Java.在第一个例子中。它不打印任何东西

 double get_max = Math.max(totalExFood, Math.max(totalExUni, totalExOther ));
  double get_min = -Math.max(totalExFood, Math.max(totalExUni,totalExOther ));
  double get_mid = (totalExFood +totalExUni  + totalExOther) - (get_max + get_min);

  String food = ("-Food         :RM"+ df.format(totalExFood));
  String univ = ("-Univercity   :RM"+ df.format(totalExUni));
  String othe = ("-Other stuffs :RM"+ df.format(totalExOther));


 System.out.println("------------------------------------------------------------------------------");
 System.out.println("FINAL REPORT"+"\n");
 System.out.println("Total months :"+ size);
 System.out.println("Total days :" +totalDay);
 System.out.println("Total income :RM" +df.format(totalInc));
 System.out.println("Total expense :RM" + df.format(totalExp) );

  if((get_max == totalExFood)&& (get_min==totalExUni)){ //testing one value
      System.out.println(food);
      System.out.println(othe);   -------> doesn't print anything
      System.out.println(univ);
  }

 System.out.println("Total saving :RM" + df.format(totalSav));
 System.out.println("Overbudget : "+ overBudg+" times");
 System.out.println("Most spend :RM" + df.format(max) +"( "+maxMonth+" )");
 System.out.println("Average monthly expense :RM" + df.format(avemonthly));
 System.out.println("Daily money spend on food :RM "+ df.format(FoodDaily));

我也试着用。 但是这个get错误(二进制运算符'&&''的操作数类型不正确)

  if((Double.compare(get_max,totalExFood)) && (Double.compare(get_min,totalExUni))){
      System.out.println(food);
      System.out.println(othe);
      System.out.println(univ);
  }

也试试这个。得到错误(double不能被取消引用)

  if(get_max.equals(totalExFood) && (get_min.equals(totalExUni)) ){
      System.out.println(food);
      System.out.println(othe);
      System.out.println(univ);
  }



// Output i want .First to print max , then print mid , and print min .

//It suppose to look like these : - 
//(highest value first)
//-Food : RM 400
//-Univercity : RM 300
//-Other Stuffs : RM 50
//or
//-Univercity : RM 400
//-Food : RM 300
//-Other Stuffs : RM 50




共1个答案

匿名用户

您应该使用Math.min获取最小值。

double get_min = Math.min(totalExFood, Math.min(totalExUni,totalExOther ));