java - 打印给定数字中最大的数字

标签 java android sorting

我的问题类似于this ,但是在安卓中。我正在尝试从 EditText 获取输入,然后单击按钮将最终结果显示到 TextView 中。

我在尝试调整这三个答案时遇到了麻烦,OP说这三个答案都有效。

如何从 TextView 中获取值并将结果显示在 TextView 中?

下面的方法似乎有效,但输出是 000,我猜是因为 arrayList 中没有存储任何内容

input = (Edittext)findViewById(R.id.et_input);
output = (TextView)findViewById(R.id.tv_output);
    ArrayList<Integer> myList = new ArrayList<Integer>();
            Scanner val = new Scanner(System.in); //can't figure out how to get user input from editText
            int x = 0;
            for (int i = 0; i < 3; i++) {
                x = val.nextInt();
                myList.add(x);
            }
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                myList.sort(null);
            }

            for (int i = myList.size() - 1; i >= 0; i--) {
                answer += myList.get(i).toString();
            }


            send.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    output.setText(answer);
                }
            });

最佳答案

你可能会做这样的事情:

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    // use textview to display the result
    output = (TextView)findViewByIf(R.id.textview1);
    // use a button to activate sorting once you have the edittexts filled
    sortNumbersButton = (Button)findViewById(R.id.button);
    // use EditText(s) to get values from user
    input = (EditText)findViewById(R.id.edittext1);
    // set a listener for a button so that we can activate sorting
    sortNumbersButton.setOnClickListener(
        new View.OnClickListener()
        {
            public void onClick(View view)
            {
                // use a string array to store string values from input
                string[] strings = input.getText().toString().split(" ");
                // use the editText getText() method to pull values from UI
                int[] intArray = new int[] {Integer.parseInt(strings[0]),
                    Integer.parseInt(strings[1]),
                    Integer.parseInt(strings[2])};
                // sort the values in the array in ascending order
                Arrays.sort(intArray);
                // use stringbuilder to append string values for output
                StringBuilder sb = new StringBuilder();
                // loop backwards since it sorts in ascending order
                for (int i = 2; i > -1; i--) {
                    sb.append(intArray[i]);
                }
                // set the textview value of the sorted integers! and done
                output.setText(sb.toString());
            }
        });
}

关于java - 打印给定数字中最大的数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51062761/

相关文章:

android - 在 Android 中加入联系人和照片表

android - JsonMappingException : Infinite recursion on OneToMany Relationship in Objectify

Java,对 double 组及其索引进行排序

java - 我的程序没有在另一个类中找到已定义的方法

java - 项目机子: Why are virtual threads not the default?

mysql - 开源应用服务器解决方案

java - 尝试使用 Comparator 对 ArrayList 进行排序

java - 耶拿,DBpedia : RDF and model name

android - 找不到构建工具修订版 23.0.1

python - 按值对字典进行排序并仅打印键