黑马程序员技术交流社区

标题: 一个看似很简单的面试题,我没做得出来 [打印本页]

作者: 张庚    时间: 2013-2-2 12:33
标题: 一个看似很简单的面试题,我没做得出来
本帖最后由 张向辉 于 2013-2-2 17:23 编辑

package ifjg;
public class Tex {
public static void main(String[] args) {
  if(___________){
   System.out.println("world");
  }
  else{
   System.out.println("hello");
  }
}
}
只能在if(_____)里面写代码,如何让输出结果是“hello world”?
作者: 杨杨    时间: 2013-2-2 13:45
String[] args 这里是关键 main函数 的参数
(String[] arr):函数的参数,参数类型是一个数组,该数组中的元素是字符串。字符串类型的数组。
也就是我我们在运行 java 类名的时候后面加参数
作者: 黑马刘向阳    时间: 2013-2-2 14:04
  1. public class Tex {
  2. public static void main(String[] args) {
  3.   if(new Tex(){

  4.         public boolean method() {
  5.                 // TODO Auto-generated method stub
  6.                 System.out.print("hello ");
  7.                 return true;
  8.         }
  9.           
  10.   }.method()){
  11.    System.out.println("world");
  12.   }
  13.   else{
  14.    System.out.println("hello");
  15.   }
  16. }
  17. }
复制代码
不知这样可以不?
作者: 张庚    时间: 2013-2-2 14:13
这样感觉怪怪的  
作者: 鲁柯    时间: 2013-2-2 14:25
终于成功了,试了大概有10来分钟吧,要完成输出hello world的效果,就要在判断语句中将另一部分输出
代码贴上,上面注释的那句是实验用的,不能用,刚开始想的方法是在args数组中动脑筋,发现不太好弄
class  Test
{
    public static void main(String[] args)
    {
        //if(System.out.equals("hello") || System.out.printf("hello")){
        if (System.out==System.out.printf("hello "))
        {
            System.out.println("world");
        }
        else{
            System.out.println("hello");
        }
    }
}

作者: 王溢君    时间: 2013-2-2 15:14
内容都是人才啊!!!!!
作者: 苏克    时间: 2013-2-2 15:50
if(System.out.format("%s","hello")!=null){
                        System.out.println("world!");
                }else{
                        System.out.println("hello");
                }
开始也没想到,后来查API找到的。

作者: 苏克    时间: 2013-2-2 15:55
if(System.out.append("hello")!=null){
                        System.out.println("world!");
                }else{
                        System.out.println("hello");
                }
这个也可以,只要是输出后有返回值的都可以,做到
作者: 朱玉玺    时间: 2013-2-2 17:36
  1. public class Tex{
  2. public static void main(String[] args) {
  3.           if(new Object(){
  4.                   public String toString(){
  5.                           System.out.print("hello ");
  6.                           return "你好";
  7.                   }
  8.           }.toString()!=null){
  9.            System.out.println("world");
  10.           }
  11.           else{
  12.            System.out.println("hello");
  13.           }
  14. }
  15. }
复制代码

作者: 朱玉玺    时间: 2013-2-2 17:37
黑马刘向阳 发表于 2013-2-2 14:04
不知这样可以不?

呵呵,跟我想到一块去了
作者: 朱玉玺    时间: 2013-2-2 17:45
里边只要new 一个类,重新类中某一个有返回值的方法,在该方法中加入打印语句,然后调用该方法,并让利用返回值创造一个判断条件,使其为真就ok了,随便定义都new一个对象都行~,
作者: 郭嘉    时间: 2013-2-2 18:22
  1. public class Tex{
  2.         public static void main(String args[])
  3.         {
  4.                 if((boolean)new Object(){
  5.                         public boolean equals(Object a){
  6.                                 System.out.print((String)a+" ");
  7.                                 return true;
  8.                         }}.equals("Hello")
  9.                 )
  10.                 {System.out.println("world");}
  11.                 else{
  12.                         System.out.println("hello");
  13.                 }
  14.         }
  15. }
复制代码
方法笨了点~~{:soso_e117:}




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2