Time:10-05
CodePudding user response:
if the else
import Java. Util. Scanner; Public class Cal {Public static void main (String [] args) {Scanner sc=new Scanner(System.in);//keyboard input Int x=sc. NextInt ();//input integer Int y; If (x<1) y=x; Else if (x<10) y=3 * x - 2; The else y=4 * x; System. The out. Println (x); } }
give you a better point, upstairs there are three problems (also not a problem, but I more rigorous) 1, the requirement of the output is the value of y, his output is the value of x, here is the error of 2, not the tip on the input, and no error handling (if the input is not the integer, Fried program directly, I do the tips here, at the same time is not the integer can handle exceptions) 3, the last is the if the else, no matter whether the back is only a word, all want to play on the {} notation, it conforms to the coding standards, at the same time convenient read Public static void main (String [] args) {Scanner sc=new Scanner(System.in);//keyboard input System. The out. Print (" please enter an integer: "); Int x=0; Try {X=sc. NextInt ();//input integer } the catch (Exception e) {System. The out. Println (" you entered is not the integer "); return;//end program } Int y; If (x<1) {Y=x; } else if (x<10) {Y=3 * x - 2; } else {Y=4 * x; } System. The out. Println (" y "+ y); }
Page link:https//www.codepudding.com/Backend/52515.html