**我想循环获取两个整数索引,并将它们发送给其他方法,直到用户将-1放入索引1 ******
public static int getIndex() {
int index1, index2;
do{
System.out.println("Enter the first index in list(-1 to finish): ");
index1= s.nextInt();
if(index1==-1) {
System.out.println("Program ends now...");
break;
}
System.out.println("Enter the second index in list: ");
index2= s.nextInt();
}while(true);
return new Indexes(index1, index2);
}
问题是如何将索引发送到其他方法? 谢谢
另一种方法:
public static String compare(ArrayList<Worker> workers, int index1, int index2 ) {
if(workers.toArray()[index1].equals(workers.toArray()[index2])==true)
return "EQUALS!";
else
return "NOT EQUALS";
}
我通过Main调用
System.out.println(compare(list,index1, index2));
答案 0 :(得分:0)
不确定我是否理解正确,但是 您可以使用Scanner类来扫描输入内容
import java.util.Scanner;
public static Indices getIndex() {
Scanner s = new Scanner(System.in);
int index1, index2;
do{
System.out.println("Enter the first index in list(-1 to finish): ");
index1 = s.nextInt();
if(index1==-1) {
System.out.println("Program ends now...");
break;
}
System.out.println("Enter the second index in list: ");
index2= = s.nextInt();
}while(true);
return new Indices(index1, index2);
}
执行此操作的最佳方法是制作自己的具有两个字段int1,int2的自定义类,并根据index1,index2变量进行设置。
class Indices{
int point1, point2;
public Indices(int p1, int p2){
this.point1 = p1;
this.point2 = p2;
}
}
然后您可以拥有两个指数,并根据需要将它们进行比较
Indices i1 = getIndex();
Indices i2 = getIndex();
compare(i1,i2);
答案 1 :(得分:-1)
使用给定参数运行方法
methodYouAreCalling(getIndex());
存储第二种方法使用的变量
indexX = Indexes.index1;
indexY = Indexes.index2;