简体   繁体   English

如何在scala中使用apache数学?

[英]how can I use apache math with scala?

I am trying to use apache math with scala but I am not able to run the examples from the documentation http://commons.apache.org/proper/commons-math/userguide/random.html 我正在尝试使用scala的apache数学,但我无法运行文档中的示例http://commons.apache.org/proper/commons-math/userguide/random.html

import math._


object Hello extends App {

  println("HELLO")

  RandomDataGenerator randomData = new RandomDataGenerator(); 
  //not found: value RandomDataGenerator
}

I am new to scala and java so please provide a detailed answer. 我是scala和java的新手,所以请提供详细的答案。

EDIT: I have created a new folder with the build.sbt . 编辑:我已经使用build.sbt创建了一个新文件夹。 If I run the command sbt console in that folder than the code seems to be working in the console. 如果我在该文件夹中运行命令sbt console ,而不是代码似乎在控制台中工作。 But now how can I can run the code on eclipse?? 但是现在我怎么能在eclipse上运行代码?

Apache project documentation tends to be terrible about explaining how to get started. Apache项目文档在解释如何入门方面往往很糟糕 For example, you'll see "Download" links everywhere that show you how to get the project code and jars. 例如,您将在任何地方看到“下载”链接,向您展示如何获取项目代码和jar。 Don't do this! 不要这样做! Use a proper build system that will manage your dependencies for you. 使用适当的构建系统来管理您的依赖项。 For this example I'll use SBT , but Maven would work just as well (although with a lot more verbosity). 对于这个例子,我将使用SBT ,但Maven也可以正常工作(虽然有更多的冗长)。

Once you've got SBT installed you can search Maven Central for "commons-math", which will take you here . 一旦你安装了SBT,你可以在Maven Central中搜索 “commons-math”,这将带你到这里 You'll see a "Scala SBT" button on the side; 你会在侧面看到一个“Scala SBT”按钮; click it and copy the text to a file called build.sbt : 单击它并将文本复制到名为build.sbt的文件中:

libraryDependencies += "org.apache.commons" % "commons-math3" % "3.3"

Okay, now you can start an SBT console with sbt console . 好的,现在您可以使用sbt console启动SBT sbt console Now you need to know the full path to the class you want, which of course is nowhere to be found in the Apache documentation, because that would be too convenient. 现在你需要知道你想要的类的完整路径,这当然在Apache文档中找不到,因为这太方便了。 With a little bit of Googling you'll find the following: 通过一点谷歌搜索你会发现以下内容:

import org.apache.commons.math3.random.RandomDataGenerator

And now you can create an instance: 现在您可以创建一个实例:

object Hello extends App {
  println("HELLO")

  val randomData = new RandomDataGenerator()

  println(randomData.nextLong(0, 100))
}

And you're done! 而且你已经完成了! Now any good Scala resource will give you an idea of how to accomplish whatever you want to do next. 现在任何好的Scala资源都会让您了解如何完成下一步的任何操作。

First of all, you might want to look up Scala's general syntactic rules, unlike Java for example, Scala doesn't start its variables with the type, nor is a semicolon required.. 首先,您可能想要查找Scala的一般语法规则,与Java不同,例如,Scala不会使用类型启动其变量,也不需要分号。

// Java:
int a = 5;
int b = 6;
System.out.println(a + b);

// Scala:
val a = 5
val b = 6
println(a + b)

So in case of your "problem", the solution is really just 因此,如果遇到“问题”,解决方案就是这样

val randomData = new RandomDataGenerator // empty parentheses can be omitted 

That, and your import might be wrong, since RandomDataGenerator is under org.apache.commons.math3.random.RandomDataGenerator , not math 那个,你的导入可能是错的,因为RandomDataGeneratororg.apache.commons.math3.random.RandomDataGenerator下,而不是math

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM