Can do-while loops only run with boolean variables? Java

Mayo :

I am having some trouble with this small segment. Can do-while loops only run using boolean variables? I am trying to ask the "user" to enter "0" or "1" so that the program will either loop or end.

Error message:

Chapter4Practice.java:23: error: incompatible types: int cannot be converted to boolean } while (choice = 1); ^ 1 error

My code:

import java.util.Scanner;

public class Chapter4Practice
{
    public static void main(String[] args)
    {
        int choice, num1, num2;
        String input;
        Scanner sc = new Scanner(System.in);

        do {
            System.out.print("Enter a number: "); 
            num1 = sc.nextInt();
            System.out.print("Enter another number: ");
            num2 = sc.nextInt();
            System.out.println("The sum is " + (num1 + num2));
            System.out.println("Do you want to do this again?");
            System.out.println("(1 = yes, 0 = no)");
            sc.nextLine();
            choice = sc.nextInt();
        } while (choice = 1);
    } //End Main

} //End Class
Elihu Del Valle :

Short answer: Yes, you can have simple or complex statements inside your do-while but at the end it will have to evaluate to either true or false

Also your statement should be == ( single = means assign, where == is evaluate)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=24945&siteId=1