2016-04-10 18 views
-1

Her deyimin kaç kez kullanıldığı bir anahtar deyimi kullanmaya çalışıyorum. Şimdiye kadar ardışık ifadelerle ilgili bir sorun yaşıyorum ";" ile ayrılmalıdır Hatayı kontrol ettim ve şu ana kadar bulabildiğim tek şey, kapalı parantezlerle ilgili bir sorun olduğudur, ancak tüm parantezlerim kapalı gibi görünüyor.Bir durumdan anahtar durumu bildirimi

func switchInt() -> Array<Int> { 

    var switchAr = [Int]() 
    var lowSwitch = 0 
    var medSwitch = 0 
    var highSwitch = 0 

    for _ in 1...10 { 

     let switchInt = Int(arc4random_uniform(100)) 

     switch switchInt { 

     case 0...35: 
      return lowSwitch +=1 
      switchAr.append(switchInt) 
     case switchAr 35<=67: 
      medSwitch +=1 
      switchAr.append(switchInt) 
     case switchAr 70<=100: 
      highSwitch; +=1 
      switchAr.append(switchInt) 

     default: 
      print("does not compute") 
     } 

    } 

    return(switchAr) 
} 
+1

artırmak için birkaç değişiklik yaptık notu neredeyse doğru ilk durum var ve her şey karışacak var bundan sonra. – nhgrif

+0

'highSwitch; + = 1' 'HighSwitch + = 1' olarak değiştirin –

cevap

-1

En azından bu derler, ama bununla ne yapmak istiyorsunuz emin değilim:

sizin koduyla epeyce temel sorunları var
func switchInt() -> Array<Int> { 

    var switchAr = [Int]() 
    var lowSwitch = 0 
    var medSwitch = 0 
    var highSwitch = 0 

    for _ in 1...10 { 

     let switchInt = Int(arc4random_uniform(100)) 

     switch switchInt { 

     case 0...35: 
      lowSwitch = lowSwitch + 1 
      switchAr.append(switchInt) 
     case 36..<67: 
      medSwitch = medSwitch + 1 
      switchAr.append(switchInt) 
     case 70..<100: 
      highSwitch = highSwitch + 1 
      switchAr.append(switchInt) 

     default: 
      print("does not compute") 
     } 

    } 

    return(switchAr) 
} 
+0

Bu bir öğrenme alıştırmasıdır. –

0

, bu (şöyle olmalıdır i programın yürütülmesini etkilemez okunabilirliği) .:

func switchInt() -> [Int] { //Short hand syntax for an array type. 
    var switchAr = [Int]() //Space is unnecessary. 
    var lowSwitch = 0 
    var medSwitch = 0 
    var highSwitch = 0 

    for _ in 1...10 { 

     let switchInt = Int(arc4random_uniform(100)) //NOTE: This will not include 100 it will give you a random number from 0 - 99. 

     switch switchInt { 

     case 0...35: 
      lowSwitch += 1 //Removed return statement as you were attempting to return an int when the exepected return type is an array of Int. 
      switchAr.append(switchInt) 
     case 35 <= 67: //Removed reference to switchAr as that is unnecessary. 
      medSwitch +=1 
      switchAr.append(switchInt) 
     case 70 <= 100: //Same as above. 
      highSwitch +=1 //Removed semicolon. 
      switchAr.append(switchInt) 

     default: 
      print("does not compute") 
     } 

    } 

    return switchAr //Removed pranas around expression following return. 
}