0

After putting all the numbers input the loop terminates and the program ends, after asking whether to continue or not.

import java.util.Scanner;

public class coding {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner in = new Scanner (System.in);
        String choose;
        int i;
        double ans;

        do {
            int num1= in.nextInt();
            int num2= in.nextInt();
            int num3= in.nextInt();
            int num4 = in.nextInt();
            int num5 = in.nextInt();
             ans = (num1+num2+num3+num4+num5)/5;
            System.out.println(ans);
            //in.nextLine();
            System.out.println("Continue the program Yes or N?");
            
            choose = in.nextLine();


        }while (choose.equalsIgnoreCase("Yes"));

    }

Mustafa Poya
  • 2,615
  • 5
  • 22
  • 36
  • 1
    Does this answer your question? [How to get the user input in Java?](https://stackoverflow.com/questions/5287538/how-to-get-the-user-input-in-java) – Amir Dora. Nov 16 '20 at 18:53

1 Answers1

0

the program will continue on Yes and will end on N, so you need to change it the while condition to this: !choose.equalsIgnoreCase("N") to check while its not true then iterate.

secondly you used choose = in.nextLine(); which cause error on next iteration, which you need to change it to choose = in.next(); to run without error.

choose = in.nextLine(); it cause to store the enter value in next call of in.nextInt() and throw an error.

Scanner in = new Scanner(System.in);
String choose;
int i;
double ans;

do {
    int num1 = in.nextInt();
    int num2 = in.nextInt();
    int num3 = in.nextInt();
    int num4 = in.nextInt();
    int num5 = in.nextInt();
    ans = (num1 + num2 + num3 + num4 + num5) / 5;
    System.out.println(ans);
    // in.nextLine();
    System.out.println("Continue the program Yes or N?");
    choose = in.next();

} while (!choose.equalsIgnoreCase("N"));
Mustafa Poya
  • 2,615
  • 5
  • 22
  • 36