有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java可以创建组合框方法吗

这是我想在各种组合框上执行的代码块

重要信息:

AssignRollno()是我创建的一个方法,它返回一个int值,并按照它所说的那样为新用户分配一个rollno。字符串“a”是用户的名称。方法execute()执行mySQL中的字符串

问题:

现在,有一些组合框命名为d1 d2 d3。。。。。d8。我不想把整个代码写8次,而是想写一次,让它在所有组合框中执行。如果可能的话。所以,我想像for循环一样执行它(显然for循环不允许我这样做)

在下面的代码中,我只希望“x”将增量保持在1到8之间,并执行代码块8次 有没有办法做到这一点

 String a=t1.getText();
 int i=AssignRollno();
 int j=dx.getSelectedIndex();
 if(j==0)
 {
 String str1="insert into dx values("+i+",'"+a+"')";
 execute(str1);
 String str2="insert into t"+i+"1"+" values(0)";
 execute(str2);
 }
 else
{
String str3="insert into t"+i+"1 values("+j+")";
execute(str3);
}

共 (2) 个答案

  1. # 1 楼答案

    我不太确定你到底想要什么。但您可以为组合框使用数组:

        ComboBox[] comoboboxes = new ComboBox[8];
        comoboboxes[0] = d1;
        comoboboxes[1] = d2;
        comoboboxes[2] = d3;
        comoboboxes[3] = d4;
        comoboboxes[4] = d5;
        comoboboxes[5] = d6;
        comoboboxes[6] = d7;
        comoboboxes[7] = d8;
    
        for (int x = 0; x < 8; x++) {
            String a = t1.getText();
            int i = AssignRollno();
            int j = comboboxes[x].getSelectedIndex();
            if (j == 0) {
                String str1 = "insert into dx values(" + i + ",'" + a + "')";
                execute(str1);
                String str2 = "insert into t" + i + "1" + " values(0)";
                execute(str2);
            } else {
                String str3 = "insert into t" + i + "1 values(" + j + ")";
                execute(str3);
            }
        }
    
  2. # 2 楼答案

    用逗号怎么样

    范例

    插入到dx值中

    ("+i+",'"+a+"'),
    
    ("+i+",'"+a+"'),
    
    ("+i+",'"+a+"'),
    
    ("+i+",'"+a+"');
    

    问题重复Inserting multiple rows in mysql