开发者

Java, Infinite loop- multiples of 2 only

开发者 https://www.devze.com 2023-03-08 03:09 出处:网络
I am asked to print multiples of 2 onlywith a never ending loop. Attempt: import java.util.Scanner; public class Infiniteloop {

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.开发者_C百科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.


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.");
    }


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.


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);


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


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
}


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);

}}}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号