Advertisement
urksiful

A simple Random Algorithm with Square Method

Sep 8th, 2016
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.02 KB | None | 0 0
  1.  
  2. import java.util.Scanner;
  3.  
  4. /**
  5.  *
  6.  * @author Hantack
  7.  */
  8. public class Simulation {
  9.  
  10.     public int createTail(int n) {
  11.        
  12.         n *= n; String ns = String.valueOf(n);
  13.         ns = (!(ns.length() % 2 == 0)) ? "0" + ns : ns;
  14.         ns = ns.substring(((ns.length() / 2) - 2), ((ns.length() / 2) + 2));   
  15.         System.out.println(ns);
  16.         return Integer.parseInt(ns);    
  17.     }
  18.  
  19.     String input = "";
  20.  
  21.     public static void main(String[] args) {
  22.         Simulation sim = new Simulation();
  23.         Scanner xr = new Scanner(System.in);
  24.         sim.input = xr.nextLine();
  25.         try {
  26.             int n = Integer.parseInt(sim.input);
  27.             if (n > 999) {
  28.             for (int i = 0; i < 5; i++) {
  29.                 n = sim.createTail(n);
  30.             }
  31.  
  32.         } else {
  33.             System.out.println("Give a valid tail");
  34.         }
  35.         }catch(java.lang.NumberFormatException err){
  36.             System.out.println("Give a value in the range of Integer");
  37.         }
  38.        
  39.  
  40.     }
  41.  
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement