如何找到最大值的索引?
Posted
技术标签:
【中文标题】如何找到最大值的索引?【英文标题】:How to find the maximum's index? 【发布时间】:2018-05-05 09:09:05 【问题描述】:我在这里遇到了这个问题。索引是 int,但程序让我翻倍。我找不到问题。有很多几何形状,它们有中心坐标和边长。我必须计算面积和环境的差异,然后选择最大数字的索引。
/主类
package beadandó;
import java.io.FileNotFoundException;
public class Main
public static void main(String[] args)
Geom geom = new Geom();
try
geom.read("input.txt");
System.out.println(geom.MaxKiv());
catch (FileNotFoundException ex)
System.out.println("File not found!");
System.exit(-1);
catch (InvalidInputException ex)
System.out.println("Invalid input!");
System.exit(-1);
/getDifference
package beadandó;
import java.util.ArrayList;
public abstract class GeometricShape
private Point center;
double length;
private ArrayList<Double> difference;
public GeometricShape( Point center, double length)
this.center = center;
this.length = length;
difference = new ArrayList<>();
public void setCenter(Point center)
this.center = center;
public void setLength(double length)
this.length = length;
public Point getCenter()
return center;
public double getLength()
return length;
public ArrayList<Double> getDifference()
return difference;
public abstract double doMath();
/MaxKiv 是最大值查找器
package beadandó;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Scanner;
public class Geom
private final ArrayList<GeometricShape> geometricShapes;
public Geom()
geometricShapes = new ArrayList<>();
public void read(String filename) throws FileNotFoundException, InvalidInputException
Scanner sc = new Scanner(new BufferedReader(new FileReader(filename)));
int number = sc.nextInt();
while (sc.hasNext())
GeometricShape geometricShape;
int kind = sc.nextInt();
double x = sc.nextDouble();
double y = sc.nextDouble();
Point center = new Point(x, y);
double length = sc.nextDouble();
switch (kind)
case 0:
geometricShape = new Circle(center, length);
break;
case 3:
geometricShape = new Triangle(center, length);
break;
case 4:
geometricShape = new Square(center, length);
break;
case 6:
geometricShape = new Hexagon(center, length);
break;
default:
throw new InvalidInputException();
geometricShapes.add(geometricShape);
public double MaxKiv()
double max = 0;
int index =0;
for (int i = 0; i < geometricShapes.size(); ++i)
if (max <geometricShapes.get(i).doMath())
max = geometricShapes.get(i).doMath();
index=i;
return index;
/计算差异(有很多形状,这是其中之一)
package beadandó;
import static java.lang.Math.sin;
public class Hexagon extends GeometricShape
public Hexagon(Point center, double length)
super(center, length);
@Override
public double doMath()
double circumfence=(3*(length*length)*sin(60));
double district=(6*length);
double difference=(circumfence-district);
return difference;
【问题讨论】:
永远不要这样设计你的问题。发布您的努力以获得快速帮助。 Why is “Can someone help me?” not an actual question? 对不起。我发布了代码。 我认为你想要index=1;
而不是这一行:index = i;
。
CodeMatrix 将代码编辑为 1。它是 i。 :D
【参考方案1】:
您已声明:
public double MaxKiv()
所以是的,显然这会给你一个double
。要获取int
,只需更改声明以返回int
:
public int MaxKiv()
您返回的值index
已经声明为int
,所以这应该足以解决您的问题。
【讨论】:
以上是关于如何找到最大值的索引?的主要内容,如果未能解决你的问题,请参考以下文章
如何找到int数组Python的最小值和最大值的索引[重复]