zoukankan      html  css  js  c++  java
  • java实例 把一个整形n强制转化为字符串

    n的范围是-100--100

    如果成功转换,则打印Good job ,若不成功则打印Wrong answer;

    import java.util.*;
    import java.security.*;
    public class Solution {
     public static void main(String[] args) {
    
      DoNotTerminate.forbidExit();
    
      try {
       Scanner in = new Scanner(System.in);
       int n = in .nextInt();
       in.close();
       //String s=???; Complete this line below
    String s=Integer.toString(n);
    
    if (n == Integer.parseInt(s)) {
        System.out.println("Good job");
       } else {
        System.out.println("Wrong answer.");
       }
      } catch (DoNotTerminate.ExitTrappedException e) {
       System.out.println("Unsuccessful Termination!!");
      }
     }
    }
    
    //The following class will prevent you from terminating the code using exit(0)!
    class DoNotTerminate {
    
     public static class ExitTrappedException extends SecurityException {
    
      private static final long serialVersionUID = 1;
     }
    
     public static void forbidExit() {
      final SecurityManager securityManager = new SecurityManager() {
       @Override
       public void checkPermission(Permission permission) {
        if (permission.getName().contains("exitVM")) {
         throw new ExitTrappedException();
        }
       }
      };
      System.setSecurityManager(securityManager);
     }
    }
  • 相关阅读:
    Java序列化与反序列化
    Java中的反射机制
    Java中读文件操作
    Java中写文件操作
    判断单链表是否有环
    Java继承与组合
    Java类初始化
    堆排序
    希尔排序
    插入排序
  • 原文地址:https://www.cnblogs.com/Angella/p/6528258.html
Copyright © 2011-2022 走看看