[英]Getting array values from user input
我正在尝试使用扫描仪从用户那里获取整数和双精度值。 我在设置值时遇到麻烦,我无法访问数组本身。 这是我的代码:
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();
}
并将其合并到循环中,即获得双精度值的部分:
dub[i]=sc.nextDouble();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.