读书人

初学者提问

发布时间: 2012-04-11 17:42:33 作者: rapoo

菜鸟提问
怎样控制字符串的输入?(现在不知道要输入多少个字符串,数量由自己控制)

[解决办法]

Java code
public static void main(String[] args) {    try{    int a = 0;    Scanner scanner = new Scanner (System.in);    System.out.println("输入字符串个数是?:");    a = scanner.nextInt();        for (int n = 0; n <a; n++) {        String temp = scanner.nextLine();        System.out.println("第" + (n+1) +"字符串是?:" + temp);          }    catch(Exception e){        System.out.println("输入错误");            }    } 

读书人网 >软件架构设计

热点推荐