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.



import java.util.Scanner;

public class C1 
{
 public static void main(String[] args) 
 {
  Scanner input = new Scanner(System.in);
  
  System.out.println("請輸入3個數字:");
  double n1 = input.nextDouble();
  double n2 = input.nextDouble();
  double n3 = input.nextDouble();
  
  displaySortedNumbers(n1,n2,n3);
 }

 public static void displaySortedNumbers(double num1 ,double num2 ,double num3 )
 {
  //比較並印出結果
  if(num1 > num2)
  {
   if( num1 >= num3)
   {
    if(num2 >= num3 )
    {
     System.out.println(num1+ " " + num2 + " " +num3); 
    }
    else if(num3 >= num2 )
    {
     System.out.println(num1+ " " + num3 + " " +num2); 
    }    
   }
  }
  else if(num2 > num3)
  {
   if( num2 >= num1)
   {
    if(num3 >= num1 )
    {
     System.out.println(num2+ " " + num3 + " " +num1);
    }
    else if(num1 >= num3 )
    {
     System.out.println(num2+ " " + num1 + " " +num3);
    }    
   }
  }
  else if(num3 > num1)
  {
   if( num3 >= num2)
   {
    if(num1 >= num2 )
    {
     System.out.println(num3+ " " + num1 + " " +num2);
    }
    else if(num2 >= num1 )
    {
     System.out.println(num3+ " " + num2 + " " +num1);
    }    
   }
  }
  else if(num1 == num2 && num2 == num3)
  {
   System.out.println(num1+ " " + num2 + " " +num3);
  }  
 } 
}

沒有留言:

張貼留言

Go(Golang)程式語言 設定GCC

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