pField = new unsigned char[fieldWidth * fieldHigh];//划定指针遍历的范围 for (int x = 0; x < fieldWidth; x++) for (int y = 0; y < fieldHigh; y++) pField[y * fieldWidth + x] = (x == 0 || x == fieldWidth - 1 || y == fieldHigh - 1) ? 9 : 0;解释这段代码的含义
时间: 2023-11-29 15:05:01 浏览: 66
这段代码的含义是创建一个二维数组,用于表示一个游戏场地的状态。该场地的宽度为fieldWidth,高度为fieldHigh。pField是一个指向无符号字符(unsigned char)类型的指针,用于遍历和访问数组中的元素。
在代码的下面部分,使用两个嵌套的循环来遍历整个场地。循环中,通过计算当前位置在数组中的索引,将对应位置的值设置为9或者0。
如果当前位置是场地的边界(即x等于0或者x等于fieldWidth-1,或者y等于fieldHigh-1),则将该位置的值设置为9,表示边界。否则,将该位置的值设置为0,表示非边界区域。
这段代码的目的是初始化场地数组,并将边界位置设置为9,非边界位置设置为0。
相关问题
public void actionPerformed(ActionEvent e) { String username=uField.getText().trim(); String password =pField.getText().trim(); String phonenum=tField.getText().trim(); String gender=bg.isSelected(maleBtn.getModel())?maleBtn.getText():femaleBtn.getText(); String Driver = "com.mysql.cj.jdbc.Driver"; String url = "jdbc:mysql://localhost:3306/data2?characterEncoding=UTF8&useSSL=false"; Connection conn = null; try{ Class.forName(Driver); conn = DriverManager.getConnection(url, "root", "yxh9497"); Statement stmt=conn.createStatement(); ResultSet rs = stmt.executeQuery("SELECT * FROM userinformation"); while(rs.next()) { if(username==rs.getString(username)) { JOptionPane.showMessageDialog(null, "用户名已被注册"); break; } } String sql1="insert into userinformation values('"+ username+ "','"+password+"','"+phonenum+"','"+gender+"')"; int a= stmt.executeUpdate(sql1); }catch(Exception ex){ ex.printStackTrace(); } try{ //关闭数据库联接类 conn.close(); }catch(Exception ex){ ex.printStackTrace(); } JOptionPane.showMessageDialog(null, "注册成功"); jf.setVisible(false); new GUI().init();}我想在当用户名重复时,不再执行String sql1="insert into userinformation values('"+ username+ "','"+password+"','"+phonenum+"','"+gender+"')"; int a= stmt.executeUpdate(sql1); }catch(Exception ex){ ex.printStackTrace(); } try{ //关闭数据库联接类 conn.close(); }catch(Exception ex){ ex.printStackTrace(); } JOptionPane.showMessageDialog(null, "注册成功"); jf.setVisible(false); new GUI().init();}这些语句,该怎么办
你可以在 while 循环中加一个标志变量,如 isExist,初始值为 false。如果找到重复的用户名,将该变量设为 true,然后在 while 循环结束后根据 isExist 的值来判断是否执行插入语句和注册成功的提示框。修改后的代码如下:
```
public void actionPerformed(ActionEvent e) {
String username = uField.getText().trim();
String password = pField.getText().trim();
String phonenum = tField.getText().trim();
String gender = bg.isSelected(maleBtn.getModel()) ? maleBtn.getText() : femaleBtn.getText();
String Driver = "com.mysql.cj.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/data2?characterEncoding=UTF8&useSSL=false";
Connection conn = null;
boolean isExist = false; // 标志变量
try {
Class.forName(Driver);
conn = DriverManager.getConnection(url, "root", "yxh9497");
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM userinformation");
while (rs.next()) {
if (username.equals(rs.getString("username"))) { // 修改此处
JOptionPane.showMessageDialog(null, "用户名已被注册");
isExist = true; // 修改标志变量的值
break;
}
}
if (!isExist) { // 如果用户名不存在,才执行以下语句
String sql1 = "insert into userinformation values('" + username + "','" + password + "','" + phonenum + "','" + gender + "')";
int a = stmt.executeUpdate(sql1);
JOptionPane.showMessageDialog(null, "注册成功");
jf.setVisible(false);
new GUI().init();
}
} catch (Exception ex) {
ex.printStackTrace();
}
try {
conn.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
```
改错。okBtn.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub student a =new student(); String newpassword=pField.getText(); int c = 0; if (newpassword.equals("")) { JOptionPane.showMessageDialog(null, "新密码不能为空", "warning", JOptionPane.PLAIN_MESSAGE); } else { c = studentDao.studentUpdate(newpassword); if (c == 1) { JOptionPane.showMessageDialog(null, "更新记录成功", "成功", JOptionPane.PLAIN_MESSAGE); } } } });
改错后的代码如下:
```
okBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String newpassword = pField.getText();
int c = 0;
if (newpassword.equals("")) {
JOptionPane.showMessageDialog(null, "新密码不能为空", "warning", JOptionPane.PLAIN_MESSAGE);
} else {
studentDao studentDao = new studentDao();
c = studentDao.studentUpdate(newpassword);
if (c == 1) {
JOptionPane.showMessageDialog(null, "更新记录成功", "成功", JOptionPane.PLAIN_MESSAGE);
}
}
}
});
```
修改说明:
1. 在 actionPerformed 方法中实例化了 studentDao 对象,以便调用其方法。
2. 将 `student a = new student();` 改为 `studentDao studentDao = new studentDao();`,以便正确创建 studentDao 对象。
3. 将 `String newpassword = pField.getText();` 的位置移到了 `studentDao` 实例化前面,以便在执行更新操作前获取用户输入的新密码。
阅读全文
相关推荐












