mercredi 22 avril 2020

How to end this program when user inputs "end"?

So basically I am trying to figure out how to stop this program using Integer.valueOf(String s) or Integer.parseInt(String s) by typing "end" for the user input

import java.util.Scanner;

public class monkey1{

    public static void main(String[] args){
    Scanner s = new Scanner(System.in);
    int sum = 0;
    int lol = 1;
        do { 
            System.out.print("Enter a number: ");
              lol = s.nextInt();
            sum += lol;
        if (lol > 0){
            System.out.println("Sum is now: " + sum);

        }
        }
    while (lol>0);

    }

}

Aucun commentaire:

Enregistrer un commentaire