【程式34】 題目:輸入3個數a,b,c,按大小順序輸出。
阿新 • • 發佈:2019-02-03
/* 2017年3月9日17:09:13 java基礎50道經典練習題 例34 Athor: ZJY Purpose: 【程式34】 題目:輸入3個數a,b,c,按大小順序輸出。 程式分析:利用指標方法。 */ import java.util.Scanner; public class ProgramNo34_1 { public static void main(String[] args) { System.out.print("請輸入要比較的三個數: "); Scanner sc = new Scanner(System.in).useDelimiter("\\s"); int a = sc.nextInt(); int b = sc.nextInt(); int c = sc.nextInt(); sc.close(); compare(a, b, c); } private static void compare(int a, int b, int c) { if(a > b) { a = a+b; b = a-b; a = a-b; } if(b > c) { b = b+c; c = b-c; b = b-c; } if(a > c) { a = a+c; c = a-c; a = a-c; } System.out.println(a+"<"+b+"<"+c); } }
/* 2017年3月9日17:09:13 java基礎50道經典練習題 例34 Athor: ZJY Purpose: */ import java.util.Scanner; public class ProgramNo34_2 { public static void main(String[] args){ System.out.print("請輸入3個數:"); Scanner scan = new Scanner(System.in).useDelimiter("\\s"); int a = scan.nextInt(); int b = scan.nextInt(); int c = scan.nextInt(); scan.close(); if(a<b){ int t = a; a = b; b = t; } if(a<c){ int t = a; a = c; c = t; } if(b<c){ int t = b; b = c; c = t; } System.out.println(a+" "+b+" "+c); } }