A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 张庚 中级黑马   /  2013-2-2 12:33  /  2428 人查看  /  13 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 张向辉 于 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”?

评分

参与人数 1黑马币 +3 收起 理由
Rancho_Gump + 3

查看全部评分

13 个回复

正序浏览
  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:}
回复 使用道具 举报
里边只要new 一个类,重新类中某一个有返回值的方法,在该方法中加入打印语句,然后调用该方法,并让利用返回值创造一个判断条件,使其为真就ok了,随便定义都new一个对象都行~,
回复 使用道具 举报
黑马刘向阳 发表于 2013-2-2 14:04
不知这样可以不?

呵呵,跟我想到一块去了
回复 使用道具 举报
  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. }
复制代码
回复 使用道具 举报
if(System.out.append("hello")!=null){
                        System.out.println("world!");
                }else{
                        System.out.println("hello");
                }
这个也可以,只要是输出后有返回值的都可以,做到

评分

参与人数 1黑马币 +3 收起 理由
Rancho_Gump + 3

查看全部评分

回复 使用道具 举报
if(System.out.format("%s","hello")!=null){
                        System.out.println("world!");
                }else{
                        System.out.println("hello");
                }
开始也没想到,后来查API找到的。

评分

参与人数 1黑马币 +3 收起 理由
Rancho_Gump + 3

查看全部评分

回复 使用道具 举报
内容都是人才啊!!!!!
回复 使用道具 举报
终于成功了,试了大概有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");
        }
    }
}

评分

参与人数 1黑马币 +3 收起 理由
Rancho_Gump + 3

查看全部评分

回复 使用道具 举报
这样感觉怪怪的  
回复 使用道具 举报
  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. }
复制代码
不知这样可以不?

评分

参与人数 1黑马币 +3 收起 理由
Rancho_Gump + 3

查看全部评分

回复 使用道具 举报
String[] args 这里是关键 main函数 的参数
(String[] arr):函数的参数,参数类型是一个数组,该数组中的元素是字符串。字符串类型的数组。
也就是我我们在运行 java 类名的时候后面加参数
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马