Advertisement
Go-Ice

Sophomore Java Homework-01

Sep 30th, 2014
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.94 KB | None | 0 0
  1. /**
  2.  * Date:2014.09.30
  3.  * @author LinChuWen
  4.  * NCHU EE,course number:2335
  5.  * course name:Object Oriented Language
  6.  *
  7.  * Problem:Write a program that reads in an integer
  8.  *          and breaks it into a sequence of individual digits.
  9.  *          For example, the input 16384 is displayed as 1 6 3 8 4
  10.  *          You may assume that the input is not negative.
  11.  */
  12. import java.util.*;
  13. public class HW1 {
  14.  
  15.     public static void main(String[] args) {
  16.         int cnt;
  17.         int number;
  18.         int[] array = new int[10];
  19.         Scanner input = new Scanner(System.in);
  20.        
  21.         try{
  22.         System.out.print("Please enter an integer: ");
  23.         number = input.nextInt();
  24.         int length = String.valueOf(number).length();
  25.        
  26.         for(cnt=0;cnt<length;cnt++){
  27.             array[cnt]=number%10;
  28.             number/=10;
  29.         } //for end
  30.        
  31.         for(cnt=length-1;cnt>=0;cnt--){
  32.             System.out.print(array[cnt]+" ");
  33.         } //for end
  34.         } //try end
  35.        
  36.         finally{
  37.             input.close();
  38.         } //finally end
  39.     } //main end
  40.  
  41. } //HW1 end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement