有 Java 编程相关的问题?

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

java替换元素

我正在编写显示数组简单操作的代码。在我创建的数组中重新插入一个已删除的元素,我似乎无法让它工作。我的目标是将另一个元素放入另一个已删除的元素中(当我删除一个元素时,它将变为0)。我的insert case只告诉重复输入,它不允许我在删除的元素中的某个位置恢复

          case 2:
         {
          if (limit1 < 5 || limit1 > 20){
            System.out.println("Error: Invalid Array Limit");
            System.out.println("Press Any Key To Continue...");
            new java.util.Scanner(System.in).nextLine();
            System.out.print('\u000C');
            m();
            }
            else{
            System.out.println("Enter the " + array.length + " numbers now. 
            Or enter -1 to exit");
            int i = 0;
            while(i < array.length){
               array[i] = in.nextInt();
               boolean dups = false;    
             if(array[i] != -1){
             for(int k = 0; k < i; k++)
                if(array[k] == array[i])
                {
                    System.out.println("Error: Duplicate Element");
                    System.out.println("Please Enter Another Value");
                    dups = true;
                    break;
                }
                if(!dups){
                i++;}
           }
           else{
               array[i] = 0;
               System.out.println("Exit Confirmed");
               System.out.println("Press Any Key To Continue...");
               new java.util.Scanner(System.in).nextLine();
               System.out.print('\u000C');
               m();
            }
        }
          System.out.println("You have entered the "+ limit1 + " numbers");
          System.out.println("Press Any Key To Continue...");
          new java.util.Scanner(System.in).nextLine();
          System.out.print('\u000C');
          m();
          } 
        } 

另一个问题是,如果我输入一个哨兵值(-1),它只会使当前输入位置为0。我只是想退出这个案子,而不是在这个位置打0

enter image description here


共 (2) 个答案

  1. # 1 楼答案

    以下是我在您的代码中发现的一些错误:

      在字符串的中间,在{{CD1}}中换行,您应该这样做:
    System.out.println("Enter the " + array.length + " numbers now." + "\nOr enter -1 to exit")
    
    • 如果输入为-1,则不会立即退出,而是执行array[i]=0(请记住,array[i]现在是array[-1])
    • 然后在输入-1后就不会中断循环
    • 箱子不应装在括号内,并应始终以中断结束:
    case 1:
      //DO THINGS
      //....
      //...
      break;
    case 2:
      //DO OTHER THINGS
      //....
      //...
      break;
    

    以下是一些如何改进的建议:

    • 我不太清楚Java,但我不认为每次都需要创建一个新的扫描仪
    • 如果我是你,我会首先检查输入是否为-1(有几种方法)
    • 不使用for的括号有点混乱
    • 当发现重复项时,您已经中断了,因此不需要再次使用if(!dups)

    我希望这能解决你的问题

  2. # 2 楼答案

    我发现你的代码有问题。在没有任何break语句的情况下使用switch语句不是一种好做法。你可以很容易地重构你的方法来使用这样的while循环:

    public void e() {
        do {
            m();
            choice1 = in.nextInt();
    
            cls();
            if (choice1 > 0) {
                processChoice(); // contains switch block for processing input
            }
        } while (choice1 > 0); // Loop will terminate when user presses 0
    }
    

    每当用户按下0时,也应该退出程序

    我发现你在数组块中的插入有问题。您似乎正在将从输入中接收的值直接分配给array[i]。在将它赋给array[i]之后,检查它是否是重复值有什么意义。我认为你应该这样做:

    while (i < array.length) {
        int currentInput = in.nextInt();
        boolean dups = false;
        if (array[i] != -1) {
            for (int k = 0; k < i; k++)
                if (array[k] == currentInput) {
                    System.out.println("Error: Duplicate Element");
                    System.out.println("Please Enter Another Value");
                    dups = true;
                    break;
                }
            if (!dups) { // currentInput not a duplicate; assign to array[i]
                array[i] = currentInput;
                i++;
            }
    

    关于在提供-1时退出,您可能应该删除这一行array[i] = 0,以便不将0分配给array[i]

    if (array[i] != -1) {
        // ...
    } else {
        System.out.println("Exit Confirmed");
        System.out.println("Press Any Key To Continue...");
        new Scanner(System.in).nextLine();
        System.out.print('\u000C');
        break;
    }