简体   繁体   English

接受和排序无限的用户输入

[英]Accepting and sorting unlimited user input

I have only started learning java a few days ago, and this is the first language that I am trying to learn. 几天前我才开始学习Java,这是我尝试学习的第一门语言。 So please, excuse my ignorance. 所以,请原谅我的无知。

I am trying to create a class to accept unlimited user input (in the form of integers 5, 10, 15, 500, 10003, etc.) Then, stop accepting input when the user writes "Ok" into the command line. 我正在尝试创建一个类来接受无限的用户输入(以整数5、10、15、500、10003等形式),然后,当用户在命令行中输入“ Ok”时,停止接受输入。 Then receive a prompt asking for ascending or descending. 然后收到提示要求上升或下降的提示。 Finally, print the list of numbers in ascending or descending order, according to the user's choice. 最后,根据用户的选择,按升序或降序打印数字列表。

I did not think it would be too hard to code. 我认为编写代码不会太困难。 However, I am running into a few problems: 但是,我遇到了一些问题:

  1. I am trying to use the scanner to accept user input and place it in an ArrayList .... However, the ArrayList will only accept one type of variable (I need to place all integer inputs in the ArrayList and display results when the string "OK" is written). 我正在尝试使用扫描仪接受用户输入并将其放置在ArrayList 。...但是, ArrayList将仅接受一种类型的变量(我需要将所有整数输入都放置在ArrayList并在字符串“确定”)。

  2. I do not know how to print an ArrayList so that the numbers will display on one line each in ascending or descending order. 我不知道如何打印ArrayList以便数字将以升序或降序显示在一行上。

If anyone could write me an example code or point me in the right direction, that would be much appreciated. 如果有人可以给我写示例代码或为我指明正确的方向,那将不胜感激。

it looks like a homework :) 它看起来像是一项家庭作业:)

You check the input if it converts to int you add it to the int array,if it is equal to 'OK' you do the processing, else you ignore. 您检查输入是否转换为int,然后将其添加到int数组中;如果等于'OK',则进行处理,否则忽略。 Processing is about sorting your array, then loop through it and do: 处理是关于对数组进行排序,然后遍历它并执行:

System.out.println(myArray.get(i)); // print will print inline println will print and go to next line.

If no type is specified the ArrayList stores objects as type Object. 如果未指定类型,则ArrayList将对象存储为Object类型。 It can mix object types (String, MyClass, Array etc. are all also of type Object). 它可以混合对象类型(String,MyClass,Array等也都是Object类型)。 Int can be put in wrapper class Integer. 可以将Int放在包装类Integer中。

There is no type checking. 没有类型检查。 Depending on your compiler / IDE settings you may get a warning: Name.java uses unchecked or unsafe operations. 根据您的编译器/ IDE设置,您可能会收到警告:Name.java使用未经检查或不安全的操作。

This is risky - so as above answer suggests probably better to use Do loop until "OK" is entered. 这是有风险的-因此,以上答案建议最好使用Do循环,直到输入“ OK”为止。

You can use the following : 您可以使用以下内容:

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

public class Main {
 public static void main(String[] args) {

    System.out.println("Please enter integer numbers , write 'OK' to exit");

List<Integer>inputs=new ArrayList<Integer>();

Scanner scanner=new Scanner(System.in);
String input=scanner.next();
while(!"OK".equalsIgnoreCase(input)){
    inputs.add(Integer.parseInt(input));
    input=scanner.next();
}
if(inputs.isEmpty())
    return;
System.out.println("How would you like to sort 'ASC' or 'DSC'");
input=scanner.next();
if(input.equalsIgnoreCase("ASC")){
    Collections.sort(inputs);
}
else if(input.equalsIgnoreCase("DSC")){
    Collections.sort(inputs);
    Collections.reverse(inputs);
}
System.out.println(inputs);
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM