+ -
当前位置:首页 → 问答吧 → 字串读取问题 nextLine

字串读取问题 nextLine

时间:2011-09-04

来源:互联网

程式码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import java.util.*;
 
public class test {
  public static void main(String[] args) {
 
    int total = 0;
    Scanner input = new Scanner(System.in);
    System.out.printf("How many String do you have?");
    String[] array = new String[input.nextInt()];
 
    for (int counter = 0; counter < array.length; counter++) {
      System.out.printf("PLease enter " + (counter + 1) + " String");
      array[counter] = input.nextLine();
    }
 
    for (int counter = 0; counter < array.length; counter++) {
      if (array[counter].endsWith("kobe")) {
        System.out.printf("The %d " + array[counter] + "\n",
            (counter + 1));
        total++;
      }
    }
    System.out.printf("There are %d String endswith 'kobe'", total);
  }
 
}


小弟刚学java不久,在练习的时候有一个小问题想请问各位高手:

执行程式,输入String的数字后,为什么都会直接输出
PLease enter 1 StringPLease enter 2 String,叫我直接输入第二个的值呢??

为什么第一个会跳过去?

小弟不大会表示,还请各位见谅

作者: beginner   发布时间: 2011-09-04

改成

String[] array = new String[Integer.parseInt(input.nextLine())];

- - - - -

穿插输入 int 跟 string 我通常都会用nextLine() 去做转换

作者: way2570123   发布时间: 2011-09-05

热门下载

更多