Java 06


数据类型4
while语句既是循环语句也是条件判断语句,用于位置循环次数的情况。只有在判断条件为真的情况下会一直循环。如果判断条件不成立则一次也不会循环。与此有别的是do……while语句,不论条件成立与否都会执行一次循环主体。如果循环主体只有一个语句可以将大括号省去。例子:
public class Demo{
public static void main(String[] args){
int x = 1;
do {
x++;
} while (x <= 10);
System.out.println(x);
}
}
结果为10
for语句则是已知循环次数的循环语句。格式为
for (赋值初值;判断条件;赋值增减量){
循环的语句;
}
上个例子用for语句来表示:
public class Demo{
public static void main(String[] args){
int y = 0;
for (int x = 1; x <= 10; x++) {
y = x;
}
System.out.println(y);
}
}
结果为10
循环有时候需要嵌套操作,如打印一份九九乘法表:
public class Demo{
public static void main(String[] args){
for (int x = 1; x <= 9; x++){
for (int y = 1; y <= x; y++){
System.out.print(x + "*" + y + "=" + (x * y) + "\t");
}
System.out.print("\n");
}
}
}
结果为
1*1=1
2*1=2 2*2=4
3*1=3 3*2=6 3*3=9
4*1=4 4*2=8 4*3=12 4*4=16
5*1=5 5*2=10 5*3=15 5*4=20 5*5=25
6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36
7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49
8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64
9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81
利用break语句可以终止循环语句 如让乘法表只算到3
public class Demo{
public static void main(String[] args){
for (int x = 1; x <= 9; x++){
for (int y = 1; y <= x; y++){
System.out.print(x + "*" + y + "=" + (x * y) + "\t");
}
System.out.print("\n");
if (x >= 3) {
break;
}
}
}
}
结果为
1*1=1
2*1=2 2*2=4
3*1=3 3*2=6 3*3=9
而continue语句可以强迫程序跳到循环的起点,如让乘法表不算3
public class Demo{
public static void main(String[] args){
for (int x = 1; x <= 9; x++){
if (x == 3){
continue;
}
for (int y = 1; y <= x; y++){
System.out.print(x + "*" + y + "=" + (x * y) + "\t");
}
System.out.print("\n");
}
}
}
结果为
1*1=1
2*1=2 2*2=4
4*1=4 4*2=8 4*3=12 4*4=16
5*1=5 5*2=10 5*3=15 5*4=20 5*5=25
6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36
7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49
8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64
9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
package 软件本科9班小罗; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.beans.Statement; import java.sql.*; import javax.swing.*; public class Java06 extends JFrame implements ActionListener{ JButton Button; JLabel s1; JTextField text; JTextArea ta; Connection conn; java.sql.Statement stat1; java.sql.Statement stat2; ResultSet rs1,rs2; public Java06() { super("中英文查询"); Button=new JButton("查询"); Button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { chazhao(); } }); s1=new JLabel("输入要查询的单词:"); text =new JTextField("word",20); ta = new JTextArea("工作",7,36); JPanel panel=new JPanel(); panel.add(s1); panel.add(text); panel.add(Button); JPanel panel2=new JPanel(); panel2.add(ta); Container con=getContentPane(); con.add(panel,BorderLayout.NORTH); con.add(panel2); setSize(400,200); } public void chazhao() { String i; try { Class.forName("com.mysql.jdbc.Driver");//加载连接驱动; conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/sys?serverTimezone=UTC", "root", "123456"); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block System.out.println("加载驱动失败"); } catch (SQLException e) { System.out.println("获取连接失败"); } String sql1="select 中文 from book where 英文="+"\""+text+"\""; String sql2="select 英文 from book where 中文="+"\""+text+"\""; try { stat1=conn.createStatement(); rs1=stat1.executeQuery(sql1); stat2=conn.createStatement(); rs2=stat2.executeQuery(sql2); while(rs1.next()) { System.out.println(rs1.getString("中文")); } while (rs2.next()) { System.out.println(rs2.getString("英文")); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void main(String[] args) { new Java06().setVisible(true); } @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub if(e.getSource()==Button) { String text=ta.getText(); ta.setText(text);; } } }这个代码中哪里有错呀,运行不了,还连接不成数据库
05-30
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值