1. java如何得到數據類型的最大值
Java得到數據類型的最大值:
Integer.MAX_VALUE 獲取int類型的最大值
Integer.MIN_VALUE 獲取int類型的最小值,其他基本數據類型同理
2. java中怎麼獲取int的最大值
Integer.MAX_VALUE 獲取int類型的最大值
Integer.MIN_VALUE 獲取int類型的最小值,其他基本數據類型同理
3. java取數組中的最大值
先假設第一個數是最大的,設成max的值,然後拿它依次和後面的數去比,如果遇到比它大的,那就把max的值換成這個較大數,直到遍歷完了,max的值就是數組中的最大值嘍。
4. JAVA中MAX取最大值的問題!
其實你可以用數組存儲多個數,然後對數組進行排序,正序的話,數組的最後一個元素就是最大的,倒序的話,數組的第一個元素就是最大的,這種方法我認為是最簡單的
int a = 3, b = 4, c = 5, d = 6;
int max = (a > b ? a : b) > c ? (a > b ? a : b) : c;
max = max > d ? max : d;
System.out.println("max=" + max);
5. java如何求最大值
測試結果為
請輸入第1個數
2
請輸入第2個數
3
請輸入第3個數
4
請輸入第4個數
5
請輸入第5個數
6
請輸入第6個數
7
請輸入第7個數
7
請輸入第8個數
8
請輸入第9個數
9
請輸入第10個數
423
最大數為:423
import java.util.Scanner;
public class MaxNum {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
int max = Integer.MIN_VALUE;
for(int i = 1; i <= 10; i++) {
System.out.println("請輸入第" + i + "個數");
int num = in.nextInt();
if(num >= max) max = num;
}
System.out.println("最大數為:" + max);
}
}
6. 用java編寫找最大值函數
你好,好多種辦法。我給你寫幾種經典的排序吧,最大值就在第一個(倒序)或者最後一個(從小到大)。
首先方便輸出,定義一個工具類
public class PrintUtil {
public static void print(int[] array){
for(int i=0;i<array.length;i++){
System.out.print(array[i] + " ");
}
System.out.println();
}
}
第一種:冒泡排序
public class BubblingSort {
/*
* 演算法思想->升序
* 1、比較相鄰兩項,若為逆序,則交換
* 2、每次比較的結果就是將最大項浮到數組尾
* 演算法分析:
* ------- 最壞情況 --------
* 比較次數:(n-1) + (n-2) + ... + 1 = n*(n-1)/2
* 交換次數:[(n-1) + (n-2) + ... + 1]*3 = 3*n*(n-1)/2
* 所以n元選擇排序需要的主要操作次數是:n*(n-1)/2+3*n*(n-1)/2=2*n*n-2*n
* 結論:O(n*n)階
* ------- 最好情況 --------
* 比較次數:n-1
* 交換次數:0
* 所以n元選擇排序需要的主要操作次數是:n-1
* 結論:O(n)階
*/
public void bubbingSort(int[] array){
int len = array.length ;
for(int i=len-1;i>0;i--){
boolean flag = false ;
for(int j=0;j<i;j++){
if(array[j]>array[j+1]){
int temp = array[j] ;
array[j] = array[j+1] ;
array[j+1] = temp ;
flag = true ;
}
}
if(!flag){
break ;
}
System.out.print("第" + (5-i) + "趟的排序結果為:" );
PrintUtil.print(array) ;
}
}
public static void main(String[] args) {
int[] array = {29,10,14,37,13} ;
// int[] array = {37,29,14,13,10} ; //最差情況
// int[] array = {10,13,14,29,37} ; //最好情況
BubblingSort ss = new BubblingSort();
ss.bubbingSort(array);
PrintUtil.print(array) ;
}
}
第二種:插入排序
public class InsertSort {
/*
* 演算法思想->升序
* 1、將一個數組的元素分成兩部分,前半部分為有序數組
* 2、每一次取後半部分的第一個元素,將該元素插入到有序數組中
* 演算法分析:
* ------- 最壞情況 --------
* 比較次數:1 + 2 + ... + (n-1) = n*(n-1)/2
* 移動次數:
* 內部循環:1 + 2 + ... + (n-1) = n*(n-1)/2
* 外部循環:2*(n-1) OR 2
* 所以n元選擇排序需要的主要操作次數是:n*(n-1)/2 + n*(n-1)/2 + 2*(n-1) = n*n+n-2
* 結論:O(n*n)階
*/
public void insertSort(int[] array){
for (int i = 1; i < array.length; i++) {
int temp = array[i] ;
for(int j= i-1; j >= 0; j--){
if(temp > array[j])
break ;
if(temp < array[j]){
array[j+1] = array[j] ;
if( j != 0 && temp > array[j-1]){
array[j] = temp ;
break ;
}
if(j == 0){
array[0] = temp ;
}
}
}
System.out.print("第" + i + "趟的排序結果為:" );
PrintUtil.print(array) ;
}
}
public static void main(String[] args) {
int[] array = {29,10,14,37,13} ;
InsertSort ss = new InsertSort();
ss.insertSort(array);
PrintUtil.print(array) ;
}
}
第三種:選擇排序
public class SelectSort {
/*
* 演算法思想->升序
* 1、找到最大的元素,與最後一個元素交換
* 2、除去最後一個元素外,在數組的剩餘項中查找最大項,並與倒數第二個元素交換
*
* 演算法分析:
* 比較次數:n*(n-1)/2
* 交換次數:3*(n-1)
* 所以n元選擇排序需要的主要操作次數是:n*(n-1)/2+3*(n-1)=n*n/2+5*n-3
* 結論:O(n*n)階
*/
public void selectSort(int[] array){
int len = array.length ; //記錄數組的長度
int scope = len ; //遍歷范圍
for(int i=0;i<len-1;i++){
int max = array[0] ; //數組的最大元素
int index = 0 ; //記錄最大元素的下標
for(int j=1;j<scope;j++){
if(max < array[j]){
max = array[j] ;
index = j ;
}
}
int temp = array[scope-1] ;
array[scope-1] = array[index] ;
array[index] = temp ;
System.out.print("第" + (i+1) + "趟的排序結果為:" );
PrintUtil.print(array) ;
scope -- ;
}
}
public static void main(String[] args) {
int[] array = {29,10,14,37,13} ;
SelectSort ss = new SelectSort();
ss.selectSort(array);
PrintUtil.print(array) ;
}
}
其餘的都比較復雜就不給你多寫了。
其實很簡單的演算法,就是遍歷這N個數,沒遇到一個大的值,就去賦給max,最後輸出max,但是這個沒什麼技術含量,所以在最後說明下。
7. java如何選取list中最大值
Collections.max(list);
8. java 怎麼獲取queue最大值最小值
最小值,使用java.util.Collections.min(Collection<?>)
最大值,使用java.util.Collections.max(Collection<?>)
寫個測試下:
publicstaticvoidmain(String[]args){
Queue<Integer>queue=newArrayDeque<Integer>();
queue.add(400);
queue.add(800);
queue.add(100);
queue.add(200);
System.out.println(Collections.min(queue));
System.out.println(Collections.max(queue));
}
輸出:
100
800
9. java中如何取得一組數字的最大值
以下是Java中的List,如果是數組,大同小異
packagecom;
importjava.util.ArrayList;
importjava.util.List;
publicclassTest{
publicstaticvoidmain(String[]args){
//初始化數組
List<Integer>nums=newArrayList<Integer>();
nums.add(2);
nums.add(5);
nums.add(10);
nums.add(6);
nums.add(3);
//設置最大值Max
intMax=Collections.max(nums);
System.out.println("Max="+Max);
}
}
10. Java求最大值的三種方法
普通方法:
public class Max {
public static void main(String[] args) {
double[] myList = {1.9, 2.9, 3.4, 3.5,10,11,15,100,-1,-4.5}; //定義一維數組
double num = myList[0]; //0為第一個數組下標
for (int i = 0; i < myList.length; i++) { //開始循環一維數組
if (myList[i] > num) { //循環判斷數組元素
num = myList[i]; } //賦值給num,然後再次循環
}
System.out.println("最大值為" + num); //跳出循環,輸出結果 }
}
三元運算符:
public class Max {
public static void main(String[] args) {
double[] myList = {1.9, 2.9, 3.4, 3.5,10,11,15,1,-1,-4.2}; //定義一維數組
double num = myList[0]; //0為第一個數組下標
for (int i = 0; i < myList.length; i++){ //開始循環一維數組
num=(myList[i] < num?num: myList[i]); //三元運算符,詳情看註解
}
System.out.println("最大值為" + num); //跳出循環,輸出結果
}}
一般函數/方法:
public class Max {
double[] myList = {1.9, 2.9, 3.4, 100,3.5,10,11,12,13,-1};
double num = myList[0];
void getValue(){ //創建一般方法
for (int i = 0; i < myList.length; i++) {
num=(myList[i] < num?num: myList[i]);//三元運算符
}
System.out.println("最大值為" + num);
}
public static void main(String args[]){
Max max=new Max(); //創建對象
max.getValue(); //通過對象調用一般方法
}
}
三種求最大值的方法