"Learn Scala" chapter II Problem solving

Source: Internet
Author: User

Rt.

Package Com.scalalearn.scala.main/** * Learn Scala 02 exercises */Object LEARNSCALA02 {//1. If a number is positive, its signum is 1; if it is negative, SIGNU 

  M is-1; if it is 0, then Signum is 0//write a function to calculate the value def execise1 (num:int): Int = {if (num > 0) 1 else if (num = = 0) 0 Else-1} 2. What is the value of an empty block expression. What is its type//value is (), the type is unit//3. It is pointed out that Scala is the case that the assignment statement X=y=1 is legal//is legal//4 in the case of the type of x for the unit. Write a scala version of//for for the following Java loops (int i
      =10;i>=0;i--) System.out.println (i) def execise4 (): unit={var i = ten while (i >= 0) {println (i) i = I-1}}//5. Write a procedure countdown (N:int), print a number from N to 0 def countdown (n:int): unit={var i = n while (i >= 0 {println (i) i = i-1}}//6. Write A For loop that calculates the product of the Unicode code for all the letters in the string//For example, the word literal for all letters in "Hello" is 9415087488 L def execise6 (input:string): BigInt = {var sum:bigint = 1 val length = Input.length for (i <-0 to length -1) {sum = sum * input (i). Tolong} sum}//7. The same is the problem of solving 6, but not enough to cycle (hint, stringops) def execise7 (Input:strin G: BigInt = {var Sum:bigint = 0 sum = Input.map (_.tolong). Product sum}//8th feel and 6,7 Repeat, so do not//9. Put the previous practice letter The number is changed to the recursive function Def execise9 (input:string,curindex:int): BigInt = {if (Curindex = = input.length-1) {input (Curindex). Tolong} else{execise9 (input,curindex+1) *input (Curindex). Tolong}}//10th, title is too long, mainly by definition write function def exec Ise10 (x:double,n:int): BigDecimal ={if (n = = 0) {1}else if (N < 0) {1/execise10 (x,-1*n)}else if (n% 2 = = 1) {x*execise10 (x,n-1)}else{execise10 (X,N/2) *execise10 (X,N/2)}} def main (Args:ar Ray[string]): Unit = {println ("================execise1==============") println (Learnscala02.execise1 (333)) PR Intln (Learnscala02.execise1 ( -23)) println (learnscala02.execise1 (0)) println ("================execise4===========

    = = = ") Learnscala02.execise4 () println (" ================execise5============== ") Learnscala02.countdown (3) println ("================execise6============== ") println (Learnscala02.execise6 (" Hello ")) println (" ================execise7============ = = ") println (Learnscala02.execise7 (" Hello ")) println (" ================execise9============== ") println (learns Cala02.execise9 ("Hello", 0)) println ("================execise10==============") println (Learnscala02.execise10 (2,
 -4)}}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.