【问题标题】:Having to enter 'N' twice for it to display relevant information必须输入“N”两次才能显示相关信息
【发布时间】:2015-02-15 11:59:37
【问题描述】:

我正在尝试编写一些代码,根据用户输入的内容按升序或降序对数字进行排序。当用户输入“Y”时,该程序能够按升序对它们进行排序,但是如果他们输入“N”以降序排序,则用户必须在显示之前将“N”输入两次。我已经发布了下面的语法,所以如果有人想告诉我缺少什么/做错了什么,请随时这样做。

import java.util.*;

public class SortProgram { 
    public static void main(String args[]) { 
        Scanner in = new Scanner(System.in);
        System.out.println("\nHow many numbers do you want sorted?: ");
        int count = in.nextInt();
        int[] array = new int[count];

        for (int i = 0; i < count; i++) {
            System.out.print("Enter number #" + (i+1) + ": ");
            array[i] = in.nextInt();
        }

        System.out.print("\nPress 'Y' to sort numbers in  order, press 'N' to sort numbers in DESCENDING order: ");

        in.nextLine();

        boolean ascending = true;
        boolean descending = false;

        if (in.nextLine().toUpperCase().charAt(0) == 'Y') {
            ascending = true;
        } else if (in.nextLine().toUpperCase().charAt(0) == 'N') {
            descending = true;
            ascending = false;
        }

        for (int i = 0; i < count; i++) {
            for (int j = 0; j < count - 1; j++) {
                if (ascending) {
                    if (array[j] > array[j + 1]) {
                        int temp = array[j];
                        array[j] = array[j + 1];
                        array[j + 1] = temp;
                    }
                } else if (!ascending) {
                    if (array[j] < array[j + 1]) {
                        int temp = array[j];
                        array[j] = array[j + 1];
                        array[j + 1] = temp;
                    }
                }
            }
        }
        System.out.print("\nThe sorted numbers are: ");

        for (int i = 0; i < count; i++) {
            System.out.print(array[i] + " ");
        }
    }
}

【问题讨论】:

  • 为什么descending 甚至是一个变量?你根本不用它。
  • 一直在尝试解决各种问题,我想我会尝试一下。我对Java完全陌生,所以我不了解大众。你能看到很多错误吗?
  • 你应该明确地将 in.nextline() 分配给一个变量,这样你就不会一直调用它,然后你可以检查变量的值。我不知道这是否会改变任何东西,因为我现在没有运行它,但这是一个很好的编码习惯

标签: java sorting compilation


【解决方案1】:

您好像在给in.nextLine() 打了两次电话。每次调用时,程序都会从​​用户那里读取另一件事。

默认情况下,您的代码按升序运行。但是,由于您输入了两次,因此您需要输入两次“N”(当它“计数”时 - 即第二次,当您实际使用结果时)才能工作。

解决这个问题很简单:只需在声明 ascendingdescending 之前删除 in.nextLine()

顺便说一句,您的用户输入测试代码比它需要的要复杂一些。要正确设置ascending,您只需要:

ascending = in.nextLine().equalsIgnoreCase("y");

它使用equalsIgnoreCase(),相当于你获取第一个字符的方法,将其大写,然后与'Y'进行比较。

这将替换 for 循环之前的 if/elseif 块。

旁注#2:你在for循环中有很多重复的代码。由于唯一不同的是 if 块中的条件,因此您可以只使用布尔表达式而不是内部 if/else 语句:

    for (int i = 0; i < count; i++) {
        for (int j = 0; j < count - 1; j++) {
            boolean swap = ascending
                    ? array[j] > array[j + 1]
                    : array[j] < array[j + 1];
            if (swap) {
                int temp = array[j];
                array[j] = array[j + 1];
                array[j + 1] = temp;
            }
        }
    }

【讨论】:

  • 尝试删除,用户输入他想要的顺序后会出错。你能不能帮我改一下,这样我就可以确保在同一页上? :)
  • 什么样的错误?要使我在答案中输入的代码起作用,您需要做两件事:在分配 ascendingdescending 之前删除对 in.nextLine() 的调用,并在 for 之前替换整个 if/else 块循环使用我的答案中的单行代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多