scala打印txt第一行_如何在Scala中打印行?

scala打印txt第一行

在Scala中打印行 (Printing a line in Scala)

To print a line in Scala, there are majorly three inbuilt methods that can help you print your string to the output string. You can use any of them in your code as they are inbuilt in Scala.

在Scala中打印行,主要有三种内置方法可以帮助您将字符串打印到输出字符串。 您可以在Scala内置的代码中使用它们中的任何一个。

  1. printf()

    printf()

  2. print()

    打印()

  3. println()

    println()

1)printf() (1) printf())

printf() method is the old traditional C programming type method that outputs formatted strings to the output screen. The syntax for printing statements using printf() method is:

printf()方法是旧的传统C编程类型方法,它将格式化的字符串输出到输出屏幕。 使用printf()方法打印语句的语法为:

    var rlno = 324;
    printf("Roll Number =  %d", rlno)

This will print, "Roll Number = 324".

这将显示“卷数= 324”

There are some limitations in this like does not provide a line break at the end of the statement, deals in traditional coding that is a bit tricky.

这样做有一些局限性,例如在语句的末尾不提供换行符,处理传统的编码有点棘手。

2)print() (2) print())

The print() method in Scala is just like print() one, but the method of the passing string is a bit different. For example,

Scala中的print()方法print()一样,但是传递字符串的方法有些不同。 例如,

var rlno = 324;
print("Roll Number = " + rlno);

The output will be the same as it was with printf().

输出将与printf()相同

This method is a bit easy to write for the programmer and it does not require passing references. But this does not add a line break at the end of the line.

对于程序员而言,此方法有点容易编写,并且不需要传递引用。 但这不会在行尾添加换行符。

3)println() (3) println())

The println() method is used to print a statement and add a line break to it at the end by default. All functioning is the same but your code will add a line break and the cursor will move to the next line. For example,

默认情况下, println()方法用于打印一条语句并在其末尾添加一个换行符。 所有功能都相同,但是您的代码将添加一个换行符,并且光标将移至下一行。 例如,

var rlno = 324;
println("Roll Number = " + rlno);

It will print the same output but will move the cursor to the next line of the output screen.

它将打印相同的输出,但是会将光标移到输出屏幕的下一行。

BONUS: How to add a line break or tab space in the output of your code?

奖金:如何在代码输出中添加换行符或制表符空间?

If you need to format your output code to make it look good. You can use basic newline character (\n) or a tab character (\t) sequence to add some indentation in your code.

如果您需要格式化输出代码以使其看起来不错。 您可以使用基本的换行符( \ n )或制表符( \ t )序列在代码中添加一些缩进。

  • To add new line to your output screen, use \n

    要将新行添加到输出屏幕,请使用\ n

  • To add a tab space to your output screen, use \t

    要在输出屏幕上添加制表符空间,请使用\ t

Example:

例:

object MyClass {
        def main(args: Array[String]) {
            //"\n" example
            //it will print "Happy learning!"" in a new line
            println("Hello world\nHappy learning!") 
            
            //"\t" example
            //it will print "Happy learning!"" after a tab space
            println("Hello world\tHappy learning!") 
      }
   }

Output

输出量

Hello world
Happy learning!
Hello world	Happy learning!


翻译自: https://www.includehelp.com/scala/how-to-print-a-line-in-scala.aspx

scala打印txt第一行

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值