㈠ 在java中怎麼比較三個整數大小例如(a , b, c);並從小到大輸出
用冒泡排序,對三個數字按照由小到大進行排掘配序。以23、11、17為例,代碼如下:
import java.util.Scanner;
public class woo {
static int[] bubbleSort(int[] date) {
boolean isSwap;
for(int j = 1; j < date.length; j++) {
isSwap = false;
for(int i = 0; i < date.length - j; i++) {
if(date[i] > date[i+1]) {
date[i] = date[i] ^ date[i+1];
date[i+1] = date[i] ^ date[i+1];
date[i] = date[i] ^ date[i+1];
isSwap = true;
}
}
if(isSwap == false)
break;
}
return date;
}
public static void main(String args[]) {
int date[] = new int[3];
System.out.println("輸入三個整數:");
Scanner num = new Scanner(System.in);
for(int i = 0;i < date.length; i++)
date[i] = num.nextInt();
date = bubbleSort(date);
for(int count = 0; count < date.length; count++)
System.out.print(date[count] +" ");
System.out.println("");
}
}
(1)java整型比較擴展閱讀:
通常排序演算法,可以分為兩大類。
非線性時間比較類排序:通過比較來決定元素間的相對次序,由於判塌指其時間復雜度不能突破O(nlogn),因此稱為非線性時間比較類排序。包括交換排序、插入排序、選擇排序、歸並排序。
線性時間非比衫信較類排序:不通過比較來決定元素間的相對次序,它可以突破基於比較排序的時間下界,以線性時間運行,因此稱為線性時間非比較類排序。包括計數排序、桶排序、計數排序。
㈡ 在java中怎麼比較三個整數大小例如(a,b,c
public class Sort {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("請依次輸入3個數字");
int a = scanner.nextInt();
int b = scanner.nextInt();
int c = scanner.nextInt();
if( b > a){
int t = 0;
t = a;
a = b;
b = t;
}
if( c > a){
int t =0;
t = a;
a = c;
c = t;
}
if( c > b){
int t = 0;
t = c;
c = b;
b = t;
}
System.out.println("a = "+ a + " b= " + b + " c= " + c );
}
}
㈢ java中長整型和整型的區別
相同點:
兩者都是整數
不同點:
長整形取值范圍比整型取值范圍大。
長整形最大值:9223372036854775807
整型最大值:2147483647
㈣ 如何用Java比較兩個整數的大小
publicclassHello{
publicstaticvoidmain(String[]args){
System.out.println(getMax(2,10));
}
publicstaticintgetMax(inta,intb){
returna>b?a:b;
}
}