今天我们来说说两个简单的面试题,面试官往往是在简单题上,找出优秀的程序员!!!
- 打印1-100之间的素数
public class TestDemo1 {
public static void main(String[] args) {
//主函数
for (int i = 1; i <100 ; i++) {
if(isPrint(i)){
System.out.println(i);
}
}
}
public static boolean isPrint(int x) {
//打印素数函数
if(x==1){
return false;
}
long spatNum=(long)Math.sqrt(x);
for (int i = 2; i <spatNum ; i++) {
if(x%i==0){
return false;
}
}
return true;
}
}
- 打印乘法口诀表
先看看基础版
public class TestDemo2 {
public static void main(String[] args) {
for (int i = 1; i <=9 ; i++) {
for (int j = 1; j <=i ; j++) {
System.out.print(j+"*"+i+"="+j*i+" ");
}
System.out.println();
}
}
}
下面是升级版!
public class TestDemo3 {
public static void main(String[] args) {
for (int i = 1, j=1; j <=9 ; i++) {
System.out.print(i+"*"+j+"="+i*j+" ");
if(i==j){
i=0;
j++;
System.out.println();
}
}
}
}
怎么样?这样写是不是让代码更加清晰,逻辑性更强呢?