不能在swift中进行除法功能

时间:2017-05-15 07:57:38

标签: swift average

我正在使用swift教程进行平均功能分配。 当我写这样的代码时:

func AVG(numbers: Int...) -> Double {
var i=0, sum=0; var avg: Double
for number in numbers {
    i+=1
    sum+=number
    avg = sum/i
}
return avg
}

let Average = AVG(numbers: 3,4,5)
print (Average)

它显示一条错误消息,显示二元运算符" /"不能应用于两个int操作数。

当我执行像

这样的简单划分时
let x = 5
let y = 2
let quo = x/y

它有效。

我在功能上做错了什么?

2 个答案:

答案 0 :(得分:1)

您将mMessageAdapter = new MessagesAdapter(currentConversationKey, this, this); conversationRecyclerView.setLayoutManager(messageLinearLayout); conversationRecyclerView.setItemAnimator(new DefaultItemAnimator()); conversationRecyclerView.setAdapter(mMessageAdapter); mConversationRecyclerDataObserver = new RecyclerView.AdapterDataObserver() { @Override public void onChanged() { super.onChanged(); Log.d(TAG, "Conversation Adapter is changed"); checkIfMessagesInConversation(); } @Override public void onItemRangeChanged(int positionStart, int itemCount) { super.onItemRangeChanged(positionStart, itemCount); Log.d(TAG, "Conversation Adapter item range changed"); checkIfMessagesInConversation(); } @Override public void onItemRangeInserted(int positionStart, int itemCount) { super.onItemRangeInserted(positionStart, itemCount); Log.d(TAG, "Conversation Adapter item range inserted"); checkIfMessagesInConversation(); int messageCount = mMessageAdapter.getItemCount(); Log.d(TAG, "message Count is : "+ messageCount); int lastVisiblePosition = messageLinearLayout.findLastCompletelyVisibleItemPosition(); Log.d(TAG, "Last Visible Position is : "+ lastVisiblePosition); Log.d(TAG, "positionStart is : "+ positionStart); Log.d(TAG, "Insert Item count : "+ itemCount); if (lastVisiblePosition == -1 || (positionStart >= (messageCount - 1) && lastVisiblePosition == (positionStart - 1))) { Log.d(TAG, "Scrolling to pos "+ (messageCount - 1)); try { conversationRecyclerView.scrollToPosition(mMessageAdapter.getItemCount() - 1); } catch (Exception e){ Log.d(TAG, e.toString()); } } if(isRefreshing){ onItemsLoadComplete(); } } @Override public void onItemRangeRemoved(int positionStart, int itemCount) { super.onItemRangeRemoved(positionStart, itemCount); Log.d(TAG, "Conversation adapter item range removed"); checkIfMessagesInConversation(); } @Override public void onItemRangeMoved(int fromPosition, int toPosition, int itemCount) { super.onItemRangeMoved(fromPosition, toPosition, itemCount); Log.d(TAG, "Conversation Adapter item range moved"); checkIfMessagesInConversation(); } }; mMessageAdapter.registerAdapterDataObserver(mConversationRecyclerDataObserver); 声明为Double类型。

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click Dim fName As String = "" OpenFileDialog1.InitialDirectory = "c:\desktop" OpenFileDialog1.Filter = "CSV files (*.csv)|*.CSV" OpenFileDialog1.FilterIndex = 2 OpenFileDialog1.RestoreDirectory = True If (OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK) Then fName = OpenFileDialog1.FileName End If txtpathfile.Text = fName Dim TextLine As String = "" Dim SplitLine() As String If System.IO.File.Exists(fName) = True Then Dim objReader As New System.IO.StreamReader(fName) Do While objReader.Peek() <> -1 TextLine = objReader.ReadLine() SplitLine = Split(TextLine, ",") Me.DataGridView2.Rows.Add(SplitLine) Loop Else MsgBox("File Does Not Exist") End If End Sub 类型为avgnumbers也为Int

当您执行i=0 Int时,您尝试将其分配给sum/i。 Swift没有自动进行转换 - 它会强制您手动转换数据类型。 您可以通过简单地将这两个变量转换为Double来修复它。

请记住,在进行除法之前需要转换Int - 否则结果将会删除提醒,因为Int无法拥有它们。致@martin的信用

另一个问题是,如果您的数字数组为空,您可能会尝试返回未初始化的变量Int/Int

以下工作代码:

Double

答案 1 :(得分:0)

AVG功能有两个问题 - 顺便说一句。函数名称应该以小写字母开头。

游乐场显示明确的错误消息

  1.   

    初始化之前使用的变量'avg'

    解决方案:

    var avg = 0.0
    
  2.   

    无法将“Int”类型的值指定为“Double”类型

    解决方案:

    avg = Double(sum) / Double(i)
    
  3. PS:使用键值编码运算符@avg

    的替代方法
    func avg(numbers: Int...) -> Double {
        return (numbers as NSArray).value(forKeyPath: "@avg.self") as! Double
    }