简体   繁体   中英

Correcting issues with the “While” loop

I just started with while loops today and I'm currently struggling with a code. I must ask a user to enter a starting and ending value for the while loop. The results should display all the multiples of 4 between the starting and ending value. This is homework so a while loop must be included.

For example, the user enters 1 for the starting value and 4 for the ending value. The code should display 4 * the values in between the starting and ending value). I don't know how to make the numbers in between multiply with 4.

import javax.swing.JOptionPane;

public class WhileEx {

public static void main(String[] args){ 

int start = Integer.parseInt(JOptionPane.showInputDialog("Enter a starting number (integer)")); 
int end = Integer.parseInt(JOptionPane.showInputDialog("Enter an ending number (integer)"));        

while (start < end)
{
    start = start * 4;
    System.out.print (start +  "  ");       
}
}
}

I'll give you two hints:

  1. What is the first multiple of 4 which is greater than or equal to start ? Your program should work for cases where start is already a multiple of 4 (say, 32) and when it is not (eg 29).

  2. How do you get from a certain multiple from 4 (say, 32) to the next one (36)?

If instead, you need to

display 4 * the values in between the starting and ending value

the simplest solution is then:

while (start <= end)
{
    System.out.print((start * 4) + " ");
    start = start + 1;
}

To do this, you need the % , which returns rest after division.

This is how you can do it, there is solution with simple for cycle and then with while cycle.

public static void main(String[] args) {
    int start = Integer.parseInt(JOptionPane.showInputDialog("Enter a starting number (integer)"));
    int end = Integer.parseInt(JOptionPane.showInputDialog("Enter an ending number (integer)"));

    for (int i = start; i < end; i++) {
        if (i % 4 == 0){
            System.out.print(i + " ");
        }
    }

    System.out.println("");

    int i = start + (4 - (start % 4));
    while (i < end){
        System.out.print(i + " ");
        i += 4;
    }
}

Output for values 2 and 63 :

4 8 12 16 20 24 28 32 36 40 44 48 52 56 60 
4 8 12 16 20 24 28 32 36 40 44 48 52 56 60 

I would suggest using the mod operator -> % That would give you the remainder and if it's 0 - you got a multiple of 4.

Example:

if(start%4 == 0){
System.out.print (start); 
}
start++;

Since you are updating start value to its 4xtimes value, in the next iteration of while loop, this new value will be checked, which is not correct. Do not update the same variable which is used in the while condition. Use another variable to multiply by 4 and then use it to display.

Edit: You need to use the increment operator ++ to increase the start value to reach the end .

Also, your while loop should be like this:

int temp=0;
while (start < end)
{
temp = start * 4;
System.out.print (temp +  "  ");       
start++; 
}

Better yet:

while (start < end)
{
System.out.print ((start * 4) +  "  ");       
start++; 
}

@kittykittybangbang: Thanks for suggesting the edit.

    import javax.swing.JOptionPane;

    public class WhileEx {

    public static void main(String[] args){ 

    int start = Integer.parseInt(JOptionPane.showInputDialog("Enter a     starting number (integer)")); 
    int end = Integer.parseInt(JOptionPane.showInputDialog("Enter an ending   number (integer)"));  
    if(start==0 || start%4>0)
    {
        start = start +(4-start%4);
    }
    while (start <= end)
    {
        System.out.print (start +  "  ");       
        start = start + 4;

    }
    }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM