Java:如何在用户int输入中读取“ - ”

时间:2013-10-22 04:11:32

标签: java input int

我一直试图让java读取“ - ”以获取用户输入的电话号码... Idk how,help!

2 个答案:

答案 0 :(得分:1)

String phoneNumber = xxx-xxx-xxx;

for(int i = 0;i<phoneNumber.length();i++){

   if(phoneNumber[i] == '-')

    {
        System.out.println(phoneNumber[i]);

    }

}

答案 1 :(得分:1)

     Scanner scan = new Scanner(System.in);
     System.out.println("Enter the phone no");
     String phoneNo = scan.nextLine();
     System.out.println("Phone No: " + phoneNo);
     scan.close();

输出

Enter the phone no
122-121-121
Phone No: 122-121-121