本文介绍了< =不是前缀一元运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的任务是创建一个iOS应用程序,该应用程序将允许用户输入以下信息:

My task is to create an iOS application that will allow the user to enter the following information:

•   First Name

•   Last Name

•   Age

然后,应用程序将在单独的框中显示此信息,如下所示:

The application will then display this information in a separate box as follows:

您好,姓氏是您的消息.

Hi firstName lastName, you are Message.

该消息将根据用户输入的年龄替换,如下所示:

The Message will be replaced based on the age entered by the user as follows:

如果年龄是< 12消息=>一个孩子"

If age is < 12 Message => "a child"

如果年龄为> = 13且< = 19:消息=>青少年"

If age is >= 13 and <= 19: Message => "a teenager"

如果年龄> 19,并且< = 29:消息=>一个年轻人"

If age is > 19 and <=29: Message => "a young man"

如果年龄> = 30且< = 49:消息=>一个中年男子"

If age is >= 30 and <=49: Message => "a middle aged man"

如果年龄> = 50并且< = 64:消息=>一个有经验的人"

If age is >= 50 and <=64: Message => "an experienced man"

如果年龄> = 65:消息=>老人"

If age is >= 65: Message => "a senior man"

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var input1: UITextField!
@IBOutlet weak var input2: UITextField!
@IBOutlet weak var input3: UITextField!
@IBOutlet weak var output4: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}



@IBAction func buttonPressed(sender: UIButton) {

    var one = input1.text.toInt()
    var two = input2.text.toInt()
    var three = input3.text.toInt()!
    var total : String!

    if sender.tag == 1{
        if three < 12{
            total = ("\(one) \(two) a child")
        } else if three >= 13 && <=19 {
            total = ("\(one) \(two) a teenager")
        } else if three > 19 && <=29{
            total = ("\(one) \(two) a young man")
        } else if three >= 30 && <=49{
            total = ("\(one) \(two) a middle aged man")
        } else if three >= 50 && <=64{
            total = ("\(one) \(two) an experienced man")
        } else if three >= 65{
            total = ("\(one) \(two) a senior man")
        }

    } else{
        total = ("You are correct")
    }

}

}

有人可以帮助我解决出现的错误吗?

Can anyone help me resolving the occurring error?

推荐答案

您的代码应为

} else if three >= 13 && three <= 19 {
    total = ("\(one) \(two) a teenager")
} else if three > 19 && three <= 29 {
    total = ("\(one) \(two) a young man")
} else if three >= 30 && three <= 49 {
    total = ("\(one) \(two) a middle aged man")
} else if three >= 50 && three <= 64 {
    total = ("\(one) \(two) an experienced man")
} else if three >= 65 {
    total = ("\(one) \(two) a senior man")
}

其原因是因为错误'<=' is not a prefix unary operator表示<=不是一元运算符.这是只需要一个参数的运算符. <=但是需要两个操作数,一个在操作数之前,一在操作后-因此它是一个二进制中缀运算符.

The reason for that is that as the error '<=' is not a prefix unary operator says <= is not a unary operator. Which is a operator which only needs one argument. <= however needs two operands, one before and one after - therefore it is a binary infix operator.

更好的方法可能是使用switch语句和范围,而不是正确建议@MartinR.他们遵循以下模式:

The better way would probably be using a switch statement and ranges instead as @MartinR correctly suggested. They follow the following pattern:

switch k {
    case Int.min...12:
        print("hi")
    case 13...45:
        print("bye")
    default:
        print("nah")
}

或者(再次如Martin所建议的那样)简化ifs.因为第一个if已经捕获了Int.Min和11之间的所有值,所以else-block不需要检查该值是否大于12,因为如果不是,则第一个将已经为true,而else甚至将没有已达到.

Alternatively (again as Martin suggested) simplify your ifs. Because the first if already captures all values between Int.Min and 11 your else-block does not need to check that the value is greater than 12 because if it wasn't the first would have already been true and the else would not even have been reached.

最后一点:

这篇关于&lt; =不是前缀一元运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 00:04