
Java 无法解析变量?
小白自学,不知错在哪里请大神指教Exceptioninthread"main"java.lang.Error:Unresolvedcompilationproblem:p...
小白自学,不知错在哪里请大神指教Exception in thread "main" java.lang.Error: Unresolved compilation problem: pw2 cannot be resolved to a variable源代码:import java.util.Scanner;public class Doc { String Psd = "0"; public static void main(String []args) { Scanner sc = new Scanner(System.in); Boolean Cheeking = true; Boolean Cheek2 = true; while(Cheeking) { System.out.println("请输入您的密码"); String Psd = sc.nextLine(); int Psdsize = Psd.length(); //密码字符数判断 if(Psdsize>=6 && Psdsize<=11) { Cheeking = false; }else if(Psdsize < 6) { System.out.println("Password is to short"); }else { System.out.println("Password is to long"); } //二次密码比对 do { System.out.println("请确认密码"); String pw2 = sc.nextLine(); if(!Psd.equals(pw2)){ System.out.println("Password set UNsuccessfully"); } }while(!Psd.equals(pw2)); } }}
展开
1个回答
展开全部
pw2 为 do 代码块中的局部变量,你要把它提到外面来,才能在 while 中访问。即:
String pw2 = null;
do{
//....
pw2 = sc.nextLine();
//....
}while(!Psd.equals(pw2));
String pw2 = null;
do{
//....
pw2 = sc.nextLine();
//....
}while(!Psd.equals(pw2));
更多追问追答
追问
老师 能否咱加个微信,新学路上总会遇到问题。如果方便我请您吃饭。
追答
可私信。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询