1.编写程序,用数组实现乘法小九九的存储和输出。【提示:采用多个一维数组。】
public class Multipation {
public static void main(String[] args) {
// TODO Auto-generated method stub
int x[][]=new int[9][9];
for(int i=0;i<9;i++){
for(int j=0;j<9;j++){
if(i>=j){
int m=i+1;
int n=j+1;
x[i][j]=m*n;
System.out.print(m+"*"+n+"="+x[i][j]);
}
}
System.out.println();
}
}
}
- 定义一个类Student,属性为学号、姓名和成绩;方法为增加记录SetRecord和得到记录GetRecord。SetRecord给出学号、姓名和成绩的赋值,GetRecord通过学号得到考生的成绩。
public class Student {
/**
* @param args
*/
private int ID;
private String name;
private float score;
public void SetRecord(int ID,String name,float score){
this.ID=ID;
this.name=name;
this.score=score;
}
public float getRecord(int ID){
if(ID==this.ID)
return this.score;
else
return -1;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Student s=new Student();
s.SetRecord(0,"alex",100);
float Sco=s.getRecord(0);
System.out.print(Sco);
}
}
3.给出上题中设计类的构造函数,要求初始化一条记录(学号、姓名、成绩)。
public class Student {
* @param args
*/
private int ID;
private String name;
private float score;
Student(int ID,String name,float score){
this.ID=0;
this.name="666";. this.score=65;
}
public void SetRecord(int ID,String name,float score){
this.ID=ID;
this.name=name;
this.score=score;
}
public float getRecord(int ID){
if(ID==this.ID)
return this.score;
else
return -1;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Student s=new Student(0,"sdfs",12);
//s.SetRecord(0,"alex",100);
float Sco=s.getRecord(0);
System.out.print(Sco);
}
}
- 编写程序,测试字符串“你好,欢迎来到Java世界”的长度,将字符串的长度转换成字符串进行输出,并对其中的“Java”四个字母进行截取,输出截取字母以及它在字符串中的位置。
public class StringTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String str="你好,欢迎来到Java世界";
int length=str.length();
char [] stringArr = str.toCharArray();
//System.out.print(stringArr);
for(int i=0;i<length;i++){
//System.out.print(stringArr[i]);
//System.out.print(stringArr[0]);
if('J'==stringArr[i]){
System.out.print(i);
}
}
}
}
- 自己设计一个坐标类,能提供以下方法如求当前坐标和其他坐标之间的距离等方法,要求所有变量为私有变量,并提供两个构造函数。
public class XYdistance {
private int x;
private int y;
XYdistance(){
setX(0);
setY(0);
}
public void setX(int x) {
this.x = x;
}
public int getX() {
return x;
}
public void setY(int y) {
this.y = y;
}
public int getY() {
return y;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
XYdistance m_1= new XYdistance();
m_1.setX(10);
m_1.setY(10);
XYdistance m_2= new XYdistance();
double distance=(m_1.getX()-m_2.getX())*(m_1.getX()-m_2.getX())+(m_1.getY()-m_2.getY())*(m_1.getY()-m_2.getY());
double result=Math.sqrt(distance);
System.out.println(result);
}
}
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(a*a*a+b*b*b+c*c*c==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()<3){
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+z==36&&(x*400+y*200+z*50==3600)){
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(c)) { // 检查是否有同一国人连续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;
} else if ((m[i] == 'b' && n[j] == 'z') || (m[i] == 'b' && n[j] == 'y')) {
continue; } else
System.out.println(m[i] + " vs " + n[j]);
}
}
}
}