正如您所看到的,這是我正在處理的代碼片段。我的問題是爲什麼我必須創建一個新的掃描儀才能輸入?爲什麼我不能只用stdIn.nextLine();
呢?如果你可以包含一些參考文獻,我將非常感激。爲什麼我必須創建一個新的掃描儀?創建驅動程序時
import java.util.Scanner;
public class Week6{
public static void main(String[]args){
Horse horse1;
Horse horse2;
Horse horse3;
String name;
int age;
double height;
String color;
boolean pureBlood;
Scanner in = new Scanner(System.in);
horse1 = new Horse(); // creation of Horse1
horse2 = new Horse(); // creation of horse2
horse3 = new Horse(); // creation of horse3
System.out.print("Enter the name of Horse1: ");
name = in.nextLine();
horse1.setName(name); //setting name for horse1
System.out.println(horse1.getName());
} // end main
} // end class
接受jondiaz的答案,因爲他的答案是完美的! –