Java Switch嵌套for循环将重复

时间:2017-09-26 00:36:15

标签: java loops for-loop system.in

我是Java的新手,我遇到了这个特殊的for循环问题。每次我运行循环并输入一个不是1或2的数字时,循环显示:“请选择正确的选项”。可能是什么原因造成的?

print("you win")

1 个答案:

答案 0 :(得分:-1)

选项变量未获得您希望它保持的正确值。 通过查看您的代码,用户可以输入1或2,然后您尝试将其转换为问题所在的字符。尝试将int作为输入     import UIKit import os.log class Season: NSObject, NSCoding { //MARK: Properties var name: String var detail: String //MARK: Archiving Paths static let DocumentsDirectory = FileManager().urls(for: .documentDirectory, in: .userDomainMask).first! static let ArchiveURL = DocumentsDirectory.appendingPathComponent("season") //MARK: Types struct PropertyKey { static let name = "name" static let detail = "detail" } //MARK: Initialization init?(name: String, detail: String) { guard !name.isEmpty else { return nil } guard !detail.isEmpty else { return nil } // Initialize stored properties self.name = name self.detail = detail } //MARK: NSCoding func encode(with aCoder: NSCoder) { aCoder.encode(name, forKey: PropertyKey.name) aCoder.encode(detail, forKey: PropertyKey.detail) } required convenience init?(coder aDecoder: NSCoder) { // the name is required. If we cannnot get a name string, the initializer should fail. guard let name = aDecoder.decodeObject(forKey: PropertyKey.name) as? String else { os_log("Unable to decode the name for a Season object.", log: OSLog.default, type: .debug) return nil } let detail = aDecoder.decodeObject(forKey: PropertyKey.detail) self.init(name: name, detail: detail as! String) } }