0

I know this question has been asked several times already. Feel free to mark it as a duplicate. Anyway, I'd rather ask the community since I am still uncertain.

I should convert this while loop in a do-while loop. Any thoughts?

public class DoWhile {
      public static void main(String[] args) {
           Scanner input = new Scanner(System.in); 
           int sum = 0;
           System.out.println("Enter an integer " + "(the input ends if it is 0)"); 
           int number = input.nextInt(); 
           while (number != 0) {
               sum += number;
               System.out.println("Enter an integer " + "(the input ends if it is 0)"); 
               number = input.nextInt();
           }
      }
}
2
  • You'd help all readers of your question if you formatted your code section with indentation and correct line breaks. Please consider to edit. Commented Oct 2, 2016 at 20:32
  • Seems like code review kind of question. codereview.stackexchange.com Commented Oct 3, 2016 at 5:34

5 Answers 5

1

You cant just simple convert any while loop to do while loop , the main difference between them is in do while loop you have an iteration that will happen regardless of the condition.

      public class DoWhile {
           public static void main(String[] args) {
             int number=0;
             Scanner input = new Scanner(System.in); int sum = 0;
          do{ System.out.println("Enter an integer " +"(the input ends if it is  0)"); 
          number = input.nextInt(); 
          sum += number;
      }while (number != 0) ;


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

3 Comments

I already know this, in fact, this is an example from dr. Liang "Introduction to Java" Book.
That's much clearer now, thanks. Sorry everyone for the hassle
This will not compile since you have declared number inside the loop.Fix this and you are ok.
0
public class DoWhile {
   public static void main(String[] args) {

   Scanner input = new Scanner(System.in); 
   int sum = 0;
   int number = 0;
   do {
          System.out.println("Enter an integer " +
          "(the input ends if it is 0)");
          number = input.nextInt();
          sum += number;
   } while(number != 0)

} }

Comments

0

You have to tell the program to continue doing something in the "do" block. In your own case you have to tell the program to keep doing this" System.out.println("Enter an integer " + "(the input ends if it is 0)"); number = input.nextInt(); sum += number;". Then in the "while" block you will have to provide the terminating statement, in your own case "number != 0"

public class DoWhile {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int sum = 0;
        int number;        
        do {     
            System.out.println("Enter an integer " + "(the input ends if it is 0)");
            number = input.nextInt();
            sum += number;

        } while (number != 0);
    }
   }

Comments

0
   public class DoWhile {

       public static void main(String[] args) {

           Scanner input = new Scanner(System.in); int sum = 0;

           System.out.println("Enter an integer " + "(the input ends if it is 0)"); 
           int number = input.nextInt();
           //You need this if statement to check if the 1st input is 0
           if(number != 0)
           {
               do
               {
                    sum+=number;
                    System.out.println("Enter an integer " + "(the input ends if it is 0)"); 
                    number = input.nextInt(); 
               }while(number != 0);

          }

     }

}

Comments

0

classInfoClass

{
   public void setinfo()


    {
        int  userage;


        do

        { Console.Write("Please enter your age: ");


         } while (!int.TryParse(Console.ReadLine(),out userage));

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.