有 Java 编程相关的问题?

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

使用一维数组编写Java程序。列出10个数字,并根据索引获取一个值

所以我在编程课上有一个作业,我不知道第二部分该做什么。这是我目前掌握的代码:

    class SingleArray
    {
      public static void main(String[]args)
      {
        int[] b=new int[]{5,10,15,20,25,30,35,40,45,50};

我知道的不多,但我真的很困惑如何让它获取不同的数组值。我必须导入util扫描仪才能执行此操作吗?有人帮我指出正确的方向吗?谢谢大家


共 (4) 个答案

  1. # 1 楼答案

    要访问int[] b中的i索引,可以使用:b[i]。例如:

    System.out.println(b[i]); //prints the int at index i
    
    b[i] = 5; //assigns 5 to index i
    b[i] = x; //assigns an int x, to index i
    
    int y = b[i];  //assigns b[i] to y
    

    要在数组中搜索数字^{

    for (int i = 0; i < b.length; i++)
    {
         if (b[i] == x)
         {
             //doSomething
         }
    }
    

    Java Arrays类还提供了一些辅助方法

    这里有一个tutorial on arrays,这里还有一个Java Language Basics tutorial

  2. # 2 楼答案

    这可能有助于:

    public class OneDimensionalArrays {
        public static void main(String[] args) {
            //initialize an array num with the respective values
            int []num={5,10,15,20,25,30,35,40,45,50};
    
            // The looping will work within the range of the array.
            for (int i = 0; i<num.length;i++){
    
                System.out.println("Enter the index to find the value of ");
    
                //To catch array index out of bound exception
                try{
                    Scanner sc = new Scanner(System.in);
                    //Save the user inputted value in indexValue variable
    
                    int indexValue = sc.nextInt();
                    // Enter 100 to terminate the program execution
                    if (indexValue != 100) {
                        System.out.println("Value in the respective index of array is " + num[indexValue]);
                    }
                    else{
                        System.out.println("Program has been terminated by user");
                        break;
                                        }
                }
                catch (Exception e ){
                    System.out.println("Enter the index within range of the array");
                }
            }
        }
    }
    
  3. # 3 楼答案

    请完成下面评论中的作业-

    class SingleArray {
    
     // Declare an array of 10 integers. 
     // Assignment: Why did I declare it as static?
     private static int[] array = new int[] { 5, 10, 15, 20, 25, 30, 35, 40, 45, 50 };
    
     // A method that returns the value at a given index from the array
     // Assignment: Why the method below is static?
     private static int getValue(int index){
    
         return array[index];
    
     }    
    
     public static void main(String[] args){
    
        for (int i = 0; i < array.length; i++){
    
            int valueAtIndex = getValue(i);
    
            System.out.println("Index: " + (i + 1) + " Value: " + valueAtIndex);
    
            // Assignment: remove the statement "int valueAtIndex = getValue(i);" and 
            // modify the System.out.println statement to have the same result 
    
        }
    
      }
    
    }
    
  4. # 4 楼答案

    List<Integer> integerList=new ArrayList<Integer>();
    for(int i=0;i<10;i++){
     integerList.add(i);
    }
    //you can fetch the values by using this method:
    integerList.get(valueIndex);