java輸入多個數(shù)據(jù)(不確定),排序,并求最大值的方法
更新時間:2018年07月19日 12:17:55 作者:gt-liu
今天小編就為大家分享一篇java輸入多個數(shù)據(jù)(不確定),排序,并求最大值的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
如下所示:
package exercise03_jdknews; import java.util.Arrays; import java.util.Scanner; import java.util.Vector; /** * 鍵盤錄入多個數(shù)據(jù),以0結束,要求在控制臺輸出這多個數(shù)據(jù)中的最大值 * @author lgt * */ public class MaxNuber { public static void main(String[] args) { //輸入 Scanner sc = new Scanner(System.in); //創(chuàng)建集合對象 Vector<Integer> v = new Vector<Integer>(); //控制輸入數(shù)據(jù),輸入0表示結束 while(true){ int number = sc.nextInt(); if(number != 0){ v.add(number); }else{ break; } } sc.close(); System.out.println("---------排序前遍歷輸出數(shù)組---------"); //把集合轉成數(shù)組 //創(chuàng)建數(shù)組對象 Integer[] i = new Integer[v.size()]; //轉成數(shù)組 v.toArray(i); //排序前遍歷輸出數(shù)組 printArray(i); System.out.println("-------排序后遍歷輸出數(shù)組,并輸出最大值--------"); //排序 Arrays.sort(i); //排序后遍歷輸出數(shù)組,并輸出最大值 printArray(i); System.out.println("最大值是:" + i[i.length-1]); } //遍歷數(shù)組 public static void printArray(Integer[] k){ for(int i = 0; i < k.length; i++){ if(i!= k.length -1){ System.out.print(k[i] + ", "); }else{ System.out.println(k[i]); } } } }
以上這篇java輸入多個數(shù)據(jù)(不確定),排序,并求最大值的方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
如何在Spring Boot應用程序中配置了兩個不同的SOAP Web服務端點
這篇文章主要介紹了如何在Spring Boot應用程序中配置了兩個不同的SOAP Web服務端點,本文通過示例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-08-08基于springcloud異步線程池、高并發(fā)請求feign的解決方案
這篇文章主要介紹了基于springcloud異步線程池、高并發(fā)請求feign的解決方案,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-02-02SpringBoot下獲取resources目錄下文件的常用方法
本文詳細介紹了SpringBoot獲取resources目錄下文件的常用方法,包括使用this.getClass()方法、ClassPathResource獲取以及hutool工具類ResourceUtil獲取,感興趣的可以了解一下2024-10-10