将一个数从十进制转换成二进制并输出结果。其中String str ="";str=n%2+str;可以将计算出的字符串从右往左排列
import javax.swing.JOptionPane;
public class Study {public static void main(String[] dfre) {
String s =JOptionPane.showInputDialog(null,"请输入一个整数");
int n =Integer.parseInt(s);
String str ="";
while(n != 0){
str=n%2+str;
n=n/2;
}
System.out.println(str);
判断一个整数是不是素数(只能被1和自身整除),通过定义一个boolean值,方便后面if语句的描述
import javax.swing.JOptionPane;
public class Study {public static void main(String[] dfre) {
String s = JOptionPane.showInputDialog(null, "请输入一个整数");
int n = Integer.parseInt(s);
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 + "是合数");
}
九九乘法口诀表:两个for循环
public class Study {
public static void main(String[] dfre) {
for(int i=1;i<=9;i++){
for(int j=1;j<=9;j++){
System.out.print(j+"*"+i+"="+i*j+" ");
}
System.out.println();
}
break:提前结束循环。用的较多
continue:结束本次循环,不再执行后面程序,进入下次循环。用的较少
while循环和do while循环区别:while循环是先判断再执行,可以不执行;do while循环是先执行再判断,至少执行一次
双重for循环,每执行一次外循环,会执行整个内循环