有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

带扫描器的java阵列

请帮助我,我的教授教得不好 我的教授想让我用户输入索引和元素的值,我试着为循环做了2次,但没有效果

import java.util.Scanner;
public class Arrays 
{
    public static void main (String [] args)
    {   
        Scanner sc = new Scanner (System.in);

        int index;
        int elements;

        System.out.println("Input Array Size");
        index = sc.nextInt();


        for (int i = 0; i < index; i++) 
        {
            System.out.println("Array Index is =\t"+index);
            System.out.println ("Insert the Elements of the Array");
            break;
        }
    }
}

如果有人知道这个链接,请发送给我我非常需要你的帮助我需要学习输入搜索和数组中的删除,但是使用扫描仪hayst请帮助我-学生


共 (1) 个答案

  1. # 1 楼答案

    这里的问题是,您只是打印数组大小,而不接受任何输入来填充数组。实际的方法如下

    import java.util.Scanner;
    
    public class Arrays
    {
        public static void main(String[] args)
        {
            //Create a Scanner to read input
            Scanner scan = new Scanner(System.in);
    
            //Promt the user to enter the array size and store the input
            System.out.println("Enter the size of the array:");
            int arraySize = scan.nextInt();
    
            //Create an array (For this example we'll use an integer array)
            int[] array = new int[arraySize];
    
            //Create a for loop to run through array
            for(int i = 0; i < arraySize; i++)
            {
                //Prompt the user to enter a number at the current index (i)
                System.out.println("Enter the element at index " + i + ":");
    
                //Store the input at index i in the array
                array[i] = scan.nextInt();
            }
        }
    }