主题:新手求助
import java.util.Scanner;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class Q2
{
public static void main(String[] args)
{
Scanner in=new Scanner(System.in);
char dd=0;
do
{
System.out.println("Please input your name:");
String name=in.next();
System.out.println("Please input your telephone:");
String tel=in.next();
Pattern p=Pattern.compile("[+][0-9][0-9]-[1-9][0-9][0-9]-[0-9][0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]");
Matcher m=p.matcher(tel);
boolean b=m.matches();
if(b==true)
{
System.out.println("Please input your age:");
int age=in.nextInt();
if(age>=20&&age<=60)
{
System.out.println("Please input your Birth Month:");
int bm=in.nextInt();
if(bm>=1&&bm<=12)
{
System.out.println ( "Name="+name+"\ntelephonenumber="+tel+"\nAge="+age+"\nBirth Month="+bm);
System.out.println("Do you want to run this program again,enter y or n");
String choice=in.next();
dd=choice.charAt(0);
}
else
System.out.println("Invalid month");
}
else
System.out.println("Invalid age");
}
else
System.out.println("Invalid telephone number");
}while(dd=='y'||dd=='Y');
}
}
为何dd非要给个初值 否则while那行就出错:The local variable dd may not have been initialized
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class Q2
{
public static void main(String[] args)
{
Scanner in=new Scanner(System.in);
char dd=0;
do
{
System.out.println("Please input your name:");
String name=in.next();
System.out.println("Please input your telephone:");
String tel=in.next();
Pattern p=Pattern.compile("[+][0-9][0-9]-[1-9][0-9][0-9]-[0-9][0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]");
Matcher m=p.matcher(tel);
boolean b=m.matches();
if(b==true)
{
System.out.println("Please input your age:");
int age=in.nextInt();
if(age>=20&&age<=60)
{
System.out.println("Please input your Birth Month:");
int bm=in.nextInt();
if(bm>=1&&bm<=12)
{
System.out.println ( "Name="+name+"\ntelephonenumber="+tel+"\nAge="+age+"\nBirth Month="+bm);
System.out.println("Do you want to run this program again,enter y or n");
String choice=in.next();
dd=choice.charAt(0);
}
else
System.out.println("Invalid month");
}
else
System.out.println("Invalid age");
}
else
System.out.println("Invalid telephone number");
}while(dd=='y'||dd=='Y');
}
}
为何dd非要给个初值 否则while那行就出错:The local variable dd may not have been initialized