【问题标题】:Function error: "Binary operator '+' cannot be applied to operands of type 'Double' and 'Int'" [duplicate]函数错误:“二元运算符'+'不能应用于'Double'和'Int'类型的操作数”[重复]
【发布时间】:2018-04-12 20:12:46
【问题描述】:

我目前正在为我的编程课完成一项学校作业。在这个作业中,我们创建了一个函数,它接受一个数字等级(一个 Double)并将它除以 numberOfHours 以获得一个 GPA 变量。我在我的代码中遇到了一个错误:

二元运算符“+”不能应用于“Double”和“Int”类型的操作数

我不明白这个错误,我正在尝试通过更改什么是 Int 和什么是 Double 来修复代码,但它似乎不起作用。

错误来自的代码是:

// create your function here
func gpaUpdater(moreHours hours: Double, moreGPA grade: Double) -> Int {
    let addedHours: Int = hours + attemptedHours
    let calculatedGPA: Double = hours / grade
    //gpa = gpa + grade
    print(calculatedGPA)
    return calculatedGPA
}

完整代码如下:

//  My Functions Playground.swift
//
//
//: Playground - noun: a place where people can play

import UIKit

/*
 We're going to track your GPA from one semester to the next. Assume at the end of your sophomore years, you have attempted 60 hours and have earned 222.5 grade points. Assign attempted hours and grade points to variables. Write a function that updates your current GPA and assigns it to the GPA var (you'll update it along the way). Label your function arguments. Print your new GPA from within the function.

 At the end of the current semester, add 16 hours and 60 grade points to your record. Call the gpa function to update your overall gpa.

 Test your gpa at the end of the year to see if any administrative action needs to be taken. If the gpa is less than 1.8, the student will need to be placed on suspension. If less than 2.0, we need to put the student on probation. If over 3.5, we'll put the student on the dean's list, and if over 3.9, we'll put the student on the president's list. Create a switch that prints the recommended adminstrative action. If no action is required, print, "Carry on. Nothing to see here." Create internal and external labels for your arguments.

 */

var gpa: Double
var attemptedHours: Int
var earnedGradePoints: Int

// create your function here
func gpaUpdater(moreHours hours: Double, moreGPA grade: Double) -> Int {
    let addedHours: Int = hours + attemptedHours
    let calculatedGPA: Double = hours / grade
    //gpa = gpa + grade
    print(calculatedGPA)
    return calculatedGPA
}

// call the function
gpaUpdater(moreHours: 16, moreGPA: 60)

// add the new hours and grade points here and call the function again
func switchFunction() {
    switch gpa {
    case gpa..<1.8:
        print("You will be placed on suspension")
    case 1.8...2.0:
        print("You will be placed on probation")
    case 3.5...3.8:
        print("You will be put on the dean's list.")
    case 3.9..<gpa:
        print("You will be put on the president's list.")
    default:
        print("Carry on. Nothing to see here.")
    }
}
switchFunction()

【问题讨论】:

    标签: swift function


    【解决方案1】:

    改变这一行

    let addedHours: Int = hours + attemptedHours
    

    let addedHours =  Int(hours) + attemptedHours
    

    【讨论】:

    • 更好,let addedHours = Int(hours) + attemptedHours。更好的是保留 Double 结果以避免精度损失。
    • 是的......我混淆了 Double 是什么,我同意,但这首先会处理错误,然后他应该知道最适合他的情况
    • 我认为你错过了我的主要观点。你不需要: Int
    • 感谢您迄今为止的帮助。我现在收到一个返回错误,尽管与声明的函数有关:“28:12:无法将'Double'类型的返回表达式转换为'Int'类型。”将 Int(x) 放在 return 语句前面并不能解决这个问题。
    • 好的,我修好了。 gpaUpdater 函数中没有任何值,但至少我没有错误。这是另一个帖子的另一个问题。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-12
    • 1970-01-01
    • 1970-01-01
    • 2019-08-23
    • 1970-01-01
    相关资源
    最近更新 更多