如果是剛接觸或者剛學(xué)習(xí)Java,練習(xí)一些基礎(chǔ)的算法還是必須的,可以提升思維和語法的使用。
1、輸出兩個(gè)int數(shù)中的最大值
-
import java.util.Scanner;
-
-
public class demo {
-
public static void main(String[] args) {
-
Scanner scanner = new Scanner(System.in);
-
System.out.println("請(qǐng)依次輸入兩個(gè)整數(shù):a,b(以空格隔開)");
-
/*比較兩個(gè)數(shù)的大小*/
-
int a = scanner.nextInt();
-
int b = scanner.nextInt();
-
int max;
-
if(a >= b){
-
max = a;
-
}else {
-
max = b;
-
}
-
System.out.println("最大值為"+max);
-
}
-
}
-
}
2、輸出三個(gè)int數(shù)中的最大值
-
package demo;
-
-
import java.util.Scanner;
-
-
public class demo {
-
public static void main(String[] args) {
-
Scanner scanner = new Scanner(System.in);
-
System.out.println("請(qǐng)依次輸入兩個(gè)整數(shù):a,b(以空格隔開)");
-
int a = scanner.nextInt();
-
int b = scanner.nextInt();
-
int c = scanner.nextInt();
-
scanner.close();
-
/*方法一*/
-
int d=(a>b)?a:b;
-
int e=(d>c)?d:c;
-
System.out.println("最大值為"+e);
-
-
/*方法二*/
-
if(a>b && a>c){
-
System.out.println("最大值為"+a);
-
}else if(b>c && b>a){
-
System.out.
|