2014年12月26日 星期五

java程式設計題目---數字處理【數值反轉(使用method)】

Write a method with the following header to display an interger in reverse order:
public static int reverse(int number)
For example, reverse(3456) displays 6543. Write a test program that promotes the user to enter an integer and displays its reversal.



  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("請輸入整數:");
  9. int number = input.nextInt();
  10. System.out.println(reverse(number));
  11. }
  12. public static int reverse(int number)
  13. {
  14. int[] array = new int[10];
  15. int i = 0;
  16. int z = number;
  17. int count = 1;
  18. int t = 1;
  19. int result = 0;
  20. while(number > 0)
  21. {
  22. int a = number % 10;
  23. array[i] = a;
  24. z = number / 10 ;
  25. number = z;
  26. i++ ;
  27. count = count + 1;
  28. }
  29. for(int a = count - 2 ; a >= 0 ; a--)
  30. {
  31. int c = array[a] * t ;
  32. result = result + c;
  33. t = t * 10;
  34. }
  35. return result;
  36. }
  37. }

1 則留言:

  1. 更好的:
    package javaapplication24.簡單;

    import java.util.Scanner;

    public class JavaApplication24簡單 {
    public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.println("請輸入數字");
    int no = sc.nextInt();
    int sum = 0;
    while(no != 0) {
    int a = no % 10;
    sum = sum * 10 + a;
    no /= 10;
    }
    System.out.println(sum);
    }
    }

    回覆刪除

Go(Golang)程式語言 設定GCC

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