6.编写使用静态变量统计一个类产生的实例对象的个数的程序?
public class Static {
private static int number;
public Static(){
//number=number+1;
++number;
//System.out.println(++number);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new Static();
//m_1.Static();
System.out.println(Static.number);
}
}
7.编写程序,输出所有的水仙花数。请构造水仙花的判断方法。Static int shuixianhua(int x)
运行结果:
程序源代码:
package com.main3;
public class M_shuixianhua { public static void main(String[] args) { for (int i = 100; i < 1000; i++) { shuixianhua(i);
}
// TODO Auto-generated method stub
}
public static void shuixianhua(int x) {
int a,b,c;
a = x/100; b = x/10%10; c = x%10;
if(aaa+bbb+ccc==x)
System.out.println(x+“是水仙花数。”);
}
}
8.编写程序,终端输入需要判断的2个数据,通过调用方法最大公约数Static int maxGYS(int x)与最小公倍数Static int minGBS(int x)的方法进行判断,并将判断结果输出到终端。
程序源代码:
package zuoye2;
import java.util.Scanner; public class gz3 {
public static void main(String[] args) {
int a,b,c,d; Scanner reader=new Scanner(System.in); a=reader.nextInt(); b=reader.nextInt(); c=maxGYS(a,b);
System.out.println(“两数的最大公约数为:”+c); d=minGBS(a,b);
System.out.println(“两数的最小公倍数为:”+d); }
static int maxGYS(int x,int y) { while(y != 0) { int temp = x%y; x = y;
y = temp; }
return x; }
static int minGBS(int x,int y) {
return x*y/maxGYS(x, y);
} }
/*在循环中,只要除数不等于0,用较大数除以较小的数,将小的一个数作为下一轮循环的大数,取得的余数作为下一轮循环的较小的数,如此循环直到较小的数的值为0,返回较大的数,此数即为最大公约数,最小公倍数为两数之积除以最大公约数。 /
9.三个神秘蒙面人来访F博士。
博士询问他们年龄时,他们说:我们中年龄最小的不超过19岁。我们3人年龄总和为70岁。且我们三人年龄的乘积是所有可能情况中最大的。
请帮助F博士计算他们的年龄,从小到大排列,用逗号分开。 参考答案: 19,25,26
public class L2 {
public static void main(String[] args) {
// TODO 自动生成的方法存根
int x = 0, y = 0, z = 0, s = 0, cj;
for (int i = 1; i < 70; i++) {
for (int j = 1; j < 70; j++) {
for (int k = 1; k < 70; k++) {
if (i + j + k == 70 && (i <= 19 || j <= 19 || k <= 19)) {
cj = i * j * k; if (cj > s) {
x=i;
y=j;
z=k;
s=cj;
}
}
}
}
}
System.out.println(x+“,”+y+“,”+z);
}
}
10.使用冒泡排序(数组)
public class BubbleSort {
public static void main(String[] args) { int[] array={63,4,24,1,3,5};
BubbleSort sorter=new BubbleSort(); sorter.sort(array); } //冒泡排序
public void sort(int[] array){
for(int i=1;i<array.length;i++)
for(int j=0;j<array.length-1;j++){ if(array[j]>array[j+1]){ int temp=array[j]; array[j]=array[j+1]; array[j+1]=temp; } }
showArray(array); }
//遍历数组,并输出数组的元素。
public void showArray(int[] array){
for(int i=0;i<array.length;i++){ System.out.print(array[i]+“\t”); }
System.out.println(); }
}
11.实现会员注册,要求用户名长度不小于3,密码长度不小于6,注册时两次输入密码必
须相同 (字符串)
import java.util.Scanner; public class Register { String name;
String password; String newPassword; ///
public void nameExe(){
Scanner input=new Scanner(System.in);
System.out.println(“请输入用户名,密码和验证密码”); System.out.print(“用户名:”); name=input.next();
System.out.print(“密码:”); password=input.next();
System.out.print(“验证密码:”); newPassword=input.next();
while(name.length()<3||(password.equals(newPassword)==false)
||(password.length()<6)){ if(name.length()❤️){
System.out.println(“用户名不能小于3”); }
if((password.equals(newPassword)==false)||password.length()<6){
System.out.println(“两次输入密码不一样或密码不能小于6位”);
}
System.out.println(“\n”+“请重新输入”); System.out.print(“用户名:”); name=input.next();
System.out.print(“密码:”); password=input.next();
System.out.print(“验证密码:”); newPassword=input.next(); }
System.out.println(“注册成功!”); } }
public class Verify {
public static void main(String[] args) { Register m1=new Register(); m1.nameExe(); } }
12.某饭店招待国外考察团。按照标准,对领导是400元/人,随团职员200元/人,对司机50元/人。
考察团共36人,招待费结算为3600元,请问领导、职员、司机各几人。 答案是三个整数,用逗号分隔。 参考答案: 3,5,28
public class L3 {
public static void main(String[] args) {
// TODO 自动生成的方法存根
for(int x=1;x<36;x++){ for(int y=1;y<36;y++){
for(int z=1;z<36;z++){
if(x+y+z36&&(x400+y200+z*503600)){
System.out.println(x+“,”+y+“,”+z);
}
}
}
}
} }
12.要安排:3个A国人,3个B国人,3个C国人坐成一排。 要求不能使连续的3个人是同一个国籍。 求所有不同方案的总数?
参考答案: 283824
public class L13 { static int sum = 0; // 不同方案总个数
// 检查是否有同一国人连续3个
public static boolean check(char[] c) { int count = 1; // 初始个数
for (int i = 0; i < c.length - 1; i++) { if (c[i] == c[i + 1]) { count++;
} else {
count = 1; // 初始个数 }
if (count >= 3)
return true; }
return false;
} // 全排列
public static void allSort(char[] c, int start, int end) { if (start > end) { if (!check©) { // 检查是否有同一国人连续3个 sum++;// 不同方案总个数加1
} return;
} else {
for (int i = start; i <= end; i++) { char temp = c[i]; c[i] = c[start]; c[start] = temp;
allSort(c, start + 1, end); // 递归 temp = c[i]; c[i] = c[start]; c[start] = temp;
}
}
}
public static void main(String[] args) { char[] c = { ‘A’, ‘A’, ‘A’, ‘B’, ‘B’, ‘B’, ‘C’, ‘C’, ‘C’ }; allSort(c, 0, c.length - 1); // 全排列 System.out.println(sum);
} }
13.1949年的国庆节(10月1日)是星期六。 今年(2012)的国庆节是星期一。
那么,从建国到现在,有几次国庆节正好是星期日呢? 只要答案,不限手段!
可以用windows日历,windows计算器,Excel公式,。。。。。 当然,也可以编程!
不要求写出具体是哪些年,只要一个数目! 千万不要提交源代码!
答案不要写在这里,写在“解答.txt”中
参考答案: 9
public class L18 { public static void main(String[] args) { int count = 0;
// 1949年的国庆节(10月2日)是星期日。
// 得到这年10月2号后的的剩余天数
int total = calc(1949, 12, 31) - calc(1949, 10, 2); for (int i = 1950; i < 2012; i++) {
// calc(i)计算每年的10月1日是这一年的第天数,再用总天数对7取余==0说明是周日
total += calc(i, 10, 1); // 计算当年到10月1的总天数 if (total % 7 == 0) { System.out.println(i + “年10月1日”); count++; // 次数加1
}
}
System.out.println(“总数:” + count);
}
public static int calc(int y, int m, int d) { int[][] days = { { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }, // 平年
{ 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 } };// 闰
年 int b = 0; // 默认为平年
if ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0)
b = 1; // 是闰年 int sum = d;
for (int i = 0; i < m; i++) { sum += days[b][i];
}
return sum;
}
}
14.古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少? //这是一个菲波拉契数列问题
public class lianxi01 {
public static void main(String[] args) {
System.out.println(“第1个月的兔子对数: 1”); System.out.println(“第2个月的兔子对数: 1”); int f1 = 1, f2 = 1, f, M=24; for(int i=3; i<=M; i++) { f = f2;
f2 = f1 + f2; f1 = f;
System.out.println(“第” + i +"个月的兔子对数: "+f2); } } }
15.两个乒乓球队进行比赛,各出三人。甲队为a,b,c三人,乙队为x,y,z三人。已抽签决定比赛名单。有人向队员打听比赛的名单。a说他不和x比,c说他不和x,z比,请编程序找出三队赛手的名单。
public class lianxi18 {
static char[] m = { ‘a’, ‘b’, ‘c’ }; static char[] n = { ‘x’, ‘y’, ‘z’ };
public static void main(String[] args) {
for (int i = 0; i < m.length; i++) {
for (int j = 0; j < n.length; j++) {
if (m[i] == ‘a’ && n[j] == ‘x’) { continue;
} else if (m[i] == ‘a’ && n[j] == ‘y’) {
continue;
} else if ((m[i] == ‘c’ && n[j] == ‘x’) || (m[i] == ‘c’ && n[j] == ‘z’)) {
continue;
最后
光给面试题不给答案不是我的风格。这里面的面试题也只是凤毛麟角,还有答案的话会极大的增加文章的篇幅,减少文章的可读性
Java面试宝典2021版
最常见Java面试题解析(2021最新版)
2021企业Java面试题精选
else if ((m[i] == ‘c’ && n[j] == ‘x’) || (m[i] == ‘c’ && n[j] == ‘z’)) {
continue;
最后
光给面试题不给答案不是我的风格。这里面的面试题也只是凤毛麟角,还有答案的话会极大的增加文章的篇幅,减少文章的可读性
Java面试宝典2021版
[外链图片转存中…(img-2GNSQGoO-1714332794815)]
[外链图片转存中…(img-2MYXHmHY-1714332794816)]
最常见Java面试题解析(2021最新版)
[外链图片转存中…(img-12psr9i4-1714332794816)]
[外链图片转存中…(img-iGa1crtY-1714332794817)]
2021企业Java面试题精选
[外链图片转存中…(img-ahiWcppw-1714332794817)]
[外链图片转存中…(img-te8GyCk1-1714332794817)]