您的位置:首页 > 编程语言 > Java开发

JAVA基础第四天

2015-06-24 22:31 495 查看
1、字符串的相等的判断不能用“==”,比较两个字符串是否相等,要用“equals”

2、!表示非,将true变成假,讲false变成真。

3、位运算符:是先将操作数转化成二进制码,在运算。

eg:2<<1-->左位移运算符,相当于乘2操作。

3>>1-->右位移运算符,相当于除2操作。

4&6--> 按位于,对位的二进制码,如果都是1,结果为1,有一个是0,结果都是0.

5|6--> 按位或,对位的二进制码,只要有一个是1,结果都是1,只要两个都是0,结果才是0.

9^11-->异或,对位的二进制码,只有不相等,结果是1,如果相等,结果是0.

?--> 三目运算符,?前面是一个条件,当条件为真,将左边的值赋给x,当条件为假,将右边的值赋给x。

++--> 后置加加,是先赋值,再做加运算;前置加加是先做运算,再赋值。

4 实例:

import javax.swing.JOptionPane;

public class three {

public static void main(String[] args) {

String userName = JOptionPane.showInputDialog(null, "请输入您的用户名");

String pwd = JOptionPane.showInputDialog(null, "请输入您的密码");

// 比较两个字符串是否相等,采用equals,不要用==

if (userName.equals("天王盖地虎") && pwd.equals("宝塔镇河妖")) {

JOptionPane.showMessageDialog(null, "登陆成功");

} else if (!userName.equals("天王盖地虎")) {

JOptionPane.showMessageDialog(null, "用户名错误,登陆失败");

} else if (!userName.equals("宝塔镇河妖")) {

JOptionPane.showMessageDialog(null, "密码错误,登陆失败");

}

System.out.println(!(3>5)); //!表示非。将true变成假,将false变成真

String userName = JOptionPane.showInputDialog(null, "请输入您的用户名");

if (userName.equals("123")) {

String pwd = JOptionPane.showInputDialog(null, "请输入您的密码");

if (pwd.equals("321")) {

JOptionPane.showMessageDialog(null, "登陆成功");

} else {

JOptionPane.showMessageDialog(null, "密码错误,请重新输入");

}

} else {

JOptionPane.showMessageDialog(null, "用户名错误,请重新输入");

}

}

}

import javax.swing.JOptionPane;

public class five {

public static void main(String[] args) {

String a = JOptionPane.showInputDialog(null, "请输入一个数字");

String b = JOptionPane.showInputDialog(null, "请输入另外一个数字");

String c = JOptionPane.showInputDialog(null, "1、加\n2、减\n3、乘\n4、除");

int x = Integer.parseInt(a);

int y = Integer.parseInt(b);

int z = Integer.parseInt(c);

switch (z) {

case 1: //当z的值等于1的

JOptionPane.showMessageDialog(null, x + "+" + y + "=" + (x + y));

break;

case 2:

JOptionPane.showMessageDialog(null, x + "-" + y + "=" + (x - y));

break;

case 3:

JOptionPane.showMessageDialog(null, x + "*" + y + "=" + (x * y));

break;

case 4:

JOptionPane.showMessageDialog(null, x + "/" + y + "="

+ (x * 1.0 / y));

break;

default://当所有条件都不满足,则执行default,相当于if语句中的else

JOptionPane.showMessageDialog(null, "请输入1-4之间的数字");

break;

}

}

}

import javax.swing.JOptionPane;

public class six {

public static void main(String[] args) {

int a = 1;

while (a <= 100) {

System.out.print("*"); // print和println的区别:区别在于println会换行,而print则不会。

if (a % 10 == 0) {

System.out.println(); // 可以在print后面的括号中用"\n"来实现换行,也可以用println()来实现换行。

}

a++;

}

}

}

mport javax.swing.JOptionPane;

public class six {

public static void main(String[] args) {

int a = 1;

double sum = 0;

while (a <= 5) {

String x = JOptionPane.showInputDialog(null, "请输入第" + a + "个学生的成绩");

double y = Double.parseDouble(x);

sum = sum + y;

a++;

}

JOptionPane.showMessageDialog(null, "平均分:" + sum / 5);

int a = 1;

int b = 0;

while (a <= 100) {

b = a + b;

a++;

}

System.out.println(b);

}

}

mport javax.swing.JOptionPane;

public class six {

public static void main(String[] args) {

int a = 1;

int b = 0;

while (a < 100) {

b+=a; //a += 相当于a=a+1

a+=2;

}

System.out.println(b);

}

}

mport javax.swing.JOptionPane;

public class six {

public static void main(String[] args) {

nt a = 1;

int b = 0;

while (a <= 50) {

if (a % 3 == 0 || a % 10 == 3 || a / 10 == 3) {

b++;

System.out.println(a + "敲桌子" + b);

}

a++;

}

}

}

mport javax.swing.JOptionPane;

public class six {

public static void main(String[] args) {

int a = 1;

double max = 0;

while (a <= 5) {

String x = JOptionPane.showInputDialog(null, "请输入第" + a + "个数");

double sum = Double.parseDouble(x);

if (a == 1) {

max = sum;

}

if (max < sum) {

max = sum;

}

a++;

}

JOptionPane.showMessageDialog(null, "最大的数是:"+max);

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: