2014年12月26日 星期五

java程式設計題目---數字處理【由大到小排序(使用method)】

Write a method with the following header to display three numbers in decreasing order:
public static void displaySortedNumbers(double num1, double num2, double num 3)
Write a test program that prompts the user to enter three numbers and invokes the method to display them in decreasing order.



  1. import java.util.Scanner;
  2.  
  3. public class C1
  4. {
  5. public static void main(String[] args)
  6. {
  7. Scanner input = new Scanner(System.in);
  8. System.out.println("請輸入3個數字:");
  9. double n1 = input.nextDouble();
  10. double n2 = input.nextDouble();
  11. double n3 = input.nextDouble();
  12. displaySortedNumbers(n1,n2,n3);
  13. }
  14.  
  15. public static void displaySortedNumbers(double num1 ,double num2 ,double num3 )
  16. {
  17. //比較並印出結果
  18. if(num1 > num2)
  19. {
  20. if( num1 >= num3)
  21. {
  22. if(num2 >= num3 )
  23. {
  24. System.out.println(num1+ " " + num2 + " " +num3);
  25. }
  26. else if(num3 >= num2 )
  27. {
  28. System.out.println(num1+ " " + num3 + " " +num2);
  29. }
  30. }
  31. }
  32. else if(num2 > num3)
  33. {
  34. if( num2 >= num1)
  35. {
  36. if(num3 >= num1 )
  37. {
  38. System.out.println(num2+ " " + num3 + " " +num1);
  39. }
  40. else if(num1 >= num3 )
  41. {
  42. System.out.println(num2+ " " + num1 + " " +num3);
  43. }
  44. }
  45. }
  46. else if(num3 > num1)
  47. {
  48. if( num3 >= num2)
  49. {
  50. if(num1 >= num2 )
  51. {
  52. System.out.println(num3+ " " + num1 + " " +num2);
  53. }
  54. else if(num2 >= num1 )
  55. {
  56. System.out.println(num3+ " " + num2 + " " +num1);
  57. }
  58. }
  59. }
  60. else if(num1 == num2 && num2 == num3)
  61. {
  62. System.out.println(num1+ " " + num2 + " " +num3);
  63. }
  64. }
  65. }

沒有留言:

張貼留言

Go(Golang)程式語言 設定GCC

下載MSYS2:  https://www.msys2.org/ 安裝 程式開始執行後輸入: pacman -Syu y y 在安裝目錄底下找到msys2.exe,雙擊執行 輸入指令: pacman -Su pacman -S --needed base-devel mingw-...