Switch-biu 2023-04-13 17:02 采纳率: 90%
浏览 17

关于#数组#的问题,如何解决?Java

数组不能在方法中实现保存



```java
package week15_activity3;

import java.util.Scanner;

public class menu_driven {
    public static void main(String[] args) {
        while(true){
            System.out.println("1:Accept elements of an array from the user\n" +
                    "2:Display elements of the array \n" +
                    "3:Search an element given by the user within that array \n" +
                    "4:Sort the array using the bubble sort method.\n"+
                    "Please choose you need:");
            Scanner s = new Scanner(System.in);
            int a = s.nextInt();
            switch (a){
                case 1:
                    int array[] = accept();
                    continue;
                case 2:
                    display(array);
                    continue;
                case 3:
                    search(array);
                    continue;
                case 4:
                    sort1(array);
                    continue;
                case 5:
                    break;
            }
        }
    }
    public static int[] accept() {
        Scanner s1 = new Scanner(System.in);
        System.out.println("请输入数组(空格隔开):");
        String str = s1.next();
        String[] arr = str.split(" ");
        int[] b = new int[arr.length];
        for (int j = 0; j < b.length; j++) {
            b[j] = Integer.parseInt(arr[j]);
        }
        return b;
    }
    public static void display(int[] a){
        System.out.println("该数组内容:");
        for (int i = 0; i < a.length; i++) {
            System.out.println(a[i]+" ");
        }
    }
    public static void search(int[] a){
        Scanner s2 = new Scanner(System.in);
        System.out.println("请输入要查找的数:");
        int index = s2.nextInt();
        int flag = 0;
        for (int i = 0; i < a.length; i++) {
            if(index == a[i]){
                System.out.println("已找到,该元素在数组中的下标为:"+i+"\n");
                flag = 1;
                break;
            }
        }
        if(flag == 0){
            System.out.println("未找到该元素\n");
        }
    }
    public static void sort1(int[] a){
        for (int i = 0; i < a.length; i++) {
            for (int j = 1;j<a.length-i;j++){
                if(a[j-1]>a[j]){
                    int t = a[j-1];
                    a[j-1] = a[j];
                    a[j] = t;
                }
            }
        }
        System.out.println("已排序,排序结果为:");
        for (int i = 0; i < a.length; i++) {
            System.out.println(a[i]);
        }
    }
}

```

  • 写回答

2条回答 默认 最新

  • 真相重于对错 2023-04-13 17:08
    关注

    超出了变量生命周期

    评论

报告相同问题?

问题事件

  • 创建了问题 4月13日

悬赏问题

  • ¥15 三极管电路求解,已知电阻电压和三级关放大倍数
  • ¥15 ADS时域 连续相位观察方法
  • ¥15 Opencv配置出错
  • ¥15 模电中二极管,三极管和电容的应用
  • ¥15 关于模型导入UNITY的.FBX: Check external application preferences.警告。
  • ¥15 气象网格数据与卫星轨道数据如何匹配
  • ¥100 java ee ssm项目 悬赏,感兴趣直接联系我
  • ¥15 微软账户问题不小心注销了好像
  • ¥15 x264库中预测模式字IPM、运动向量差MVD、量化后的DCT系数的位置
  • ¥15 curl 命令调用正常,程序调用报 java.net.ConnectException: connection refused