Java,无限循环 - 仅限 2 的倍数

发布于 2024-11-09 19:27:08 字数 577 浏览 0 评论 0原文

我被要求仅使用永无止境的循环来打印 2 的倍数。

尝试:

import java.util.Scanner;

public class Infiniteloop {

    public static void main (String [] args) 
    {
        Scanner input=new Scanner (System.in);
        int number,x;
        System.out.print("Enter a number");

        number=input.nextInt();
        if(number%2==0)
        {
            while(number>=0)
            {
                x= (++number);
                System.out.println(x);
            }
        }
    }
}

我只能使用while循环。所以我尝试将2的余数设置为零。我尝试使用计数器,但它不会增加它。不断打印出零。我需要一些帮助。谢谢。

I am asked to print multiples of 2 only with a never ending loop.

Attempt:

import java.util.Scanner;

public class Infiniteloop {

    public static void main (String [] args) 
    {
        Scanner input=new Scanner (System.in);
        int number,x;
        System.out.print("Enter a number");

        number=input.nextInt();
        if(number%2==0)
        {
            while(number>=0)
            {
                x= (++number);
                System.out.println(x);
            }
        }
    }
}

I can only use while-loop. So I tried to set the remainder of 2 equal to zero. I tried using the counter but it doesnt increment it. Keeps printing out zeros. I need some help. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

心欲静而疯不止 2024-11-16 19:27:08

假设您想提示用户输入起始数字,然后打印以下所有偶数:

number = input.nextInt(); //read the input
number += number % 2; //if input is odd, add 1
while (true)
{
   System.out.println (number);
   number += 2;
}

假设您想检查偶数:

while (true)
{
  number = input.nextInt();
  if (number % 2 == 0) System.out.println (number);
}

或者如果您不关心空行:

while (true) System.out.println (input.nextInt () % 2 == 0 ? "even" : "");

编辑:对于二的幂也是如此:

public static void main (String [] args)
{
    Scanner input = new Scanner (System.in);
    int number;
    while (true)
    {
        System.out.print ("Enter a number");
        number = input.nextInt ();
        while ( (number & 1) == 0) number >>= 1;
        if (number == 1) System.out.println ("Perfect divisor.");
    }

Supposing that you want to prompt the user for a start number and then print all the following even numbers:

number = input.nextInt(); //read the input
number += number % 2; //if input is odd, add 1
while (true)
{
   System.out.println (number);
   number += 2;
}

Supposing you want to check for even numbers:

while (true)
{
  number = input.nextInt();
  if (number % 2 == 0) System.out.println (number);
}

Or if you don't care about empty lines:

while (true) System.out.println (input.nextInt () % 2 == 0 ? "even" : "");

EDIT: Same thing for powers of two:

public static void main (String [] args)
{
    Scanner input = new Scanner (System.in);
    int number;
    while (true)
    {
        System.out.print ("Enter a number");
        number = input.nextInt ();
        while ( (number & 1) == 0) number >>= 1;
        if (number == 1) System.out.println ("Perfect divisor.");
    }
苏辞 2024-11-16 19:27:08

我很惊讶这个编译。

x= (++number)

末尾没有分号。

另外,将 if 语句移到 while 内。如果您要检查 2 的倍数,您将需要在循环

编辑的每次迭代之后进行检查:您更改了原始代码。请从源中复制/粘贴,而不是重新键入。

I am surprised this compiles.

x= (++number)

has no semi-colon at the end.

also, move the if statement inside of the while. If you are checking for multiples of 2, you will want that check after each iteration of the loop

edit: you changed your original code. Please copy/paste from your source instead of re-typing.

◇流星雨 2024-11-16 19:27:08

问题不是很清楚,但可能这样会对您有所帮助:

Scanner input=new Scanner (System.in);
int number;
do {
    System.out.print("Enter a number: ");
    number=input.nextInt();
    if(number%2==0)
       System.out.println(number);
} while (number > 0);

Question is not very clear but may be something like this would help you:

Scanner input=new Scanner (System.in);
int number;
do {
    System.out.print("Enter a number: ");
    number=input.nextInt();
    if(number%2==0)
       System.out.println(number);
} while (number > 0);
〆一缕阳光ご 2024-11-16 19:27:08

无限循环不需要计数器。可以这样写:

if((number % 2) != 0) {
    number++;
}

while(true) {
    System.out.println(number);
    number = number + 2;
}

编辑:添加无限查找2的倍数

An infinite loop does not need a counter. It can be written like this:

if((number % 2) != 0) {
    number++;
}

while(true) {
    System.out.println(number);
    number = number + 2;
}

edit: Added infinitely finding multiples of 2

霊感 2024-11-16 19:27:08

我猜这是一个家庭作业问题,所以也许解释方法比提供完整答案更能帮助您。

首先,您可以使用 while 循环来确保您的代码多次执行:

while 循环

while 循环将在给定的布尔条件计算结果为 true 时继续执行其中的代码。因此,您可以用以下内容包装您的代码:

while(true) {
//...
}

并且括号之间的任何内容都将永远持续执行(逐行)。

如果在循环开始时从用户处获得一个数字,则循环将停止执行任何进一步的代码,直到用户输入某些内容(它将被阻止,等待 IO)。

获得数字后,循环将开始执行其余代码,然后返回循环顶部并重复该过程。

while (true) {
//ask user for number

//print out the number

// check that it is even

// print whether it is even or odd
}

I'm guessing that this is a homework question, so perhaps explaining the methodology will help you more than a full answer.

Firstly, you can use a while loop to ensure that your code gets executed more than once:

while loop

A while loop will keep executing the code inside it while the given boolean condition evaluates to true. So, you can wrap up your code with:

while(true) {
//...
}

and anything between the brackets will continually execute (line by line) forever.

If you get a number from the user at the beginning of the loop, the loop will stop executing any further code until the user types something (it will be blocked, waiting on IO).

Once you get the number, the loop will start executing the rest of the code, before returning to the top of the loop and repeating the process.

while (true) {
//ask user for number

//print out the number

// check that it is even

// print whether it is even or odd
}
旧街凉风 2024-11-16 19:27:08
class Fordemo
{


public static void main(String args[])
{

int k,x=0;

for(k=1;k<=10;k++)
{

x=k*2;

System.out.println("multiple of 2 is "+x);

}}}
class Fordemo
{


public static void main(String args[])
{

int k,x=0;

for(k=1;k<=10;k++)
{

x=k*2;

System.out.println("multiple of 2 is "+x);

}}}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文