我如何根据用户输入实例化一个数组
how do i instantiate an array based on user input
我对如何使用用户的输入来实例化数组感到困惑,我认为它应该在此处的评论旁边
导入 java.util.Scanner;
public class Sales
{
public static void main(String[] args)
{
int[] sales;
sales = getSales();
printSales(sales);
printSummary(sales);
}
private static int[] getSales()
{
Scanner input = new Scanner(System.in);
int[] temp;
System.out.print("Enter the number of salespeople: ");
temp = _____________; // RIGHT HERE
你应该这样写:
temp = new int[input.nextInt()];
我对如何使用用户的输入来实例化数组感到困惑,我认为它应该在此处的评论旁边 导入 java.util.Scanner;
public class Sales
{
public static void main(String[] args)
{
int[] sales;
sales = getSales();
printSales(sales);
printSummary(sales);
}
private static int[] getSales()
{
Scanner input = new Scanner(System.in);
int[] temp;
System.out.print("Enter the number of salespeople: ");
temp = _____________; // RIGHT HERE
你应该这样写:
temp = new int[input.nextInt()];