java - how to apply system exit when pressing 0 -
so have factorial program accepts input user. working fine i'm trying figure out how end program inputting 0. since it's on boolean operation can't input if statement because compile error.
import java.util.*; import java.io.*; public class assignment2a { public static void main(string[] args){ scanner scan = new scanner(system.in); boolean correct = false; while(!false){ try{ system.out.println("welcome factorial function"); system.out.println("please enter number or press 0 exit"); int n = scan.nextint(); // if(n=0){ // system.exit(0); //} int factorial= fact(n); system.out.println("the factorial of number entered is: " + factorial); correct = true; }catch(exception e){ system.out.println("that not number!"); system.exit(0); } } } static int fact(int n){ int output; if(n==1){ return 1; } output = fact(n-1)*n; return output; } }
unfortunately, there isn't (easy) way this. closest equivalent implement keylistener
, that's swing methods (windows , stuff, not cli).
instead, should check if value has been entered. example, 0! 1, have program "enter number factor, or enter 0 exit". negative numbers, since doesn't program handle them well. on side note, maybe should add check negative numbers, too.
Comments
Post a Comment