我正在尝试使用扫描仪从用户处获取整数和 double 值。我在设置数组本身的值时遇到问题。这是我的代码:
import java.util.Scanner;
public class MainClass {
public static void main(String[] args) {
final int SIZE = 7;
int[] arr = new int[SIZE];
int[] arr2 = new int[SIZE];
double[] dub = new double[SIZE];
Scanner sc = new Scanner(System.in);
PayRoll p = new PayRoll();
for(String i: p.AccEmp()){
System.out.println("How many hours were worked by the this employee number: " + i);
arr = sc.nextInt();
p.SetHRS(arr);
System.out.println("What is the pay for this employee number: " + i);
dub = sc.nextDouble();
p.setPR(dub);
}
}
}
P 是一个实例,accEmp 是另一个类的访问器。 另外,我不能使用数组列表。
最佳答案
你的arr
变量是一个int数组。 Scanner.nextInt
将读取 int,但不是数组。
行arr = sc.nextInt()
将无法编译。将 arr
更改为 int
或将值添加到数组中。
我相信,由于您似乎在循环员工,因此您应该保留对循环索引的引用并添加到该索引处的数组:
for(int i = 0; i < p.accEmp().length/* or .size() if it's a list */; i++)
{
arr[i] = sc.nextInt();
}
并将其合并到循环中,即获取 double 值的部分:dub[i]=sc.nextDouble();
关于java - 从用户输入获取数组值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29711250/