如何使用 if/else 语句在 JTextField 中打印出多个文本?
How do I printout multiple text in JText Field using if/else statement?
private void resultActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if(pick1 > pick2 ){
resultstf.setText("The New President is Koon!");
}
else if(pick2 > pick1){
resultstf.setText("The New President is Baam!");
}
else if(pick1 == pick2){
resultstf.setText("The Result for the new President is a Tie! Please Vote Again.");
}
if(pick3 > pick4){
resultstf.setText("The New VP is Sachi!");
}
else if(pick4 > pick3){
resultstf.setText("The New VP is Faker!");
}
}
如何在每次按下结果按钮时打印出多个文本?就像我想同时打印出 "The New President is Koon" 和打印出 "The New VP is Sachi"。
使用 StringBuilder
并在进行过程中构建消息:
private void resultActionPerformed(java.awt.event.ActionEvent evt) {
StringBuilder message = new StringBuilder();
// TODO add your handling code here:
if (pick1 > pick2) {
message.append("The New President is Koon!\n");
}
else if (pick2 > pick1) {
message.append("The New President is Baam!\n");
}
else if (pick1 == pick2) {
message.append("The Result for the new President is a Tie! Please Vote Again.\n");
}
if (pick3 > pick4) {
message.append("The New VP is Sachi!\n");
}
else if (pick4 > pick3) {
message.append("The New VP is Faker!\n");
}
resultstf.setText(message.toString());
}
private void resultActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if(pick1 > pick2 ){
resultstf.setText("The New President is Koon!");
}
else if(pick2 > pick1){
resultstf.setText("The New President is Baam!");
}
else if(pick1 == pick2){
resultstf.setText("The Result for the new President is a Tie! Please Vote Again.");
}
if(pick3 > pick4){
resultstf.setText("The New VP is Sachi!");
}
else if(pick4 > pick3){
resultstf.setText("The New VP is Faker!");
}
}
如何在每次按下结果按钮时打印出多个文本?就像我想同时打印出 "The New President is Koon" 和打印出 "The New VP is Sachi"。
使用 StringBuilder
并在进行过程中构建消息:
private void resultActionPerformed(java.awt.event.ActionEvent evt) {
StringBuilder message = new StringBuilder();
// TODO add your handling code here:
if (pick1 > pick2) {
message.append("The New President is Koon!\n");
}
else if (pick2 > pick1) {
message.append("The New President is Baam!\n");
}
else if (pick1 == pick2) {
message.append("The Result for the new President is a Tie! Please Vote Again.\n");
}
if (pick3 > pick4) {
message.append("The New VP is Sachi!\n");
}
else if (pick4 > pick3) {
message.append("The New VP is Faker!\n");
}
resultstf.setText(message.toString());
}