0

please do keep in mind I am a brand new computer programmer and I am very stuck on this program I have to write. I already had asked a question earlier and got some great feedback and it's almost complete. The problem I am having is that in the program I am using a for a loop but technically I am only supposed to use while loops and am having major compile issues when trying to revert it to a while loop. Here is an example of the code.

import java.util.Scanner;

public class Harrison5a1 
{
    public static void main(String[] args) 
    {
        Scanner input = new Scanner(System.in);

         int integerx;
        System.out.print("Enter an even integer x: ");
        integerx = input.nextInt();

        while (integerx % 2 != 0) 
        {
            System.out.print("Try again, enter an even integer x: ");
            integerx = input.nextInt();
        }

        for (int x = 4; x <= integerx; x += 4) 
        {
            System.out.println("4 is a multiple of " + x);
        }
    }
}
13
  • 2
    What are your "major compile issues"? Commented Feb 20, 2017 at 12:51
  • It running fine no compilation issues. Commented Feb 20, 2017 at 12:52
  • 1
    Your question is way too specific to be any kind of useful to the StackOverflow community. Nonetheless, if you want to transform your for loop into a while : int x = 4; while(x <= integerx) { System.out.println("4 is a multiple of " + x); x += 4; } Commented Feb 20, 2017 at 12:53
  • so you want to transform your last (for loop) to a (while loop) Commented Feb 20, 2017 at 12:56
  • @OmarAbdelhafiz that is correct Commented Feb 20, 2017 at 12:58

2 Answers 2

2

if all you want is to transform the last loop (for loop) into a while then all you need is

int x = 4; // the for loop initialization 
while(x <= integerx){ // for loop evaluation (done every iteration)
    System.out.println("4 is a multiple of " + x); // whatever you want
    x += 4; // for loop update 
}

This does the exact same thing as a foor loop, and every for loop can be turned into a while loop and vice-versa, it can be more readable or not but that is up to the programmer.

EDIT I have tested it on Online Compiler Java and it works as you intend (if i perceived your requirements in the comments rightfully).

Sign up to request clarification or add additional context in comments.

8 Comments

exactly how i had it coded and it works just fine, but I am having an issue with the output giving me the wrong answer
Enter an even integer x: 12 4 is a multiple of 8 4 is a multiple of 12 4 is a multiple of 16
see I am getting 4 is a multiple of 16 also it won't display 4 is a multiple of 4 and I don't understand why
what is the expected answear and the input, we need to see your input, output and expected output please.
Expected input is an even number from the user then from there the program must calculate the multiples of it by 4 so an example would be the user inputted 12 as is integer. the expected output thus would be 4 is a multiple of 4 4 is a multiple of 8 4 is a multiple of 12
|
0

Your original requirements, posted 2 days ago where you got the code you're now trying to fix, are as follows: Your program will do the following:

  1. Prompt the user to enter an even integer x
  2. Continue to prompt the user for x until the user enters an even integer
  3. The program will then print all the even integers between 2 and x that are multiples of four.

None of the code above will accomplish step 3. I suggest you read step 3 very closely, figure out how you would accomplish it without code (on a piece of paper, for example) and only then try to code it. You also need to read your text book and actually understand what these loops are doing instead of asking people to write them for you. You're in school, presumably, to learn how to code. Come back and ask a reasonable question after you have actually tried to do it and looked at your text book and some other examples.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.