Home > Back-end >  Add from the judgment for loop, don't jump out, bosses, how do I solve?
Add from the judgment for loop, don't jump out, bosses, how do I solve?

Time:01-16

Package shopping;

import java.util.Scanner;

Public class jieMian {
Public static void main (String [] args) {
Denglu ();

}

Public static void denglu () {
//declare
String name="admin".
Int num=123;
int a;
System. Out.println (" t \ \ t welcome to use its own will shopping management System!" );
System. The out. Println (" \ \ t t1. Login System ");
System. The out. Println (" \ \ t t2. Exit ");
System. The out. Println (" * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ");
Scanner input=new Scanner(System.in);
For (; ; ) {
System. The out. Println (" please select, input number: ");
{if (input. HasNextInt ())
A=input. NextInt ();
If (a==1) {
For (; ; ) {
System. The out. Println (" please enter account: ");
String name1=input. Next ();
System. The out. Println (" please enter the password: ");
Int num1=input. NextInt ();
If (name1. Equals (name) & amp; & Num1=={num)
break;
} else {
System. Out.println (" user name password mistake, please input again!" );
}
}
break;
} else if (a==2) {
System. Out.println (" welcome to you next time!" );
break;
} else {
System. Out.println (" input error, please input again!" );
}
} else {
System. Out.println (" input error, please input again!" );
continue;
}
}
}
}

CodePudding user response:

Input. HasNextInt () to the input. HasNext () give it a try

CodePudding user response:


/* ** @ author: jiaolian 
* @ date: Created in 2021-01-15 they
* @ description: logical error
* @ modified By:
* public: call practice
*/
Public class jieMian {
Public static void main (String [] args) {
Denglu ();

}

Public static void denglu () {
//declare
String name="admin".
Int num=123;
String a;
System. Out.println (" t \ \ t welcome to use its own will shopping management System!" );
System. The out. Println (" \ \ t t1. Login System ");
System. The out. Println (" \ \ t t2. Exit ");
System. The out. Println (" * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ");
Scanner input=new Scanner(System.in);
For (; ; ) {
System. The out. Println (" please select, input number: ");
{if (input. HasNext ())
A=input. Next ();
Try {
//defines the number of input;
Int count=Integer. The valueOf (a);
If (count==1) {
For (; ; ) {
System. The out. Println (" please enter account: ");
String name1=input. Next ();
System. The out. Println (" please enter the password: ");
Int num1=input. NextInt ();
If (name1. Equals (name) & amp; & Num1=={num)
break;
} else {
System. Out.println (" user name password mistake, please input again!" );
}
}
break;
} else if (count==2) {
System. Out.println (" welcome to you next time!" );
break;
} else {
System. Out.println (" input error, please input again!" );
}
} the catch (Exception e) {
System. Out.println (" input error, please input again!" );
}

} else {
System. Out.println (" input error, please input again!" );
continue;
}
}
}
}

The problem here is that logic errors, the console itself should receive a string, but you are hard to use hasNextInt () is no problem, but don't forget to do with the try catch the exception handling oh, like to help you, some attention oh