[英]Is it possible to shorten this code?
我並不是Java的初學者,但我也不是專家。 有什么方法可以縮短此代碼,使其占用更少的空間並可能更少的行數?
JOptionPane.showMessageDialog(null, "The student's names are: "
+ roster[0][0] + " " + roster[1][0] + ", "
+ roster[0][1] + " " + roster[1][1] + ", "
+ roster[0][2] + " " + roster[1][2] + ", and "
+ roster[0][3] + " " + roster[1][3] + ".");
引入變量以消除重復。
T[] col1 = roster[0]; T[] col2 = roster[1]; String content = col1[0] + " " + col2[0] + ", " + col1[1] + " " + col2[1] + ", " + col1[2] + " " + col2[2] + ", and " + col1[3] + " " + col2[3] + "."; JOptionPane.showMessageDialog(null,"The student's names are: " + content);
將字符串串聯成多個分配,則除最后一個分配外,所有分配均相同。
int i = 0; String content = ""; content += col1[i] + " " + col2[i] + ", ";i++; content += col1[i] + " " + col2[i] + ", ";i++; content += col1[i] + " " + col2[i] + ", ";i++; content += "and " + col1[i] + " " + col2[i] + ".";i++;
使用三元運算符使多重分配保持一致。
int i = 0; String content = ""; content+= (i==3?"and ":"") + col1[i]+" "+col2[i] + (i==3?".":", "); i++; content+= (i==3?"and ":"") + col1[i]+" "+col2[i] + (i==3?".":", "); i++; content+= (i==3?"and ":"") + col1[i]+" "+col2[i] + (i==3?".":", "); i++; content+= (i==3?"and ":"") + col1[i]+" "+col2[i] + (i==3?".":", "); i++;
使用while循環刪除重復項。
int i = 0; String content = ""; while(i<=3) { content+= (i==3?"and ":"") + col1[i]+" "+col2[i] + (i==3?".":", "); i++; }
用for循環替換while循環。
String content = ""; for (int i = 0; i <= 3; i++) { content+= (i==3?"and ":"") + col1[i]+" "+col2[i] + (i==3?".":", "); }
引入變量以使代碼更具可讀性。
String content = ""; for (int i = 0; i <= 3; i++) { String prefix = i == 3 ? "and " : ""; String current = col1[i] + " " + col2[i]; String suffix = i == 3 ? "." : ", "; content += prefix + current + suffix; }
內聯僅使用一次的變量col1
和col2
:
String content = ""; for (int i = 0; i <= 3; i++) { String prefix = i == 3 ? "and " : ""; String current = roster[0][i] + " " + roster[1][i]; String suffix = i == 3 ? "." : ", "; content += prefix + current + suffix; }
用常數替換魔術數字3
,最終代碼如下:
final int last = 3; String content = ""; for (int i = 0; i <= last; i++) { String prefix = i == last ? "and " : ""; String suffix = i == last ? "." : ", "; String current = roster[0][i] + " " + roster[1][i]; content += prefix + current + suffix; } JOptionPane.showMessageDialog(null, "The student's names are: " + content);
StringBuilder message = new StringBuilder("The student's names are: ");
for (int i = 0; i < roster[0].length; i++) {
message
.append(roster[0][i])
.append(" ")
.append(roster[1][i]);
if (i < roster[0].length - 1)
message.append(", ");
if (i == roster[0].length - 2)
message.append("and ")
}
message.append(".");
JOptionPane.showMessageDialog(null, message.toString());
可能是這樣的。 如您所見,您實際上並沒有保存任何行,但是顯然,由於可以考慮可變長度的花名冊,因此代碼更加靈活。
您可以執行以下操作:最初分配您不想重復的普通語句。 然后遍歷花名冊。
String rosterString= "The student's names are: ";
for(int i=0;i<= roster.length;i++){
for(int j=0;j<= roster[i].length;j++){
rosterString += (roster[i][j] + " ");
if (i == 1 && j < 2) {
rosterString += ", ";
}
else if (i == 1 && j == 2) {
rosterString += ", and";
}
else if (i == 1 && j == 3) {
rosterString += ".";
}
else {
rosterString += " ";
}
}
}
然后將rosterString
傳遞給您的方法。
String rosterString = "";
for(int i = 0; i < roster[0].length; i++) {
rosterString += roster[0][i] + " " + roster[1][i] + ", ";
}
該代碼將創建一個包含名稱和逗號的字符串。 然后,您可以添加在if語句來檢查它是否接近尾聲改變,
一個and
或.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.