如何使用 Java 中的 for 循环显示所有以数字 3 结尾的数字?

huangapple go评论56阅读模式
英文:

How do I display all numbers that end with the number 3 with a for loop in java?

问题

我需要在Java中编写一个for循环,显示从13到93的所有以数字3结尾的数字。它必须包括13和93。

英文:

I need to write a for loop in java that displays all of the numbers ranging from 13 - 93 that end in the number 3. It must include 13 and 93.

答案1

得分: 2

13 循环到 93。每次递增 10。像这样,

for (int i = 13; i <= 93; i += 10) {
    // 打印数值
}
英文:

Loop from 13 to 93. Increment by 10. Like,

for (int i = 13; i &lt;= 93; i += 10) {
    // Print the value
}

答案2

得分: 1

如果你愿意,你可以编写一个函数来完成这个任务,但可以使用参数进行更灵活的控制。其中参数包括表示起始值和结束值的参数,以及类似于 endsWith 的值。这只是一个建议而已。

英文:

If you wanted, you could write a function that achieves this task but with parameters
that allow for more flexible control. Where it takes in arguments representing a
from value and a to value, and something like a endsWith value. It's just a suggestion though.

答案3

得分: 1

任何以十进制表示的数字模10,都将得到最后一位数字。

for (int i = start ; i <= end ; i++) {
    if (i % 10 == 3) {
        System.out.println(i);
    }
}
英文:

Any decimal based number mod 10 will get you the last digit.

	for (int i = start ; i &lt;= end ; i++) {
		if (i % 10 == 3) {
			System.out.println(i);
		}
	}

huangapple
  • 本文由 发表于 2020年10月18日 12:11:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/64409654.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定