我目前正在研究一個項目,要求我設置數據輸入。在數據輸入模式下,用戶將被要求在循環中輸入科學家數據。如果用戶以'y'迴應,他們將被提示輸入另一位科學家。我認爲可以這樣做的最佳方式是使用do-while循環來填充數組,直到用戶決定結束。我有麻煩Do-While循環來填充陣列
- 填充名稱到陣列中,並
- 初始循環之後的程序將不會提示輸入一個名稱。
以下是我有:
public class Scientist {
private String name;
private String field;
private String greatIdeas;
public static void main(String[] args) {
String scientists[] = new String[100];
int scientistCount = 0;
Scanner input = new Scanner(System.in);
do{
String answer;
System.out.println("Enter the name of the Scientist: ");
scientists[scientistCount]=input.nextLine();
System.out.println(scientistCount);
System.out.println("Would You like to add another Scientist?");
scientistCount++;
}
while(input.next().equalsIgnoreCase("Y"));
input.close();
}
}
你確實有什麼樣的麻煩? – Berger
我無法使用do-while循環來填充數組,並且在初始循環之後,我不再提示輸入科學家名稱。 @Berger –