如何计算和显示结果,执行循环的次数?

时间:2015-02-11 13:01:35

标签: loops swift

我想学习如何计算和显示他在循环Swift中工作多少次的结果。提前谢谢!

    import Foundation

var result: Int
var i: Int = 1

for i = 1; i <= 20; i++  {
    println("The result \(i)")
}

我希望在此代码之后执行此操作,结果是派生出来的,例如,用户猜到了这个数字的次数以及丢失了多少次,所以我不知道该怎么做。

import Foundation

var randomNumber = 1
var userGuess = 1
var continueGuessing = true
var keepPlaying = true
var input = ""

while (keepPlaying) {
    randomNumber = Int(arc4random_uniform(101)) //get a random number between 0-100
    println("The random number to guess is: \(randomNumber)"  );
    while (continueGuessing) {
        println ("Pick a number between 0 and 100. ")
        input = NSString(data: NSFileHandle.fileHandleWithStandardInput().availableData, encoding:NSUTF8StringEncoding)! //get keyboard input
        input = input.stringByReplacingOccurrencesOfString("\n", withString: "", options: NSStringCompareOptions.LiteralSearch, range: nil) //strip off the /n
        userGuess = input.toInt()!
        if (userGuess == randomNumber) {
            continueGuessing = false
            println("Correct number!");
        }
            //nested if statement
        else if (userGuess > randomNumber){
            //user guessed too high
            println("Your guess is too high");
        }
        else{
            // no reason to check if userGuess < randomNumber. It has to be.
            println("Your guess is too low");
        }
    }
    println ("Play Again? Y or N");
    input = NSString(data: NSFileHandle.fileHandleWithStandardInput().availableData, encoding:NSUTF8StringEncoding)!
    input = input.stringByReplacingOccurrencesOfString("\n", withString: "", options: NSStringCompareOptions.LiteralSearch, range: nil)

    if (input == "N" || input == "n"){
        keepPlaying = false
    }
    continueGuessing = true
}

2 个答案:

答案 0 :(得分:1)

试试这个(BOLD中的答案)记得写下&#39; count = 0 &#39;最后,否则它会在用户按下&#39;之后继续递增。重新开始。

//  Marty Tripp - Updated to count Guesses
//  main.swift
//  Guess


import Foundation

var randomNumber = 1
var userGuess:Int? = 1
var continueGuessing = true
var keepPlaying = true
var input = ""
**var count = 0**

while (keepPlaying) {
randomNumber = Int(arc4random_uniform(101))  //get a random number      between 0-100
//print("The random number to guess is: \(randomNumber)"  );
while  (continueGuessing) {
    print ("Pick a number between 0 and 100. ")
    input = NSString(data:     NSFileHandle.fileHandleWithStandardInput().availableData, encoding: NSUTF8StringEncoding)! as String //get keyboard input
    input = input.stringByReplacingOccurrencesOfString("\n", withString: "", options: NSStringCompareOptions.LiteralSearch, range: nil)  //strip off the /n
        userGuess = Int(input)
        **count += 1**
    if (userGuess == randomNumber) {
        continueGuessing = false
        print("Correct Number!");
        print("Your number of tries were:", count)
    }


    //nested if statement
    else if (userGuess > randomNumber) {
        //user guessed too high
        print("Sorry cutie pie, your guess is too high");
    }
    else{
        //no reason to check if userGuess < randomNumber.  It has to be.
        print("Sorry cutie pie, your guess is too low");
    }
}
print("Play Again? Y or N");
input = NSString(data: NSFileHandle.fileHandleWithStandardInput().availableData,     encoding:NSUTF8StringEncoding)! as String
input = input.stringByReplacingOccurrencesOfString("\n", withString:     "", options: NSStringCompareOptions.LiteralSearch, range: nil)
**count = 0**
if (input == "N" || input == "n"){
    keepPlaying = false
}
continueGuessing = true
}

答案 1 :(得分:0)

while (continueGuessing)循环之外初始化变量。

在循环内部,增加该变量。

这将计算该人有多少猜测。