zoukankan      html  css  js  c++  java
  • Hello,World

    引用自http://www.cnblogs.com/jbelial/archive/2013/05/08/3067471.html#2676127

    题目:

    复制代码
    1 public class text {
    2     public static void main(String[] args) {
    3         if ( 你的代码 )
    4             System.out.print("hello ");
    5         else 
    6             System.out.println("world");
    7     } 
    8 }
    复制代码

      能使他打印“hello world” 。

      

      可使用的方法如下:

      (一)使用匿名内部类:

    复制代码
    1 public class HelloWorld {
    2     public static void main(String... a) {
    3        if (new Object(){{System.out.print("Hello,");}} == null) {
    4            System.out.print("Hello,");
    5        } else {
    6            System.out.println("World");
    7        }
    8     }
    9 }
    复制代码

      (二)利用PrintStream的append或format等方法(append处改为format):

    复制代码
    1 public class HelloWorld {
    2     public static void main(String... a) {
    3        if (System.out.append("Hello,") == null) {
    4            System.out.print("Hello,");
    5        } else {
    6            System.out.println("World");
    7        }
    8     }
    9 }
    复制代码

      (三)利用反射调用System.out.print 

    复制代码
    public class text {
        public static void main(String[] args) throws Exception {
            if ( System.out.getClass().getMethod("print",String.class).invoke(System.out, "Hello ")!=null)
                System.out.print("hello ");
            else 
                System.out.println("world");
        } 
    }
    复制代码

      (四)利用jdk1.5的printf方法 

    复制代码
    1 public class HelloWorld {
    2     public static void main(String... a) throws Exception {
    3        if (System.out.printf("Hello,") == null) {
    4            System.out.print("Hello,");
    5        } else {
    6            System.out.println("World");
    7        }
    8     }
    9 }
    复制代码
    (五)匿名内部类
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    package com.iotest;
     
    public class TestIm {
     
         
        public static void main(String[] args) {
            if(new Thread(){ 
                
                    this.start(); 
                
                @Override 
                public void run() { 
                    System.out.print("world");
                
            }.isAlive()){
                System.out.print("hello ");
            }else{
                System.out.println("world");
            }
        }
     
         
    }
  • 相关阅读:
    LeetCode | Contains Duplicate III
    LeetCode | Contains Duplicate II
    LeetCode | Contains Duplicate
    LeetCode | Find Minimum in Rotated Sorted Array II
    CentOS下使用yum快速安装memcached
    centos6.5安装Apache+MySQL+PHP
    centos 防火墙开放80端口
    jeecms搜索结果排序-二次开发
    JS常用的标准函数
    spring download
  • 原文地址:https://www.cnblogs.com/jym-sunshine/p/4689376.html
Copyright © 2011-2022 走看看