已知字符‘\u0007’是控制字符bell,打印该字符会有“嘟”的一声响。例如下面的代码:         public class A{
    public static void main(String[] args) throws Exception{
  System.out.println("\u0007");
             }
  }
一道变态的题目如下:实现一个静态方法getBell(), 使得下面程序发出“嘟”的一声响。要求方法体中不能出现字符串"\u0007", 也不可以通过int, char等基本数据类型做强制转换(比如,不可以String s = new String((char)'\u0007')), 也不可以使用任何的IO操作以及反序列化操作。         public class A{
    public static void main(String[] args) throws Exception{
  System.out.println(getBell());
             }             public static String getBell(){
                   //to be implemented
             }
  }

解决方案 »

  1.   


        public static String getBell(){
         char ch = 7;
         return Character.valueOf( ch  ).toString();
        }
    这个行不?
      

  2.   

    public static String getBell(){
        System.out.println("嘟");
    }
      

  3.   


    public static String getBell(){
    //前面一堆\u0007只是迷惑而已,最终 响应的始终是char,
      return new String(new byte[]{7});
    }
      

  4.   

    用pattern里的 \a 可以不
      

  5.   


    public static String getBell() {
    java.awt.Toolkit.getDefaultToolkit().beep();
    return "anything you like";
    }
      

  6.   

    我承认你这个实现符合题目的要求,但只能算是个workaround. 你是调用一个native的工具包发出的声响,并不是通过打印字符串发出的声响, 题目其实是希望getBell方法返回的结果里就包含\u0007字符
      

  7.   

    好吧,再来一个public static String getBell() {
        return new String(new byte[]{(byte) Thread.currentThread().getStackTrace()[1].getMethodName().length()});
    }
      

  8.   


    不是说过了"不可以通过int, char等基本数据类型做强制转换"么, 你这个和
    return new String(new byte[]{7})有啥区别阿?
      

  9.   

    return new String("倇".getBytes("unicode"),"iso8859-1");
      

  10.   

    用个比较弱的方法:
     StringBuffer s=new StringBuffer("7000u\\");;
     return s.reverse().toString();1.没有出现“\u0007”字符串,(嘿没告诉我倒序不行)
    2.没有通过int, char等基本数据类型做强制转换
    3.更没用任何的IO操作以及反序列化操作 ^v^
      

  11.   

    用数字7的ascii码-0的ascii码做返回值
      

  12.   

    public static void main(String[] args) throws Exception {
    System.out.println("\u0007");
    }这个代码为啥我就没有嘟一声呢,这只是个不可见字符啊
    还是我的电脑不支持?
      

  13.   

    用键盘输入那几个字符不久OK 了么 ?呵呵 行不行?LZ
      

  14.   

      public static String getBell(){
            char ch = 7;
            return Character.valueOf( ch  ).toString();
        }
      

  15.   

    我要楼主发出“嘟”一声,要求
    1、不能用嘴发
    2、也不能用喉咙发
    3、更不能使用工具看来楼主只能放个屁了哈哈,开个玩笑
    就算用屁发出了“度”也声也不知道有意义啊。本人是菜鸟,上网搜索了一下,楼主看看服不符合要求?
    http://sunwenran.iteye.com/blog/706458
      

  16.   

    private static String s = "\u0007";
    public static String getBell(){
                    return s;
    }
      

  17.   

    题目够变态,下面无论随机数x等于几,都保证y=7
      public static String getBell() {
        int x = new Random().nextInt();
        int y = ~((~x + x) << 3);
        return Character.valueOf((char) y).toString();
      }
      

  18.   

    原理:无论x等于几,(~x + x) 永远等于 -1
      

  19.   

    public static String getBell(){
      BufferedReader stdin =new BufferedReader(new InputStreamReader(System.in));
      return stdin.readLine();
      }
    然后输入\u0007  呵呵
      

  20.   

    Toolkit.getDefaultToolkit().beep();
    这个答案行不?
      

  21.   

    public static String getBell() {
    // to be implemented
    Scanner s = new Scanner(System.in);
    return s.next();
    }在控制台输入\u0007
      

  22.   

    public class A{        public static final String a="\u0007";//不知是否可以在静态变量中定义,应该不属于方法体中        public static void main(String[] args) throws Exception{
             System.out.println(getBell());
                 }             public static String getBell(){
                       return a;
                 }
         }
      

  23.   

    呵呵,取得类名字符串不就可以吗?例如(未测试):             public static String getBell(){
                       //to be implemented
    return (char)(System.class.getName().size() + 1);
                 }
      

  24.   

    手头没有编译器,改一下: public static String getBell(){
      //to be implemented
    return new String((char)(System.class.getName().size() + 1));
      }
      

  25.   


    估计他是在 Eclipse 的控制台中运行的,需要到操作系统的控制台中运行才可以听到主板的鸣叫。
      

  26.   

    说题目无聊的人, 那就请无视该帖子吧.
    看看Java Puzzler 这本书, 那个题目不是很"无聊"的?
    题目旨在考查对java的了解, 仅此而已
      

  27.   

    直接一个return的事情,有那么麻烦么?
      

  28.   


    return "".replaceAll("", "\7");
    题目还能具体些么?这个算出现\u0007了么
      

  29.   

    楼主的“嘟”的一声
    不知是主板喇叭发声还是音响发声啊!
    不会是调用发声器吧!例如:
    public class Beep {  
      
        public static JNative getBeepJnative() throws NativeException {  
            return new JNative("kernel32.dll", "Beep");  
        }  
      
        public static void beep(JNative jn, int freq, int dwruration)  
                throws NativeException, IllegalAccessException {  
            jn.setRetVal(Type.VOID);  
            jn.setParameter(0, freq);  
            jn.setParameter(1, dwruration);  
            jn.invoke();  
        }  
      
        public static void main(String[] args) throws NativeException,  
                IllegalAccessException {  
            // 控制声音频率  
            int freqs[] = { 523, 587, 659, 698, 784, 880, 998, 1047, 998, 880, 784,  
                    698, 659, 587, 523 };  
            JNative jn = Beep.getBeepJnative();  
            // 控制声音时长  
            int dwruration = 300;  
            for (int freq : freqs) {  
                Beep.beep(jn, freq, dwruration);  
            }  
        }  
    }  别人的代码——只是举个例子 !
      

  30.   


    System.out.println("\7".equals("\u0007")) 得到的结果是true, 不过是用8进制来表示这个字符罢了,这两个是一回事
      

  31.   

    这样做的话还跨平台了哩!!
    win和lin没有音箱也会发声了!
      

  32.   

    如果是考查对java的了解,那应该就是知识性的问题,不能算谜题了,因为不知道相关的知识,就不可能答对。
    真正的谜题应该是思考方法的问题。
      

  33.   


    我同意你关于“谜题”的论点,但是如果你读过java puzzler这本书,就会发现里面很多所谓的谜题就是考查一些很不常用的feature,并不是考查你思考的方法。也许我将它命名成“谜题”不合适,但是你就将就着看吧
      

  34.   

    public class HelloWord{
     public static void main(String[] args) throws Exception{
    System.out.println(getBell());
     } public static String getBell() throws Exception{
         //return new String(new byte[]{(byte) Thread.currentThread().getStackTrace()[1].getMethodName().length()});
    //return new String("倇".getBytes("unicode"),"iso8859-1");
    //return (Class.forName("HelloWord").getMethod("getBell").getName()).length()+"";
    }
    }
    这是上面有位仁兄的代码·这都不行吗?
      

  35.   

    如果是这样的话
    public class A{
            public static void main(String[] args) throws Exception{
             System.out.println(getBell());
                 }             public static String getBell(){
                       return "1\u0007".substring(1);//
    或者return "1\u0007".replace("1","")
                 }
         }
    "1\u0007".equals("\u0007")是false
      

  36.   

    不算变态吧,用几个数 与 或 运算,再赋给char型变量。
      

  37.   


    public class A {
    public static void main(String[] args) throws Exception {
    System.out.println(getBell());
    } public static String getBell() {
    return "";
    }
    }
    囧....
      

  38.   


    public static String getBeep() {

    class tmp extends ClassLoader{
    private tmp(){

    }
    byte[] arrs = new byte[]{-54, -2, -70, -66, 0, 0, 0, 50, 0, 20, 7, 0, 2, 1, 0, 4, 66, 101, 101, 112, 7, 0, 4, 1, 0, 16, 106, 97, 118, 97, 47, 108, 97, 110, 103, 47, 79, 98, 106, 101, 99, 116, 1, 0, 6, 60, 105, 110, 105, 116, 62, 1, 0, 3, 40, 41, 86, 1, 0, 4, 67, 111, 100, 101, 10, 0, 3, 0, 9, 12, 0, 5, 0, 6, 1, 0, 15, 76, 105, 110, 101, 78, 117, 109, 98, 101, 114, 84, 97, 98, 108, 101, 1, 0, 18, 76, 111, 99, 97, 108, 86, 97, 114, 105, 97, 98, 108, 101, 84, 97, 98, 108, 101, 1, 0, 4, 116, 104, 105, 115, 1, 0, 6, 76, 66, 101, 101, 112, 59, 1, 0, 8, 116, 111, 83, 116, 114, 105, 110, 103, 1, 0, 20, 40, 41, 76, 106, 97, 118, 97, 47, 108, 97, 110, 103, 47, 83, 116, 114, 105, 110, 103, 59, 8, 0, 17, 1, 0, 1, 7, 1, 0, 10, 83, 111, 117, 114, 99, 101, 70, 105, 108, 101, 1, 0, 9, 66, 101, 101, 112, 46, 106, 97, 118, 97, 0, 33, 0, 1, 0, 3, 0, 0, 0, 0, 0, 2, 0, 1, 0, 5, 0, 6, 0, 1, 0, 7, 0, 0, 0, 47, 0, 1, 0, 1, 0, 0, 0, 5, 42, -73, 0, 8, -79, 0, 0, 0, 2, 0, 10, 0, 0, 0, 6, 0, 1, 0, 0, 0, 2, 0, 11, 0, 0, 0, 12, 0, 1, 0, 0, 0, 5, 0, 12, 0, 13, 0, 0, 0, 1, 0, 14, 0, 15, 0, 1, 0, 7, 0, 0, 0, 45, 0, 1, 0, 1, 0, 0, 0, 3, 18, 16, -80, 0, 0, 0, 2, 0, 10, 0, 0, 0, 6, 0, 1, 0, 0, 0, 6, 0, 11, 0, 0, 0, 12, 0, 1, 0, 0, 0, 3, 0, 12, 0, 13, 0, 0, 0, 1, 0, 18, 0, 0, 0, 2, 0, 19};

    public String getInstance(){
    tmp loader = new tmp();
    Class c = loader.defineClass(null,arrs, 0, arrs.length);
    String s = null;
    try {
    s = Class.forName("Beep", true, loader).newInstance().toString();
    } catch (Exception e) {
    e.printStackTrace();
    }
    return s;
    }
    }
    return new tmp().getInstance();
    }
    合题意了没有呢?
      

  39.   

    可以使用BigInteger类其中定义了常量BigInteger.one和BigInteger.Ten
      

  40.   

    public static String getBell(){
     a();
    }
    public static String a(){
     System.out.println("\u0007");
    }
    getBell没违规哦,、、、、、
      

  41.   

    这真难为你了哈, 其实没必要搞这么复杂,用ClassLoader做了一次隐藏... 但这是一个可以接受的答案了