我正在开发一个测验应用程序,我有一系列问题和答案。答案选择与问题相关联,具有与其对应问题相同的索引(例如,答案数组中的第一个答案选项与问题数组的第一个索引一起使用,依此类推)。但是,我随机选择了问题数组中提出的问题,但相应的答案没有出现在显示的正确问题上。通过随机选择和显示的问题,出现正确答案选择的最佳方式是什么?另外,在测验失败并重置之前,如何防止已经回答的问题不再被随机选择?谢谢。
这是我的代码:
import UIKit
class ViewController:UIViewController {
//random question generation function
func randomQuestion() {
index = Int(arc4random_uniform(UInt32(questions.count)))
questionLabel.text = questions[index]
}
//costants
let questions = ["Who is Thor's half brother?", "What is the name of Thor's hammer?"]
var answers = [["Atum", "Loki", "Red Norvell", "Kevin Masterson"], ["Mjolinr", "Uru", "Stormbreaker", "Odin's Staff"]]
//variables
var currentQuestion = 0
var rightAnswerBox:UInt32 = 0
var index = 0
//Question Label
@IBOutlet weak var questionLabel: UILabel!
//Answer Button
@IBAction func buttonAction(_ sender: AnyObject) {
if (sender.tag == Int(rightAnswerBox)) {
print ("Correct!")
} else {
wrongSeg()
print ("Wrong!")
}
if (currentQuestion != questions.count)
{
newQuestion()
}
}
override func viewDidAppear(_ animated: Bool)
{
newQuestion()
}
//function that displays new question
func newQuestion()
{
//countdown timer section
randomQuestion()
rightAnswerBox = arc4random_uniform(4)+1
//create a button
var button:UIButton = UIButton()
var x = 1
for index in 1...4
{
//creat a button
button = view.viewWithTag(index) as! UIButton
if (index == Int(rightAnswerBox))
{
button.setTitle(answers[currentQuestion][0], for: .normal)
} else {
button.setTitle(answers[currentQuestion][x], for: .normal)
x += 1
}
}
currentQuestion += 1
randomImage()
}
答案 0 :(得分:1)
我建议使用更合适的数据结构。
类似的东西:
class Question {
var id: Int
var questionText: String
var answers: [String]
var answered = false
}
然后随机化问题数组。您可以跟踪您所在的索引并在数组中向前移动直到它结束。
在循环结束时,检查哪些问题未得到解答,删除已回答的问题(或创建新阵列)并重复此过程。
您可以枚举数组
var questions = [Question]()
//populate the array
for (index, question) in questions.enumerated() {
//This returns the question as well as the current index
}
答案 1 :(得分:1)
<强> 1。制作结构
这样可以打包一个问题和一系列答案,以后稍后进行随机播放。
Data
<强> 2。现在使用Fisher-Yates Shuffle
随机化一系列问题因为O(n)时间而冷却。
public NotaFiscalServiceSoap getNotaFiscalServiceSoap() throws IOException, GeneralSecurityException {
if(notaFiscalServiceSoap==null){
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean() ;
factory.setWsdlURL(municipio.getUrlWsdl().toString());
factory.setServiceClass(NotaFiscalServiceSoap.class);
factory.setServiceName(Q_NAME);
factory.setConduitSelector(getConduitSelector());
notaFiscalServiceSoap = factory.create(NotaFiscalServiceSoap.class);
}
return notaFiscalServiceSoap;
}
private ConduitSelector getConduitSelector() throws IOException, GeneralSecurityException {
ServiceInfo serviceInfo = new ServiceInfo();
serviceInfo.setTargetNamespace(NAMESPACE);
EndpointInfo endpointInfo = new EndpointInfo();
endpointInfo.setService(serviceInfo);
endpointInfo.setName(Q_NAME);
endpointInfo.setAddress(municipio.getUrlWsdl().toString());
URLConnectionHTTPConduit conduit = new URLConnectionHTTPConduit(null, endpointInfo);
conduit.setTlsClientParameters(getTLSClientParameters());
ConduitSelector selector = new UpfrontConduitSelector(conduit);
return selector;
}
private TLSClientParameters getTLSClientParameters() throws GeneralSecurityException, IOException{
KeyStoreType trustKeyStore = new KeyStoreType();
trustKeyStore.setFile(pathCertWsdl);
trustKeyStore.setPassword(passCertWsdl);
trustKeyStore.setType("jks");
TrustManagersType trustManagerType = new TrustManagersType();
trustManagerType.setKeyStore(trustKeyStore);
KeyStoreType keyStoreType = new KeyStoreType();
keyStoreType.setFile(pathCertA1);
keyStoreType.setPassword(passCertA1);
keyStoreType.setType("pkcs12");
KeyManagersType keyManagerType = new KeyManagersType();
keyManagerType.setKeyStore(keyStoreType);
keyManagerType.setKeyPassword(passCertA1);
TLSClientParametersType clientParametersType = new TLSClientParametersType();
clientParametersType.setTrustManagers(trustManagerType);
clientParametersType.setKeyManagers(keyManagerType);
clientParametersType.setUseHttpsURLConnectionDefaultHostnameVerifier(true);
clientParametersType.setUseHttpsURLConnectionDefaultSslSocketFactory(true);
return TLSClientParametersConfig.createTLSClientParametersFromType(clientParametersType);
}