简体   繁体   English

编写一个 while 循环,打印所有可被 10 整除且小于给定数 n 的正数

[英]Write a while loop that prints all positive numbers that are divisible by 10 and less than a given number n

I have this homework problem and I cannot seem to get it at all.我有这个家庭作业问题,但我似乎根本无法解决。 the suggested outcome if n were to be 100 would be 10 20 30 40 50 60 70 80 90如果n100 ,则建议的结果为10 20 30 40 50 60 70 80 90

I know my question is probably really stupid but I cannot seem to understand this for the life of me.我知道我的问题可能真的很愚蠢,但我似乎无法理解这一点。

Scanner in = new Scanner(System.in);
      System.out.print("n: ");
      int n = in.nextInt();
      
      while (n % 10 < 1)
      {
         System.out.print(n + " ");
         n = n - 1;
      }
      System.out.println(); 

There are many solutions, you can do it with help of an extra variable.有很多解决方案,您可以借助一个额外的变量来完成。

Scanner in = new Scanner(System.in);
System.out.print("n: ");
int n = in.nextInt();
int iter = 10;
while(iter < n)
{
    System.out.print(iter + " ");
    iter += 10;
}

I guess you were trying to make the program without using an extra variable, but you have to take an extra variable if you want all the numbers to be displayed in increasing order.我猜你试图在不使用额外变量的情况下编写程序,但如果你希望所有数字以递增顺序显示,则必须使用额外变量。 You can try this code snippet out.您可以试试这个代码片段。 It will work.它会起作用。

  Scanner in = new Scanner(System.in);
  System.out.print("n: ");
  int n = in.nextInt();
  int a = 10;
  while(a <= n){
    System.out.print(a+" ");
    a += 10;
  }

Not in Java, but here is my solution in C++ where I just decrement n by 10 because it needs to round down.不在 Java 中,但这是我在 C++ 中的解决方案,我只是将 n 减 10,因为它需要向下舍入。

#include <iostream>

using namespace std;

int main()
{
   cout << "n: " << endl;
   int n; 
   cin >> n;

   int count = 0;
   while (count < (n-10))
   {  
      count += 10;
      
       cout << count " ";
   }

   cout << endl;
    
   return 0;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何编写打印给定数字是否可被 3 整除的程序? [等候接听] - How to write a program that prints whether a given number is divisible by 3? [on hold] 能被 1 到 100 的所有数整除的最小正数是多少? - What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 100? 能被1到20的所有数均分的最小正数是多少? - What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20? 如何用Java编写一个输出小于n的完美数字的函数? - How would I write a function in Java that prints out perfect numbers less than n? 如何显示小于给定数字的所有偶数? - How to display all even numbers less than a given number? 给定数字n,我们必须找出小于或等于n的正好有3个除数的数字 - given a number n , we have to find out such numbers that are less than or equal to n, that have exactly 3 divisors Project Euler #5(可被 1 到 20 的所有数字整除的最小正数):优化方法? ~爪哇 - Project Euler #5(Smallest positive number divisible by all numbers from 1 to 20): Ways to Optimize? ~Java 试图找到可被1到20的所有数字均分的最小正数 - Trying to find the smallest positive number that is evenly divisible by all of the numbers from 1 to 20 打印出素数小于给定数N - Print out prime Number less than a given number N 编写一个只使用 while 循环打印数字金字塔的 Java 程序 - Write a java program that prints a pyramid of numbers using only a while loop
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM