已知字符‘\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
}
}
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
}
}
public static String getBell(){
char ch = 7;
return Character.valueOf( ch ).toString();
}
这个行不?
System.out.println("嘟");
}
public static String getBell(){
//前面一堆\u0007只是迷惑而已,最终 响应的始终是char,
return new String(new byte[]{7});
}
public static String getBell() {
java.awt.Toolkit.getDefaultToolkit().beep();
return "anything you like";
}
return new String(new byte[]{(byte) Thread.currentThread().getStackTrace()[1].getMethodName().length()});
}
不是说过了"不可以通过int, char等基本数据类型做强制转换"么, 你这个和
return new String(new byte[]{7})有啥区别阿?
StringBuffer s=new StringBuffer("7000u\\");;
return s.reverse().toString();1.没有出现“\u0007”字符串,(嘿没告诉我倒序不行)
2.没有通过int, char等基本数据类型做强制转换
3.更没用任何的IO操作以及反序列化操作 ^v^
System.out.println("\u0007");
}这个代码为啥我就没有嘟一声呢,这只是个不可见字符啊
还是我的电脑不支持?
char ch = 7;
return Character.valueOf( ch ).toString();
}
1、不能用嘴发
2、也不能用喉咙发
3、更不能使用工具看来楼主只能放个屁了哈哈,开个玩笑
就算用屁发出了“度”也声也不知道有意义啊。本人是菜鸟,上网搜索了一下,楼主看看服不符合要求?
http://sunwenran.iteye.com/blog/706458
public static String getBell(){
return s;
}
public static String getBell() {
int x = new Random().nextInt();
int y = ~((~x + x) << 3);
return Character.valueOf((char) y).toString();
}
BufferedReader stdin =new BufferedReader(new InputStreamReader(System.in));
return stdin.readLine();
}
然后输入\u0007 呵呵
这个答案行不?
// to be implemented
Scanner s = new Scanner(System.in);
return s.next();
}在控制台输入\u0007
System.out.println(getBell());
} public static String getBell(){
return a;
}
}
//to be implemented
return (char)(System.class.getName().size() + 1);
}
//to be implemented
return new String((char)(System.class.getName().size() + 1));
}
估计他是在 Eclipse 的控制台中运行的,需要到操作系统的控制台中运行才可以听到主板的鸣叫。
看看Java Puzzler 这本书, 那个题目不是很"无聊"的?
题目旨在考查对java的了解, 仅此而已
return "".replaceAll("", "\7");
题目还能具体些么?这个算出现\u0007了么
不知是主板喇叭发声还是音响发声啊!
不会是调用发声器吧!例如:
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);
}
}
} 别人的代码——只是举个例子 !
System.out.println("\7".equals("\u0007")) 得到的结果是true, 不过是用8进制来表示这个字符罢了,这两个是一回事
win和lin没有音箱也会发声了!
真正的谜题应该是思考方法的问题。
我同意你关于“谜题”的论点,但是如果你读过java puzzler这本书,就会发现里面很多所谓的谜题就是考查一些很不常用的feature,并不是考查你思考的方法。也许我将它命名成“谜题”不合适,但是你就将就着看吧
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()+"";
}
}
这是上面有位仁兄的代码·这都不行吗?
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
public class A {
public static void main(String[] args) throws Exception {
System.out.println(getBell());
} public static String getBell() {
return "";
}
}
囧....
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();
}
合题意了没有呢?
a();
}
public static String a(){
System.out.println("\u0007");
}
getBell没违规哦,、、、、、