我幾分鐘前發佈了一個問題,並要求我發佈「SSCCE」來證明我的問題。我大量壓縮了我的程序,所以如果一些結構沒有意義,或者看起來比新手的工作更加低效,那應該只是這個操作的產物。如果聲明由於未知原因返回false
現在,我的程序主要用於將一系列俄語動詞變形複製到集中式陣列,oneRay。在這裏的設置中,空白行上方顯示提示,在該空白行中輸入適當的綴合。按下「提交」按鈕假設檢查陣列的答案,但由於我自己的錯誤或缺乏理解,即使直接複製我所知道的正確答案,我也會看到「失敗」消息。有了這條線我得到的輸入:
build1 = new StringBuilder(ssfield1.getText());
,並用此我檢查它反對元素:
if(build1.equals(oneRay[pick][1].toLowerCase().replaceAll("\\s","")))ssfield1.setText("CORRECT");
else{ssfield1.setText("FAIL");}
我覺得這可能是檢查對錯誤輸入錯誤的元素的一個簡單的問題文本框,但它那下跪是,所以這裏是我的凝聚「SSCCE」所有代碼:
不長,我希望,肯定自足(和相當簡單太): 讓我知道,如果你需要其他任何東西,或者如果這些問題爲時已晚!
package wbh;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class SwingImplementation_RUS extends JFrame{
String [] [] oneRay;Random random = new Random();
JPanel panel; JTextField field,sfield1,sfield2,ssfield1,ssfield2; JButton buton;
int pick, hold;StringBuilder build1,build2;
public SwingImplementation_RUS(final int subj){
super("To be updated");
final String RUS_1[][]={
{"знать","знаю","знаешь","знает","знает","знает","знаем","знаете","знают",}};
new Thread(new Runnable() {
public void run() {
buton = new JButton("Submit");
setLayout(new GridLayout(3,1,3,3));
panel = new JPanel();panel.setLayout(new GridLayout(5,2,3,3));
field = new JTextField();
sfield1 = new JTextField("Я");sfield2 = new JTextField("Ты");
ssfield1 = new JTextField("");ssfield2 = new JTextField("");
buton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent m) {
System.out.println("CHECKED");
build1 = new StringBuilder(ssfield1.getText());
build2 = new StringBuilder(ssfield2.getText());
called2();}});
Font f = new Font("Arial", Font.BOLD, 75);
field.setFont(f);
add(field);
add(panel);
panel.add(sfield1);panel.add(ssfield1);
panel.add(sfield2);panel.add(ssfield2);
add(buton);
setLocation(500,0);setSize(865, 700);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setVisible(true);
}}).start();
oneRay = new String[RUS_1.length][9];
for(int i = 0; (RUS_1.length) > i; i++){
oneRay[i][0] = RUS_1[i][0];oneRay[i][1] = RUS_1[i][1];
oneRay[i][2] = RUS_1[i][2];}
try {Thread.sleep(100);} catch (InterruptedException e) {e.printStackTrace();}
hold = oneRay.length;pick = random.nextInt(hold);
field.setText(" "+oneRay[pick][0]);
}
private void called2(){
if(build1.equals(oneRay[pick][1].toLowerCase().replaceAll("\\s","")))ssfield1.setText("CORRECT");
else{ssfield1.setText("FAIL");}
if(build2.equals(oneRay[pick][2].toLowerCase().replaceAll("\\s","")))ssfield2.setText("CORRECT");
else{ssfield2.setText("FAIL");}
}
public static void main (String [] args){
SwingUtilities.invokeLater(new Runnable(){
public void run() {
new SwingImplementation_RUS(1);
}
});
}
}
您正在使用'equa一個StringBuilder對一個String的方法。我想這是錯誤。嘗試'ssfield1.getText()。equals(...)' – Fildor
@Fildor +1,嘗試使用build1.toString()。等於 –
@IlyaBursov你說得對,那應該會更好。 – Fildor