zoukankan      html  css  js  c++  java
  • 浅谈Processing中的 println() 打印输出函数[String]

    简单看一下Processing中的打印输出函数println()相关用法。

    部分源码学习

      /**
       * ( begin auto-generated from println.xml )
       *
       * Writes to the text area of the Processing environment's console. This is
       * often helpful for looking at the data a program is producing. Each call
       * to this function creates a new line of output. Individual elements can
       * be separated with quotes ("") and joined with the string concatenation
       * operator (+). See <b>print()</b> for more about what to expect in the output.
       * <br/><br/> <b>println()</b> on an array (by itself) will write the
       * contents of the array to the console. This is often helpful for looking
       * at the data a program is producing. A new line is put between each
       * element of the array. This function can only print one dimensional
       * arrays. For arrays with higher dimensions, the result will be closer to
       * that of <b>print()</b>.
       *
       * ( end auto-generated )
     * @webref output:text_area
     * @usage IDE
     * @see PApplet#print(byte)
     * @see PApplet#printArray(Object)
     */
      static public void println() {
        System.out.println();
      }
    
      /**
       * @param variables list of data, separated by commas
       */
      static public void println(Object... variables) {
    //    System.out.println("got " + variables.length + " variables");
        print(variables);
        println();
      }
    

    学习成效

    很明显,按照源码编写,就是调了System.out.println();,而且还拓展了形参类型Object... variables,如下面的用法:

    int a = 9;
    String b = "what";
    float c = 54.3f;
    println(a,b,c);
    

    输出:

    9 what 54.3
    

    也就是说在括号里,你可以陆续把需要打印的值加进去用“,”分割它自动以一个空格字符分割并连续输出
    当然基本写法应该是这样子(等价):

    println(a+" " + b + " " +c);
    

    还有一种占位符用法[1]:

    System.out.printf("%d %s %f",a,b,c);
    

    当然还可以用String类中的方法转换:

    String str = String.format("%d %s %f",a,b,c);
    println(str);
    

    还可以使用MessageFormat相关方法[2]:

    import java.text.MessageFormat;
    
    String message = MessageFormat.format("{0} {1} {2}", a,b,c);
    println(message);
    

    当然如果需求奇异苛刻,还可以自定义算法或者使用模板引擎,所谓“字符串模板”,请看参考文章[2]。

    尾声

    java语言确实是老了,不如新生代灵活,比如Kotlin,它自带的字符串模板就很好用,如下:

    val a = 9
    val b = "what"
    val c = 54.3f
    println("$a $b $c")
    

    不过既然java依旧是编程界的大佬,它的规则是经历了千锤百炼的,好好学,必有价值!~


    参考:
    [1] https://www.cnblogs.com/happyday56/p/3996498.html ------ java占位符使用
    [2] https://www.cnblogs.com/softidea/p/9140204.html ------ java 替换字符串模板(模板渲染)

  • 相关阅读:
    API WAVE 专栏
    PCM数据格式(转)
    Windows 下音频数据采集和播放(转)
    java实现FFT变换(转)
    用74HC165读8个按键状态(转)
    机器人局部避障的动态窗口法(dynamic window approach) (转)
    TLD视觉跟踪算法(转)
    FFT算法在单片机中的使用&&LCD12864驱动
    Oracle442个应用场景-----------Oracle数据库物理结构
    Swift具体解释之三----------函数(你想知道的都在这里)
  • 原文地址:https://www.cnblogs.com/sharpeye/p/14649765.html
Copyright © 2011-2022 走看看