public class Test{ int count =9; public void count1(){ int count =10; System.out.println("count1="+count); } public void count2(){ System.out.println("count2="+count); } public static void main(String args[]){ Test t = new Test(); t.count1(); t.count2(); } }
时间: 2023-04-08 15:03:46 浏览: 118
这是一个关于Java编程语言的问题,我可以回答。这段代码的输出结果是:count1=10,count2=9。因为在count1方法中,count被重新赋值为10,而在count2方法中,使用的是类成员变量count的值,即9。
相关问题
public class Test { static int count =0; int i=0; public void changeCount() { while (i< 5){ j++; count++;} public static void main(String[]args) { Test check1=new Test(); Test check2 = new Test(); check1.changeCount(); check2.changeCount(); System.out. println(check1.count+":"+ check2.count); }}
这段代码存在语法错误,其中 j++; count++;应该改为 i++; count++;同时,changeCount()方法缺少右括号。修正后的代码如下:
```
public class Test {
static int count = 0;
int i = 0;
public void changeCount() {
while (i < 5) {
i++;
count++;
}
}
public static void main(String[] args) {
Test check1 = new Test();
Test check2 = new Test();
check1.changeCount();
check2.changeCount();
System.out.println(check1.count + ":" + check2.count);
}
}
```
此代码的输出结果为:"10:10",因为静态变量count是所有实例共享的,而i是每个实例独有的。在changeCount()方法中,每个实例都会使自己的i增加到5,然后使count增加5,因此两个实例的count都增加了10。
/* Test3.java 查看1年级1班共有多少学生 */ public static void main(String[] args) { try (Connection connection = DBUtil.getConnection();){ Statement statement = connection.createStatement(); String sql = "SELECT c.name,COUNT(s.id) FROM class c,student s WHERE s.class_id=c.id,AND c.name='1年级1班'"; ResultSet resultSet = statement.executeQuery(sql); while (resultSet.next()){ String cname = resultSet.getString("c.name"); int count = resultSet.getInt("COUNT(s.id)"); System.out.println(""+cname+","+count); } } catch (SQLException throwables) { throwables.printStackTrace(); } }改正该代码
代码存在两个问题:
1. SQL语句中逗号的位置不正确,应该是WHERE s.class_id=c.id AND c.name='1年级1班'。
2. 在输出结果时,应该将count值转换为字符串类型,否则会被当成加法运算。
修改后的代码如下:
```
/* Test3.java 查看1年级1班共有多少学生 */
public static void main(String[] args) {
try (Connection connection = DBUtil.getConnection();) {
Statement statement = connection.createStatement();
String sql = "SELECT c.name,COUNT(s.id) FROM class c,student s WHERE s.class_id=c.id AND c.name='1年级1班'";
ResultSet resultSet = statement.executeQuery(sql);
while (resultSet.next()) {
String cname = resultSet.getString("c.name");
int count = resultSet.getInt("COUNT(s.id)");
System.out.println("" + cname + "," + String.valueOf(count));
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
```
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)