Java 中 10,000 以下的 3、5 或 7 倍数的总和

编程入门 行业动态 更新时间:2024-10-27 08:35:57
本文介绍了Java 中 10,000 以下的 3、5 或 7 倍数的总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我知道如何让程序将 3、5 和 7 的倍数之和相加,但我不确定如何让程序只使用每个数字一次.例如,我可以让程序找出所有数字并将它们相加为 3,然后对 5 执行相同的操作,但是数字 15 将在最终数字中出现两次.我不确定我是如何让它只取一次号码的.感谢您的帮助.

I know how to get the program to add up the sums of the multiple for each of 3, 5 and 7, but I'm not sure how I'd get the program to only use each number once. For example, I can get the program to find out all of the numbers and add them up for 3 and then do the same for 5, but then the number 15 would be in the final number twice. I'm not sure exactly how I'd get it to only take the number once. Thanks for any help.

推荐答案

最简单的方法是使用 for 循环,因此:

The easiest approach would be to use a for loop thus:

int sum = 0; for(int i=1; i<10000; i++) { if (i % 3 == 0 || i % 5 == 0 || i % 7 == 0) sum += i; }

更多推荐

Java 中 10,000 以下的 3、5 或 7 倍数的总和

本文发布于:2023-11-29 19:24:02,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1647395.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:倍数   总和   Java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!