📜  不使用分号打印分号的Java程序

📅  最后修改于: 2022-05-13 01:54:39.440000             🧑  作者: Mango

不使用分号打印分号的Java程序

给定的任务是打印一个 ' ; ' 不使用 ' ; ' 代码中的任何地方。解决这个有趣问题的最简单的想法是在程序中以某种方式使用 ASCII 值。但是由于Java不支持宏,不像C/C++,所以要去掉' ;'在代码中每一行的末尾是在条件语句中使用,if-statement 或 while 循环和PrintStreamprintf (String, Object) 方法。这里的主要逻辑是printf()返回非 null 的 PrintStream 对象,因此将其与 null ( Java中的字面量)进行比较,我们将获得所需的输出。

方法一:使用if语句

Java
// Java program to print ; without using ;
// using if statement
  
class PrintSemicolonExample {
    public static void main(String[] args)
    {
        // The ASCII value for semicolon is 59
        if (System.out.printf("%C", 59) == null) {
        }
    }
}


Java
// Java program to print ; without using ;
// using while loop
  
class PrintSemicolonExample {
    public static void main(String[] args)
    {
        while ((System.out.printf("%C", 59) == null)) {
        }
    }
}


输出
;

方法二:使用while循环

Java

// Java program to print ; without using ;
// using while loop
  
class PrintSemicolonExample {
    public static void main(String[] args)
    {
        while ((System.out.printf("%C", 59) == null)) {
        }
    }
}
输出
;