Development Tip

Scala에서 사용자로부터 입력을받는 방법은 무엇입니까?

yourdevel 2020. 11. 23. 20:16
반응형

Scala에서 사용자로부터 입력을받는 방법은 무엇입니까?


사용자의 의견을 받고 싶습니다. Scala에서 문자열로 사용자 입력을 요청하는 방법을 알려주시겠습니까?


Scala 2.11에서 사용

scala.io.StdIn.readLine()

사용되지 않는 Console.readLine.


로부터 스칼라 maling 목록 (서식 및 링크가 업데이트되었습니다)

짧은 답변:

readInt

긴 대답 :

터미널에서 읽으려면을 확인하십시오 Console.scala. 다음과 같은 기능을 사용할 수 있습니다.

Console.readInt

또한 사용자의 편의 Predef.scala를 위해는의 기능에 대한 일부 바로 가기를 자동으로 정의합니다 Console. 의 항목 Predef은 항상 어디서나 자동으로 가져 오기 때문에 다음과 같이 사용할 수 있습니다.

readInt

다음은 정수 값을 읽는 표준 방법입니다.

val a=scala.io.StdIn.readInt()
println("The value of a is "+ a)

비슷하게

def readBoolean () : Boolean stdin에서 전체 행에서 부울 값을 읽습니다.

def readByte () : Byte stdin에서 전체 행에서 Byte 값을 읽습니다.

def readChar () : Char stdin에서 전체 행에서 Char 값을 읽습니다.

def readDouble () : Double stdin에서 전체 행에서 Double 값을 읽습니다.

def readFloat () : Float stdin에서 전체 행에서 Float 값을 읽습니다.

def readInt () : Int stdin에서 전체 행에서 Int 값을 읽습니다.

def readLine (text : String, args : Any *) : String 형식화 된 텍스트를 stdout에 인쇄하고 stdin에서 전체 행을 읽습니다.

def readLine () : String stdin에서 전체 행을 읽습니다.

def readLong () : Long stdin에서 전체 행에서 Long 값을 읽습니다.

def readShort () : Short stdin에서 전체 행에서 Short 값을 읽습니다.

def readf (format : String) : List [Any] 형식 지정자에 지정된대로 stdin에서 구조화 된 입력을 읽습니다.

def readf1 (format : String) : Any 형식 지정자에 지정된대로 stdin에서 구조화 된 입력을 읽고 형식 지정에 따라 추출 된 첫 번째 값만 반환합니다.

def readf2 (format : String) : (Any, Any) 형식 지정자에 지정된대로 stdin에서 구조화 된 입력을 읽고 형식 지정에 따라 추출 된 처음 두 값만 반환합니다.

def readf3 (format : String) : (Any, Any, Any) 형식 지정자에 지정된대로 stdin에서 구조화 된 입력을 읽고 형식 사양에 따라 추출 된 처음 세 개의 값만 반환합니다.

마찬가지로 동일한 줄에서 여러 사용자 입력을 읽으려면 예 : 이름, 나이, 체중 스캐너 개체를 사용할 수 있습니다.

import java.util.Scanner

// simulated input
val input = "Joe 33 200.0"
val line = new Scanner(input)
val name = line.next
val age = line.nextInt
val weight = line.nextDouble

Scala Cookbook : Recipes for Object-Oriented and Functional Programming by Alvin Alexander에서 요약


object InputTest extends App{

    println("Type something : ")
    val input = scala.io.StdIn.readLine()
    println("Did you type this ? " + input)

}

이렇게하면 입력을 요청할 수 있습니다.

scala.io.StdIn.readLine()

사용자 입력을 읽는 간단한 예

val scanner = new java.util.Scanner(System.in)

scala> println("What is your name") What is your name

scala> val name = scanner.nextLine()
name: String = VIRAJ

scala> println(s"My Name is $name")
My Name is VIRAJ

또한 Read Line을 사용할 수 있습니다.

val name = readLine("What is your name ")
What is your name name: String = Viraj

readLine을 사용하면 사용자에게 프롬프트를 표시하고 입력을 문자열로 읽을 수 있습니다.

val name = readLine("What's your name? ")

에서 불꽃이 :

import java.io._
object Test {
    // Read user input, output
    def main(args: Array[String]) {

        // create a file writer
        var writer = new PrintWriter(new File("output.txt"))

       // read an int from standard input
       print("Enter the number of lines to read in: ")
       val x: Int = scala.io.StdIn.readLine.toInt

       // read in x number of lines from standard input
       var i=0
       while (i < x) {
           var str: String = scala.io.StdIn.readLine
           writer.write(str + "\n")
           i = i + 1
       }

       // close the writer
       writer.close
     }
}

이 코드는 사용자로부터 입력을 받아 출력합니다.

[input] Enter the number of lines to read in: 2
one
two

[output] output.txt
one
two

readLine ()을 사용하여 사용자 문자열 입력을받을 수 있습니다.

object q1 {
  def main(args:Array[String]):Unit={  
    println("Enter your name : ")
    val a = readLine()
    println("My name is : "+a)
  }
}

또는 스캐너 클래스를 사용하여 사용자 입력을받을 수 있습니다.

import java.util.Scanner;

object q1 {
  def main(args:Array[String]):Unit={ 
      val scanner = new Scanner(System.in)
    println("Enter your name : ")
    val a = scanner.nextLine()
    println("My name is : "+a)
  }
}

시도하십시오

스칼라> readint

이 방법을 시도하십시오

참고 URL : https://stackoverflow.com/questions/5055349/how-to-take-input-from-a-user-in-scala

반응형