[英]Getting array values from user input
I am trying to get an integer and a double value from the user using a scanner. 我正在尝试使用扫描仪从用户那里获取整数和双精度值。 Im having trouble setting the values i get to the array itself. 我在设置值时遇到麻烦,我无法访问数组本身。 Here is my code: 这是我的代码:
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 is an instance, accEmp is an accessor from another class. P是一个实例,accEmp是另一个类的访问器。 Also, i cant use Array List. 另外,我不能使用数组列表。
Your arr
variable is an array of int. 您的arr
变量是一个int数组。 Scanner.nextInt
will read and int, but not an array. Scanner.nextInt
将读取和读取int,但不会读取数组。
The line arr = sc.nextInt()
won't compile. 行arr = sc.nextInt()
不会编译。 Either change arr
to be an int
or add the value to the array. 将arr
更改为int
或将值添加到数组。
I believe, since you seem to be looping over employees, that you should keep a reference to the looping index and add to the array at that index : 我相信,由于您似乎正在遍历员工,因此应保留对循环索引的引用,并在该索引处添加到数组中:
for(int i = 0; i < p.accEmp().length/* or .size() if it's a list */; i++)
{
arr[i] = sc.nextInt();
}
and also incorporate this within the loop, the part where you get the double values: 并将其合并到循环中,即获得双精度值的部分:
dub[i]=sc.nextDouble();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.