2016-06-28 9 views
0

非常に初心者の方にはごめんなさい...私はSwiftをコードすることを学んでいます。 私は変数を定義して、その値に基づいて条件付きメッセージを出力しています。変数を別の値に変更すると、メッセージ文字列が変更されると予想されますが、変更されません。私は間違って何をしていますか? これはコードです:Swiftの変数変更時に文字列を更新する方法

//: Playground - noun: a place where people can play 

import Cocoa 

var str = "Hello, playground" 


var townname = "Azadinos" 
var population: Int = 5422 
var message: String 
var Haspostoffice: Bool = true 

if population < 10000 { 
     message = "with a population of \(population), \(townname) is a small town" 
} else if population >= 10000 && population < 15000 { 
     message = "with a population of \(population), \(townname) is a medium sized town!" 
}else {message = "\(townname) is a huge town!"} 

print (message) 

population = 250000 

print (population) 

print(message) 

私は2番目のメッセージは、最初のものとは異なっているが、それがないことを期待します。私は間違って何をしていますか? ありがとうございます

+0

簡単な答えは:あなたは、新しい 'population'値に基づいて、それを更新していない、変数' message'で設定します。 – MaddTheSane

答えて

0

あなたは​​内でそれらの値を含めることによりconstants, variables, literals, and expressionsの混合組み合わせから新しいstring価値を置くのに役立ちます。上記のドキュメントのように例えば:

let multiplier = 3 //This is constant 
let message = "\(multiplier) times 2.5 is \(Double(multiplier) * 2.5)" 
// message prints : "3 times 2.5 is 7.5" 

ここにあなたの作業コードは次のとおりです。

var townname = "Azadinos" 
var population: Int = 5422 
var message: String 
var Haspostoffice: Bool = true 

if population < 10000 { 
    message = "with a population of \(population), \(townname) is a small town" 
} else if population >= 10000 && population < 15000 { 
    message = "with a population of \(population), \(townname) is a medium sized town!" 
} else { 
    message = "\(townname) is a huge town!" 
} 

print (message) 
population = 250000 
print (population) 
print(message) 

は今、あなたの質問why it is not updating for second messageとして、あなたは関数を記述する必要があり、その後、あなたの変数への変更を行います。コードは単一のフローで実行されるため、最後の行で開始され、終了します。あなたのif-elseの条件は、variable(message)に変更がある場合はそれを知りません。だから、あなたがmessage変数を変更した後にif-else条件をもう一度書き込まなければならないか、functionを作成して、messageが変更された後にその関数を呼び出すか、意味がある場合は、コードの下を見てください。異なる方法で関数を書くようにしてください。

var townname = "Azadinos" 
var population: Int = 5422 
var message = "" 
var Haspostoffice: Bool = true 

func printMyVars() { 
    if population < 10000 { 
     message = "with a population of \(population), \(townname) is a small town" 
    } else if population >= 10000 && population < 15000 { 
     message = "with a population of \(population), \(townname) is a medium sized town!" 
    } else { 
     message = "\(townname) is a huge town!" 
    } 
    print(message) 
} 
printMyVars() 
population = 250000 
printMyVars() 

この版画:

with a population of 5422, Azadinos is a small town 
Azadinos is a huge town! 
+0

ありがとうございました!それは非常に高く評価され、良いレッスンです。 –

+0

問題を解決するのに役立つと答えてください。 – Santosh

0

変更後の人口統計を実行する必要があります。

//: Playground - noun: a place where people can play 

import Cocoa 

var str = "Hello, playground" 

var townname = "Azadinos" 
var population: Int = 5422 
var message:  String 
var Haspostoffice: Bool = true 

if population < 10000 { 
    message = "with a population of \(population), \(townname) is a small town" 
} else if population >= 10000 && population < 15000 { 
    message = "with a population of \(population), \(townname) is a medium sized town!" 
} else { 
    message = "\(townname) is a huge town!" 
} 
print (message) 
population = 250000 
print (population) 
// Do If statement after changing population 
if population < 10000 { 
    message = "with a population of \(population), \(townname) is a small town" 
} else if population >= 10000 && population < 15000 { 
    message = "with a population of \(population), \(townname) is a medium sized town!" 
} else { 
    message = "\(townname) is a huge town!" 
} 
print(message) 

コードを単純化するための機能を作成することをおすすめします。次のコードのように(私はこのコードを実行しませんでした)。あなたは

String interpolationスウィフトDocumentationString Interpolationを経る必要があります

//: Playground - noun: a place where people can play 

import Cocoa 

var str = "Hello, playground" 

var townname = "Azadinos" 
var population: Int = 5422 
var message:  String 
var Haspostoffice: Bool = true 

func makeMessage(population: Int) -> String { 
    if population < 10000 { 
     message = "with a population of \(population), \(townname) is a small town" 
    } else if population >= 10000 && population < 15000 { 
     message = "with a population of \(population), \(townname) is a medium sized town!" 
    } else { 
     message = "\(townname) is a huge town!" 
    } 
    return message 
} 

message = makeMessage(population) 
print (message) 
population = 250000 
print (population) 
message = makeMessage(population) 
print(message) 
関連する問題