我已經循環了一個數組,所以首先它從輸入的數據全名抓取並將它拆分爲lname和fname,然後用循環內部的循環將所輸入的分數相加。我該如何清除/清空一個循環中的數組
當你結束第二個循環時,第一個循環再次要求String.split(在我的例子中是fullname),但它不允許我輸入新的數據來分割,而是給出錯誤「java.lang。 ArrayIndexOutOfBoundsException異常:1" ,並強調「字符串FNAME =零件[1];
任何想法,什麼都幫我拿上了正軌,將不勝感激
我也試過
string = "";
string = null;
!
這是我的代碼到目前爲止:
import java.util.Scanner;
public class TestScores
{
public static void main(String[] args) {
Scanner kb = new Scanner (System.in);
String fullname;
int sum = 0;
int count = 0;
int score;
System.out.println("Please enter name here e.g lastname, firstname");
fullname = kb.nextLine();
while (fullname.equals ("Done") !=true){
String[] parts = fullname.split(",");
String lname = parts[0];
String fname = parts[1];
System.out.println("Enter scores (Type -1 to finish scoring.");
score = kb.nextInt();
while(score != -1)
{
sum += score;
score = kb.nextInt();
count++;
}
System.out.println(fname + " " + lname + "'s total Score is: " + sum);
System.out.println();
System.out.println("Please enter another name here e.g lastname, firstname to start scoring");
fullname = kb.nextLine();
}
}
}
刪除C++標籤,因爲它與C++無關 – billz
'我只是在爲C++做...'這是java而不是C++! – myusuf
String []部分在while循環中,當循環結束時String []部分被破壞不是嗎? –