JAVA基础第五天 (15-6-23)

来源:互联网 发布:小黑瓶和小棕瓶 知乎 编辑:程序博客网 时间:2024/06/05 16:00

1、&和&&的区别:

&是位运算,操作数可以是boolean,也可以是数字。&&是逻辑运算,操作数只能是boolean。

2、&&是短路运算符,当第一个条件为假,第二个条件不再执行,&是非短路运算符,无论第一个条件是真是假,第二个条件都会执行。

3、运算符的优先级:括号、分号、点运算符>一元运算符>算术运算符>比较运算符>逻辑运算符>赋值运算符。

4、定义常量的优点:可以引入业务上的名称;可以做到一改全改。

5、实例:

import javax.swing.JOptionPane;
 
public class one {


public static void main(String[] args) {
 String a = JOptionPane.showInputDialog(null, "请输入一个数字");
 int number = Integer.parseInt(a);
 String str = "";
 while (number != 0) {
 str = number % 2 + " "+str; //循环内部的储存器,把字符串倒过来。。。
 number = number / 2;
 }
 System.out.println(str);


 int a = 0;
 while (a < 5) {
 System.out.print("$");
 if (a > 2) {
 break;
 }
 a++;
 }
 System.out.println("\n");


 for (int b = 0; b < 5; b++) {
 System.out.print("$");
 }
 for (int a = 1; a <= 100; a++) {
 if (a % 3 == 0) {
 System.out.println(a);
 }
 }


 for (int i = 100; i < 1000; i++) {
 int x = i / 100;
 int y = (i / 10) % 10;
 int z = i % 10;
 int w = x * x * x + y * y * y + z * z * z;
 if (i == w) {
 System.out.println(i);
 }
 }


 int i = 10;
 do {
 System.out.println(i);
 } while (i > 10);


 String m = JOptionPane.showInputDialog(null, "请输入一个数");
 int n = Integer.parseInt(m);
 boolean isShu = false;
 for (int x = 2; x < n; x++) {
 if (n % x == 0) {
 isShu = true;
 break;
 }
 }
 if (isShu == false) {
 JOptionPane.showMessageDialog(null, n + "是素数");
 } else {
 JOptionPane.showMessageDialog(null, n + "是合数");
 }
String a = JOptionPane.showInputDialog(null, "请输入一个数");
String b = JOptionPane.showInputDialog(null, "请输入另一个数");
int n = Integer.parseInt(a);
int m = Integer.parseInt(b);
int max = 0;
for (int i = 1; i <= m; i++) {
if (n % i == 0 && m % i == 0) {
max = i;
}
}
JOptionPane.showMessageDialog(null, "这两个数的最大公约数是" + max);
}
}




import java.awt.geom.FlatteningPathIterator;


public class Two {
public static void main(String[] args) {
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
System.out.print("*");
}
System.out.print("\n");
}
for(int i=1;i<=9;i++){
for(int j =1;j<=9;j++){
System.out.print(j+"*"+i+"="+i*j+"\t");
}
System.out.print("\n");
}


boolean isShu = false;


for (int i = 3; i <= 100; i++) {
boolean ispan = false;
for (int j = 2; j < i; j++) {
if (i % j == 0) {
ispan = true;
break;
}
}
if (ispan == false) {
System.out.println(i);
}
}
}
}

0 0
原创粉丝点击