JAVA口试经典的一些编程题(含代码 更新中.)
1, 子线程循环10次,接着主线程循环100,接着又回到子线程循环10次,接着再回到主线程又循环100,如此循环50次
代码实现如下:
public class ThreadCommunicationDemo {
// main函数 是一个主线程
public static void main(String[] args) {
final Business business = new Business();
// 开启一个 子线程
new Thread(new Runnable() {
public void run() {
for (int i = 1; i <= 50; i++) {
business.sub(i);
}
}
}) {
}.start();
//
for (int i = 1; i <= 50; i++) {
business.main(i);
}
}
}
//
class Business {
private boolean isShouldSub = true;
// 子线程
public synchronized void sub(int i) {
// 如果不是子线程要执行 则处于等待状态
while (!isShouldSub) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
// 否则执行子线程循环
for (int j = 1; j <= 10; j++) {
System.out.println("子线程:" + j + ",循环次数:" + i);
}
// 结束之后 将boolean 改为flase
isShouldSub = false;
// 同时 要唤醒线程等待状态
this.notify();
}
// 主线程
public synchronized void main(int i) {
// 如果是子线程正在执行 则主线程处于等待状态
while (isShouldSub) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
// 否则 执行主线程循环
for (int j = 1; j <= 100; j++) {
System.out.println("主线程:" + j + ",循环次数:" + i);
}
// 结束之后 将boolean 改为 true
isShouldSub = true;
// 同时 要唤醒线程等待状态
this.notify();
}
}
2,验证一个字符 是否在一个数组中包含
代码实现如下:
/**
* 验证一个字符 是否在一个数组中包含
* @author
*
*/
public class Check1 {
public static int check(String[] strArr,String str) throws IllegalAccessException{
int location = -1;
int len = strArr.length;
if (strArr == null) {
throw new IllegalAccessException();
}
for (int i = 0; i < len; i++) {
if (str.equals(strArr[i])) {
location = i;
break;
}
}
return location;
}
//
public static void main(String[] args) {
String strArr[] = {"a","b","c","d","e"};
String str = "b";
int location ;
try {
location = check(strArr, str);
if (location == -1) {
System.out.println("查找的字符:" + str+ "没有在数组中");
}else {
System.out.println("字符在数组中出现的位置是:"+location);
}
} catch (IllegalAccessException e) {
System.out.println("请不要输入null数组.");
e.printStackTrace();
}
}
}
3,java分解一个整数
代码实现如下:
import java.util.Scanner;
/**
* 分解一个数字
*
* 请输入一个整数数字:
3435
您输入的数字是:3435
3 * 5 * 229
* @author
*
*/
public class Decomposition {
private static int k = 2;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入一个整数数字:");
int number = scanner.nextInt();
System.out.println("您输入的数字是:" + number);
fenJie(number);
}
public static void fenJie(int number){
if (k == number) {
System.out.println(k);
return;
}else if(number % k == 0){
System.out.print(k+ " * ");
fenJie(number / k);
}else {
k++;
fenJie(number);
}
}
}
4,将输入的一个字符串 拆开 分类
代码实现如下:
import java.util.Scanner;
/**
* 将输入的一个字符串 拆开 分类
*
* 请随便输入一个字符串
sdf34#$%%多发点4353
number : 6
space : 0
letter : 3
other : 7
* @author
*
*/
public class Fenlei {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请随便输入一个字符串");
String str = scanner.nextLine();
char [] c = str.toCharArray();
int number = 0;//数字
int space = 0;//空
int letter = 0;//字母
int other = 0;
for (int i = 0; i < c.length; i++) {
if (((c[i] > 'a') && (c[i] < 'Z')) || ((c[i] > 'A') && (c[i] < 'z'))) {
letter ++;
}
else if ((c[i] > '0')&&(c[i] < '9')){
number ++;
}
else if (String.valueOf(c[i]).equals("")) {
space ++;
}
else{
other ++;
}
}
System.out.println("number : " + number);
System.out.println("space : " + space);
System.out.println("letter : " + letter);
System.out.println("other : " + other);
}
}
5,计算100 到 200中 共有多少个素数
代码实现如下:
/**
* 100 到 200中 共有多少个素数
* @author Administrator
*
*/
public class SushuTest {
public static void main(String[] args) {
int count = 1;
for (int i = 100; i <= 200; i++) {
if (isSuShu(i)) {
count++;
}
}
System.out.println("素数一共是:" + count + "个");
}
public static boolean isSuShu(int i){
boolean isSuShu = true;
for(int j = 2; j < Math.sqrt(i); j++){
if (i % j == 0) {
isSuShu = false;
}
}
return isSuShu;
}
}
6,一个整数,大于 0,不用循环和本地变量,按照 n,2n,4n,8n 的顺序递增, 当
值大于 5000 时,把值按照指定顺序输出来。
例:n=1237
则输出为:
1237,
2474,
4948,
9896,
9896,
4948,
2474,
1237,
代码实现如下:
public class TestPrint1 {
public static void main(String[] args) {
printOne(1237);
}
private static void printOne(int n) {
System.out.println(n);
if (n < 5000) {
printOne(n * 2);
}else {
System.out.println(n);
printTwo(n/2);
}
}
private static void printTwo(int n){
if (n >= 1237) {
System.out.println(n);
printTwo(n/2);
}
}
}
7,java打印
*
***
*****
*******
*****
***
*
代码实现如下:
public class Test1 {
public static void main(String[] args) {
for (int i=1; i<=13; i+=2){
for(int j=1; j<=i && i+j<= 14; j++){System.out.print("*");}
System.out.println(); // 换行
}
}
}
8,用迭代的方法,判断是不是一个回文字符串,如”abdba”
代码实现如下:
public class Test2 {
/**
* @param args
* 用迭代的方法,判断是不是一个回文字符串,如”abdba”
* @author BZ70000910
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Test2 t = new Test2();
System.out.println(t.idAbcbA("abcba"));
}
public boolean idAbcbA(String str){
while(str.length() > 1){
int strLen = str.length();
char first = str.charAt(0);
char last = str.charAt(strLen - 1);
if (first == last) {
String str2 = str.substring(1,strLen-1);
System.out.println(str2);
idAbcbA(str2);
}else {
return false;
}
//不加break,就会进入死循环,因为退出循环也是一层一层的。
break;
}
return true;
}
}
9,java来获取一个list集合里面 最大值、中间值、最小值格式多少?
代码实现如下:
import java.util.ArrayList;
import java.util.Arrays;
public class Test5 {
private ArrayList<Integer> arrayList;
public Test5(ArrayList<Integer> arrayList) {
this.arrayList = arrayList;
}
/**
* @param args
*/
public static void main(String[] args) {
ArrayList<Integer> arrayList = new ArrayList<Integer>();
Test5 t = new Test5(arrayList);
t.addElement(4);
t.addElement(2);
t.addElement(1);
t.addElement(3);
t.addElement(7);
t.addElement(6);
t.addElement(5);
System.out.println("最大值是:"+t.maxi());
System.out.println("中间值是:"+t.middlei());
System.out.println("最小值是:"+t.mini());
}
public int length(){
return arrayList.size();
}
public void addElement(int element){
arrayList.add(element);
}
public int maxi(){
int max = arrayList.get(0);
for(int i = 1; i < arrayList.size();i++){
if (max < arrayList.get(i)) {
max = arrayList.get(i);
}
}
return max;
}
public int mini(){
int min = arrayList.get(0);
for (int i = 1; i < arrayList.size(); i++) {
if (min > arrayList.get(i)) {
min = arrayList.get(i);
}
}
return min;
}
public int middlei(){
int middle = 0;
int size = length();
Integer[] i = arrayList.toArray(new Integer[size]);
Arrays.sort(i);
return i[size/2];
}
}