在Swift数组中找到元素的总和(Finding sum of elements in Swift array)

在swift中找到整数数组的最简单(最佳)方法是什么? 我有一个数组称为倍数,我想知道这个倍数的总和。 先谢谢你!


What is the easiest (best) way to find the sum of an array of integers in swift? I have an array called multiples and I would like to know the sum of the multiples.


原文:https://stackoverflow.com/questions/24795130
2023-05-22 10:05

满意答案

是:

$boolean = !$boolean;

如果它不是一个布尔值,你可以使用三元结构:

$int = ($some_condition ? 1 : 2); // if $some_condition is true, set 1
                                  // otherwise set 2

Yes:

$boolean = !$boolean;

if it's not a boolean value, you can use the ternary construction:

$int = ($some_condition ? 1 : 2); // if $some_condition is true, set 1
                                  // otherwise set 2

相关问答

更多

将布尔值传入指令(Passing Boolean Value Into Directive)

你做错了什么 readOnly:'@' 这意味着readOnly将是一个字符串,使其成为一个js变量尝试 readOnly:'=' 然后 <div single-view-card read-only="{{false}}"/> 应该 <div single-view-card read-only="true"/> 你需要显示更多的代码,这可能是错误的一部分,但我认为还有更多 希望能帮助到你 what you are doing wrong is readOnly:'@' this me...

从一个开关里走出来,但没有布尔值,这有可能吗?(Going out from a switch inside a do while, but without a boolean, it's possible? Java)

如果用户选择0,则只需要继续。 Scanner kb = new Scanner(System.in); // c = continue char c = 'a'; // attempt to limit the input to 3 times int attempt = 3; // option = op int op = 0; do { do{ System.out.println("Choose continue[0] or go out[1]."); ...

布尔值开关/反相(Boolean value switch/invert)

是: $boolean = !$boolean; 如果它不是一个布尔值,你可以使用三元结构: $int = ($some_condition ? 1 : 2); // if $some_condition is true, set 1 // otherwise set 2 Yes: $boolean = !$boolean; if it's not a boolean value, you can use the terna...

Android,Use switch case用于确定布尔值(Android,Use switch case is used to determine the boolean)

你想学习开关。 您可以将枚举用于您的目的, public enum BtnWifi { // you can create new enum java file for this CONNECTED, NOT_CONNECTED } //.... Setting value for enum in your code BtnWifi btnWifi; if(isConnected) { //if connection established btnWifi = BtnWifi.CONNE...

将布尔值分配给整数(Assigning boolean values to integer)

你想要这样的东西: class SwitchBoard(object): def __init__(self, num_switches): # create a dict of {number: state} key-values self.switches = {i: False for i in range(num_switches)} def flip(self, num): # test for the number be...

使用布尔值的If语句的语法(Syntax for an If statement using a boolean)

您可以随意更改bool的值。 至于if: if randombool == True: 有效,但您也可以使用: if randombool: 如果你想测试某些东西是否是假的,你可以使用: if randombool == False 但你也可以使用: if not randombool: You can change the value of a bool all you want. As for an if: if randombool == True: works, but you ...

Angular 4 - 从数据库中绑定布尔值来切换开关(Angular 4 - Bind boolean values from db to toggle switch on/off)

<ng-container *ngFor="let data of displayData$ | async;let i = index"> <tr class= "row-break"> <form> <p [style.color]="data.Status == true ? 'green' : 'black'"> <cm-toggle-switch [(ngModel)]="data.Status" name="switch1" ></cm-toggle-...

开关条件有布尔值?(switch condition has boolean value?)

警告只是告诉你,你正在使用带有布尔值的switch语句。 对于真正的布尔值,你应该只使用if - else语句,而不是switch 。 请注意,如果您使用true布尔类型,则只会出现此警告。 如果您使用BOOL ,那么这是Mac OS或非64位iOS上的unsigned char 。 因此你不会为这些目标得到这个警告。 但是如果您使用bool (或者如果您在64位iOS目标上使用BOOL ),则会收到此警告。 您可以用if - else语句替换switch语句来禁止此警告。 The warning...

无法使用TMemoryStream读取布尔值(Cannot read boolean value with TMemoryStream)

Boolean值的有效值为0和1.如果将不同的值放入Boolean变量中,则未来的行为是未定义的。 您应该读入一个字节变量b并将b <> 0赋值给Boolean 。 或者通过将字节转换为ByteBool消毒。 或者您可以选择验证从文件读取的值并拒绝除0和1之外的任何值。 Valid values for a Boolean are 0 and 1. If you put a different value into a Boolean variable then future behaviour ...

使用PebbleKit向Pebble发送布尔值(Sending boolean to Pebble with PebbleKit)

在Android方面看起来不错。 我认为这更像是一个AppSync问题。 以下是手表应用程序中要检查的一些内容: 确保在手表上创建具有初始值的元组列表。 此列表需要包含您的密钥GPS_HAS_FIX_KEY ; Tuplet initial_values[] = { TupletInteger(GPS_HAS_FIX_KEY, (uint8_t) 0), /* Other tuplets that you will synchronize */ }; 确保将这些app_sync_in...

相关文章

更多

千锋首发Swift视频教程

千锋Swift视频教程-7.Swift结构体.mp4 千锋Swift视频教程-13.代理反向传值.mp ...

Swift入门视频教程-尚学堂视频教程

最新Swift语言语法介绍,包括Swift流程控制语句、Swift各种构造函数、closure、泛型、 ...

报错说找不到abbrev这个方法,但Array有这个方法的吧?

以下是ruby-doc.org http://www.ruby-doc.org/core/class ...

scala数组操作

定长数组 最简单的数组创建如下,记住方括号在Scala中用做泛型,相当于<>在Java中作用。 定义1 ...

Java 数组

Java 数组 数组对于每一门编辑应语言来说都是重要的数据结构之一,当然不同语言对数组的实现及处 ...

怎么把2个数组里面相同的元素组合成一个新的数组

怎么把2个数组里面相同的元素组合成一个新的数组

动态拼接JSON数组的问题

比如说var level = [[&quot;ID&quot;,&quot;NAME&quot;]]; ...

Guava处理java byte类型工具类-Bytes类

static int indexOf(byte[] array

学习di'z地址

杨丰盛cocos2d-x教程下载地址:http://www.dwz.cn/gwPgd 无限互联新浪微博 ...

求一个group by后面字段顺序影响结果的例子

select a,b ,sum(e) from test group by a,b order by ...

最新问答

更多

获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)

我用Google搜索了一个解决方案。 “EnumDisplayModeProvider”是我自己设置网站的各种模式的枚举。 public EnumDisplayModeProvider GetDisplayModeId() { foreach (var mode in DisplayModeProvider.Instance.Modes) if (mode.CanHandleContext(HttpContext)) {

如何通过引用返回对象?(How is returning an object by reference possible?)

这相对简单:在类的构造函数中,您可以分配内存,例如使用new 。 如果你制作一个对象的副本,你不是每次都分配新的内存,而是只复制指向原始内存块的指针,同时递增一个也存储在内存中的引用计数器,使得每个副本都是对象可以访问它。 如果引用计数降至零,则销毁对象将减少引用计数并仅释放分配的内存。 您只需要一个自定义复制构造函数和赋值运算符。 这基本上是共享指针的工作方式。 This is relatively easy: In the class' constructor, you allocate m

矩阵如何存储在内存中?(How are matrices stored in memory?)

正如它在“熵编码”中所说的那样,使用Z字形图案,与RLE一起使用,在许多情况下,RLE已经减小了尺寸。 但是,据我所知,DCT本身并没有给出稀疏矩阵。 但它通常会增强矩阵的熵。 这是compressen变得有损的点:输入矩阵用DCT传输,然后量化量化然后使用霍夫曼编码。 As it says in "Entropy coding" a zig-zag pattern is used, together with RLE which will already reduce size for man

每个请求的Java新会话?(Java New Session For Each Request?)

你是如何进行重定向的? 您是否事先调用了HttpServletResponse.encodeRedirectURL()? 在这里阅读javadoc 您可以使用它像response.sendRedirect(response.encodeRedirectURL(path)); The issue was with the path in the JSESSIONID cookie. I still can't figure out why it was being set to the tomca

css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)

我认为word-break ,如果你想在一个单词中打破行,你可以指定它,这样做可以解决问题: .column { word-break:break-all; } jsFiddle演示。 您可以在此处阅读有关word-break属性的更多信息。 I think word-break, with which you can specify if you want to break line within a word, will do the trick: .column { word-break

无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)

我认为您忘记在分类时间内缩放输入图像,如train_test.prototxt文件的第11行所示。 您可能应该在C ++代码中的某个位置乘以该因子,或者使用Caffe图层来缩放输入(请查看ELTWISE或POWER图层)。 编辑: 在评论中进行了一次对话之后,结果发现在classification.cpp文件中错误地删除了图像均值,而在原始训练/测试管道中没有减去图像均值。 I think you have forgotten to scale the input image during cl

xcode语法颜色编码解释?(xcode syntax color coding explained?)

转到: Xcode => Preferences => Fonts & Colors 您将看到每个语法高亮颜色旁边都有一个简短的解释。 Go to: Xcode => Preferences => Fonts & Colors You'll see that each syntax highlighting colour has a brief explanation next to it.

在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)

你考虑过第三方拼写检查吗? 您可以将在C#中开发的自定义WinForms控件插入访问数据库吗? VB6控件怎么样? 如果你能找到一个使用第三方库进行拼写检查的控件,那可能会有效。 Have you considered a third party spell checker? Can you insert a custom WinForms controls developed in C# into an access database? What about a VB6 control? If

从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)

我有同样的问题,因为我在远程服务器上有两个图像,我需要在每天的预定义时间复制到我的本地服务器,这是我能够提出的代码... try { if(@copy('url/to/source/image.ext', 'local/absolute/path/on/server/' . date("d-m-Y") . ".gif")) { } else { $errors = error_get_last(); throw new Exception($err

从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))

我不确定我完全明白你在说什么。 你能编辑你的帖子并包含你正在做的Subversion命令/操作的特定顺序吗? 最好使用命令行svn客户端,以便容易为其他人重现问题。 如果您只是想获取文件的旧副本(即使该文件不再存在),您可以使用如下命令: svn copy ${repo}/trunk/moduleA/file1@${rev} ${repo}/trunk/moduleB/file1 其中${repo}是您的存储库的URL, ${rev}是您想要的文件的版本。 这将恢复该文件的旧版本,包括最高版本