gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 10:18:53 28 4
gpt4 key购买 nike

我想学习如何计算并显示他在 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
}

最佳答案

试试这个(粗体答案)记住在最后写“count = 0”,否则在用户按“y”重新开始后它会继续递增。

//  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
}

关于loops - 如何计算并显示结果,循环执行的次数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28455051/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com