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.



import java.util.Scanner;

public class C1 
{ 
 public static void main(String[] args) 
 {
  Scanner input = new Scanner(System.in);
  
  System.out.println("請輸入整數:");
  int number = input.nextInt();
  
  System.out.println(reverse(number));
  
 }
 
 public static int reverse(int number)
 {
  int[] array = new int[10];
  int i = 0;
  int z = number;  
  int count = 1;
  int t = 1;
  int result = 0;
  
  while(number > 0)
  {   
   int a = number % 10;
   array[i] = a;
   z = number / 10 ;
   number = z;
   i++ ;
   count = count + 1; 
  }
  
  for(int a = count - 2 ; a >= 0 ; a--)
  {
   int c = array[a] * t ;
   result = result + c;
   t = t * 10;   
  }  
  return result;  
 }
}

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-...