當前位置:首頁 » 編程語言 » java三數大小

java三數大小

發布時間: 2023-08-21 23:00:00

Ⅰ 在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三個變數怎麼比較大小

有好幾種。第一種,將三個數用sort排序,則,三個數的大小就出來了。第二種用數學方法,調用靜態變數Math.max();最大的數就出來了!第三種就是寫個函數,先比較兩個數的大小,再與第三個數比較,代碼如下:
import java.util.*;
public class Ex3
{
public static int MAX(int x,int y){
if(x>y)
return x;
else
return y;
}
public static void main(String[] args){
System.out.println("比較三個數的最大值和最小值");
Scanner sc =new Scanner(System.in);
int x = sc.nextInt();
int y = sc.nextInt();
int z = sc.nextInt();
System.out.println("最大的數是:" + MAX(MAX(x,y),z));
}
}

Ⅲ java三個數排序比較大小的完整代碼,並給出詳細解釋,初學者,謝謝

import java.util.Arrays;

import java.util.Collection;

public class Demo2 {

public static void main(String[] args) {

// 這是你的三個數

int[] arr = { 12, 32, 18 };

// 兩層嵌套循環

for (int i = 0; i < arr.length; i++) {

for (int j = 0; j < i; j++) {

// 如果後者小於前者,讓他們交換位置,一直循環

// 直到每個數字都從頭到尾跟數組里的每個數字比較一次

if (arr[i] < arr[j]) {

// 這三步就是交換位置,相信聰明的你一定看得懂了

arr[i] = arr[i] + arr[j];

arr[j] = arr[i] - arr[j];

arr[i] = arr[i] - arr[j];

}

}

}

//最後列印出來

for (int i = 0; i < arr.length; i++) {

System.out.println(arr[i]);

}

}

}

資料拓展:

Java是一門面向對象編程語言,不僅吸收了C++語言的各種優點,還摒棄了C++里難以理解的多繼承、指針等概念,因此Java語言具有功能強大和簡單易用兩個特徵。Java語言作為靜態面向對象編程語言的代表,極好地實現了面向對象理論

Ⅳ 用java語言程序,比較三個數的大小,輸出最大的那個數

你好,幫你寫了一個例子:
import java.lang.Math;
import java.util.Scanner;
public class max {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.println("Please input 3 integers: ");
int x = Integer.parseInt(keyboard.nextLine());
int y = Integer.parseInt(keyboard.nextLine());
int z = Integer.parseInt(keyboard.nextLine());
int max = Math.max(x,y);
if(max>y){ //suppose x is max then compare x with z to find max number
max = Math.max(x,z);
}
else{ //if y is max then compare y with z to find max number
max = Math.max(y,z);
}
System.out.println("The max of three is: " + max);
}
}

請記得採納哦!

Ⅳ JAVA 三個數比大小。給寫個main函數。學習下。

一共三個數(A、B、C)可以隨便拿一個數,比如A跟B比較, 如果 A > B 則再拿 A 如 C 比較,如果A > C 則 A 是最大數,此時比較C 與B,看誰小。代碼如果下

第一種方式:

if(A > B){
if(A > C){
System.out.println("最大的數是:" + A);
if(B > C){
System.out.println("最小的數是:" + C);
}else{
System.out.println("最小的數是:" + B);
}
}else{
System.out.println("最大的數是:" + C);
System.out.println("最小的數是:" + B);
}
}else{
if(B > C){
System.out.println("最大的數是:" + B);
if(A > C){
System.out.println("最小的數是:" + C);
}else{
System.out.println("最小的數是:" + A);
}
}else{
System.out.println("最大的數是:" + C);
System.out.println("最小的數是:" + A);
}
}

第二種方式:

//定義三個變數
int min = 0; //這里的值可以是隨意的值。
int max = 0;
int temp = 0;

temp = A > B ? A : B; //如果 A > B 則把 A 賦值給 temp,否則就把 B 賦值給 temp。也就是得到這兩個數中最大的數。

max = temp > C ? temp : C; //與上面得到的最大數比較,就可以得到這三個數中最大的數。求最小數與上面一樣道理,不寫注釋了,自己看。

System.out.println("最大的數是:" + max); //記得先輸出,因為變數下面還要用。

temp = A > B ? B : A;

min = temp > C ? C : temp;

System.out.println("最小的數是:" + min);

這樣寫是不是比上面寫簡單很多。

第三種上面已經有人寫了,就是用數組。代碼已經有了就不寫了。 希望你能理解,而不只是把代碼 Ctrl + C。加油。

Ⅵ java中3個數怎樣比較大小

public class Compare
{
public static void main(String args[])
{
bigger(55,55,55);
}
public static void bigger(int i,int j,int k)
{
if(i>j)
{
System.out.println("第一個數值要比第二個大,下面比較第一個和第三個的大小關系");
if(i<k)
{
System.out.println("第三個數值最大");
System.out.println("最大數值是"+k);
}
else if(i==k)
{
System.out.println("第一個數和第三個數一樣大,最大數值是"+k);
}
else
{
System.out.println("最大數值是第一個數,數值是"+i);
}
}
else if(i==j)
{
System.out.println("第一個數值和第二個數值一樣大");
if(i<k)
{
System.out.println("第三個數值最大");
System.out.println("最大數值是"+k);
}
else if(i==k)
{
System.out.println("三個數值一樣大,最大數值是"+k);
}
else
{
System.out.println("最大數值是第一個數或者是第二個數值,數值是"+i);
}
}
else
{
System.out.println("第二個數值比第一個數值大");
if(j>k)
{
System.out.println("最大數值是第二個數,最大數值為"+j);
}
else if(j==k)
{
System.out.println("第二個數值和第三個數值一樣大,最大數值是"+j);
}
else
{
System.out.println("最大數值是第三個數,最大數值為"+k);
}
}
}
}

熱點內容
死鎖避免的演算法 發布:2025-02-05 04:43:07 瀏覽:579
python查文檔 發布:2025-02-05 04:27:49 瀏覽:496
javaxmldom 發布:2025-02-05 04:27:40 瀏覽:9
linux修改內存大小 發布:2025-02-05 04:26:05 瀏覽:997
ftp命令復制文件 發布:2025-02-05 04:26:00 瀏覽:303
python好用的ide 發布:2025-02-05 04:14:18 瀏覽:516
id密碼開頭是多少 發布:2025-02-05 04:11:51 瀏覽:101
數據結構c語言ppt 發布:2025-02-05 04:11:45 瀏覽:43
如何用學習機配置的筆寫字 發布:2025-02-05 04:09:15 瀏覽:395
5歲編程 發布:2025-02-05 04:06:21 瀏覽:653