确定一个数是否包含特定数字

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

Determine if a number contains a specific digit

问题

以下是翻译好的内容:

我的想法是创建一个循环,并且在用户提供的两个数字之间不打印包含数字 3 的数字,比如 13、23、43 等等。

我的问题出在循环上。如何检查这些数字是否包含数字 3 呢?

举个例子,如果要打印从 2 到 24,就不应该打印 3、13 和 23。

  1. for (int i = x; i <= y; i++) {
  2. if (String.valueOf(i).contains("3")) {
  3. System.out.print("");
  4. } else {
  5. System.out.println(i);
  6. }
  7. }
英文:

My idea is to create a loop and not print numbers that contain a 3 in them like 13, 23, 43,etc. between 2 numbers given by a user.

My problem is on the loop. How do I check that the numbers contain a 3 on them?

For example if it prints from 2 to 24. It should not print 3,13 and 23.

  1. for(int i = x; i &lt;= y; i++){
  2. if(i%3 == 0){
  3. System.out.print(&quot;&quot;);
  4. else{
  5. System.out.println(i);
  6. }
  7. }

答案1

得分: -1

  1. for (int i = x; i <= y; i++) {
  2. if (i % 10 == 3) {
  3. System.out.print("");
  4. } else {
  5. System.out.println(i);
  6. }
  7. }
英文:
  1. for(int i = x; i &lt;= y; i++){
  2. if(i%10 == 3){
  3. System.out.print(&quot;&quot;);
  4. }
  5. else{
  6. System.out.println(i);
  7. }
  8. }

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

发表评论

匿名网友

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

确定