gpt4 book ai didi

arrays - 如何打印变量的名称而不是 Swift 中的值?

转载 作者:行者123 更新时间:2023-11-28 13:29:57 27 4
gpt4 key购买 nike

我希望代码打印变量的名称而不是值。它只是不想工作。

我没有尝试太多,因为我不知道该怎么做。我在网上也找不到太多关于此的文档。

import UIKit

var a = 1
var b = 2
var c = 3
var d = 4
var e = 1
var f = 2
var g = 3
var h = 4
var i = 1
var j = 2
var k = 3
var l = 4

var suggestedGroup1 = 16

var classStudents = [a,b,c,d,e,f,g,h,i,j,k,l]
var groupsNumber = 3
var peoplePerGroup = classStudents.count / groupsNumber

var totalClassPoints = a + b + c + d + e + f + g + h + i + j + k + l

var OnePerson = classStudents.randomElement()
var TwoPerson = classStudents.randomElement()
var ThreePerson = classStudents.randomElement()
var FourPerson = classStudents.randomElement()

suggestedGroup1 = OnePerson! + TwoPerson! + ThreePerson! + FourPerson!

while suggestedGroup1 != 10 {
OnePerson = classStudents.randomElement()
TwoPerson = classStudents.randomElement()
ThreePerson = classStudents.randomElement()
FourPerson = classStudents.randomElement()
suggestedGroup1 = OnePerson! + TwoPerson! + ThreePerson! + FourPerson!

}

if suggestedGroup1 == 10 {
print ("Group 1 is made up of", OnePerson!, ",", TwoPerson!, ",", ThreePerson!, "and", FourPerson!)
}

当它打印时(“第 1 组由”、OnePerson!、“、”、TwoPerson!、“、”、ThreePerson!、“和”、FourPerson!),我想要的是人名而不是他们的“值(value)”。

最佳答案

你需要一个单独的方法,我创建了一个结构并给它命名和值。这是不言自明的,如果您不明白任何部分,请告诉我。

import UIKit

struct StudentClass {
let name: String
let value: Int
}

var a = StudentClass(name: "a", value: 1)
var b = StudentClass(name: "b", value: 2)
var c = StudentClass(name: "c", value: 3)
var d = StudentClass(name: "d", value: 4)
var e = StudentClass(name: "e", value: 1)
var f = StudentClass(name: "f", value: 2)
var g = StudentClass(name: "g", value: 3)
var h = StudentClass(name: "h", value: 4)
var i = StudentClass(name: "i", value: 1)
var j = StudentClass(name: "j", value: 2)
var k = StudentClass(name: "k", value: 3)
var l = StudentClass(name: "l", value: 4)

var suggestedGroup1 = 16

var classStudents = [a,b,c,d,e,f,g,h,i,j,k,l]
var groupsNumber = 3
var peoplePerGroup = classStudents.count / groupsNumber

var totalClassPoints = a.value + b.value + c.value + d.value + e.value + f.value + g.value + h.value + i.value + j.value + k.value + l.value

var OnePerson = classStudents.randomElement()
var TwoPerson = classStudents.randomElement()
var ThreePerson = classStudents.randomElement()
var FourPerson = classStudents.randomElement()

suggestedGroup1 = OnePerson!.value + TwoPerson!.value + ThreePerson!.value + FourPerson!.value

while suggestedGroup1 != 10 {
OnePerson = classStudents.randomElement()
TwoPerson = classStudents.randomElement()
ThreePerson = classStudents.randomElement()
FourPerson = classStudents.randomElement()
suggestedGroup1 = OnePerson!.value + TwoPerson!.value + ThreePerson!.value + FourPerson!.value

}

if suggestedGroup1 == 10 {
print ("Group 1 is made up of", OnePerson!.name, ",", TwoPerson!.name, ",", ThreePerson!.name, "and", FourPerson!.name)
}

这是输出:-

Group 1 is made up of h , k , f and e

关于arrays - 如何打印变量的名称而不是 Swift 中的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57543341/

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