I want the code to print the name of the variable and not the value. It just does not want to work.
I have not tried much as I have no idea how to do this. I also cannot find much documentation on this online.
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!)
}
When it prints ("Group 1 is made up of", OnePerson!, ",", TwoPerson!, ",", ThreePerson!, "and", FourPerson!), I want the name of the person and not their "value".
You need a separate approach for you, I created a struct and give it is name and values. It is self-explanatory, if you don't understand any part let me know.
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)
}
Here is the output: -
Group 1 is made up of h , k , f and e