1. 设有一个字符数组,现对数组中的元音字母做一个统计。
2. 设计一个Java程序,设有一个给定的int类型数组并有一批数据,现让你用二种以上的方法对其进行升或降序排列。
3. 写一个简单程序,判断输入的一串字符串是否为身份证号码,注意身份证判别的几个条件。
4. 设计一个方法接收一个int数,然后返回它的反转字符串。如n=1234,则输出:4321
5. 设计一个方法接收一个三位的整数,如果是一个水仙花数,此方法返回一个布尔true值,否则是false值。再设计一个方法遍历100至999,对每个数都借用刚才的方法判断其是否是水仙花数,如果是打印出来,否则就是下一个。所谓水仙花数,是一个3位数,其各位的立方和等该数自身。如:153=1的三次方*5的三次方*3的三次方
6. 设计一个方法,接收一个四位整数,然后此方法判断这个4位数的9倍是该4位数的反序数。如:4位数的1089的9倍是9801。9801是1089的反序数。最后把具有这种特性的4位数全都打印出来。
7. 有以下15位身份证数据,假设被存在sfzMessage中;
sfzMessage=“370102770102413, 370102760807911, 370102660810114,…”
现在把这组数据变为18位,结果为
“ 37010219770102413A, 37010219760807911B,
370102196608101149”
为简单实现,最后一位可随机产生”10X23456789”中的任何一个来补充
8. 设计一个方法要求检查一个指定字符是否允许作为 Java 标识符中的首字符,并测试。
9. 设计一个类并提供一个方法,将整型值转换为二进制、十六进制和八进制值
10. 设计一个方法,接收三个参数分别是年,月,日,然后返加一个Calender类型的对象,再设计一个方法接收Calender类型对象,在方法内分别打印此日期是一个月的第几天,一年的第几天,一周的第几天,此天是周几,这个月的第一天是周几,是一周的第几天。
11. 使用System类的getProperty()获取Java的安装目录,java的类库路径,用户当前目录、用户名、路径分隔符5个属性
12. 使用Random类nextInt()函数产生10个1-9的随机整数。ran.nextInt(9) + 1
13. 首先定义一个StringBuffer类对象S,然后通过append()方法为对象添加26个小写字母,要求每次添加一个字母,共添加26次。
14. 定义Card类,定义CardGame类,在CardGame类中初始化生成54个(1~13)不重复的card对象,并随机分成两组A,B。
在CardGame类中生成如下方法
定义一个print()方法,打印A,B两组的牌
定义一个play()方法,实现A,B两组对应card对象的比较大小,并输出。
定义一个count()方法统计A,B两组各自的得胜的次数,并输出得胜组。
实现并测试
package zuoye3;
//1.设有一个字符数组,现对数组中的元音字母做一个统计。
public class T1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
char[] t1 = { 'y', 'a', 'n', 't', 'a', 'i' };
for(int i=0;i<t1.length;i++){
System.out.print(t1[i]);
}
System.out.println();
System.out.println("共有元音字母:" + vowelCount(t1));
}
public static int vowelCount(char[] ch) {
int count = 0;
for (int i = 0; i < ch.length; i++) {
if (ch[i] == 'a' || ch[i] == 'e' || ch[i] == 'i' || ch[i] == 'o'
|| ch[i] == 'u' || ch[i] == 'A' || ch[i] == 'E'
|| ch[i] == 'I' || ch[i] == 'O' || ch[i] == 'U') {
count++;
}
}
return count;
}
}
package zuoye3;
import java.util.Arrays;
//2.设计一个Java程序,设有一个给定的int类型数组并有一批数据,现让你用二种以上的方法对其进行升或降序排列。
public class T2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] ary={1,3,6,3,2,3,4,5,6,6,7,6,5};
//ary = selectionSort(ary); //选择排序
ary = bubleSort(ary); //冒泡排序
//输出
String s = Arrays.toString(ary);
System.out.print(s);
}
public static int[] selectionSort(int[] ary){
for(int i=0;i<ary.length-1;i++){
for(int j = i+1;j<ary.length;j++){
if(ary[i]>ary[j]){
int temp = ary[i];
ary[i] = ary[j];
ary[j] = temp;
}
}
}
return ary;
}
//冒泡排序
public static int[] bubleSort(int[] ary){
for(int i=0;i<ary.length-1;i++){
for(int j=0;j<ary.length-i-1;j++){
if(ary[j]>ary[j+1]){
int temp = ary[j];
ary[j]=ary[j+1];
ary[j+1]=temp;
}
}
}
return ary;
}
}
package zuoye3;
import java.util.Scanner;
/**
* 身份证第18位算法
* 将前面身份证号码17位数分别乘以不同的系数
* 7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2
* 3 4 0 5 2419800101001
* 从第一位到第十七位的系数分别为
* 将这17位数字和系数相乘的结果相加
* 用加出来和除以11,看余数
* 余数对应
* 0 1 2 3 4 5 6 7 8 9 10
* 1 0 X 9 8 7 6 5 4 3 2
*/
public class T3 {
private static Scanner sc;
public static void main(String[] args) {
// TODO Auto-generated method stub
// 3. 写一个简单程序,判断输入的一串字符串是否为身份证号码,
// 注意身份证判别的几个条件。
sc = new Scanner(System.in);
String id;
do{
System.out.print("请输入正确的身份证号码:");
id =sc.nextLine();}while(!verify(id));
System.out.print(verify(id));
/**
* 用do-while循环输入直到正确
*/
}
public static boolean verify(String id){
if(id.length()!=18) return false;
/**
* * 将前面身份证号码17位数分别乘以不同的系数
* 7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2
*/
int [] w={7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2};//加权,W权
// 将这17位数字和系数相乘的结果相加
//用加出来和除以11,看余数
int sum=0;
for(int i=0;i<w.length;i++){
sum+=(id.charAt(i)-'0')*w[i];
}
int c = sum%11;
/**
* * 余数对应
* 0 1 2 3 4 5 6 7 8 9 10
* 1 0 X 9 8 7 6 5 4 3 2
*/
char[] ch=
{'1','0','X','9','8','7','6','5','4','3','2'};
char code = ch[c];
char last = id.charAt(17);
last = last=='x'?'X':last;//大小写Xx转换
return id.charAt(17)==code;
}
}
package zuoye3;
import java.util.Scanner;
public class T4 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个整数:");
int t4 = sc.nextInt();
System.out.println(t4+"的反转字符串为:"+fanzhuan(t4));
}
public static String fanzhuan(int i) {
String str = "";
while (i != 0) {
int a = i % 10; //取余
str = str + a;
i = i / 10; //取整
}
return str;
}
}
package zuoye3;
public class T5 {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("水仙花数为:");
traversal();
}
public static boolean isFlowerNum(int num) {
boolean flag = false;
int i = num / 100;
int j = num / 10 % 10;
int k = num % 10;
if (num == i * i * i + j * j * j + k * k * k) {
flag = true;
}
return flag;
}
public static void traversal() {
for (int i = 100; i < 999; i++) {
if (isFlowerNum(i)) {
System.out.println(i);
}
}
}
}
package zuoye3;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class T6 {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("请输入一个四位数:");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try {
int num = Integer.parseInt(br.readLine());
if (reversal(num)) {
System.out.println(num + " 的9倍是 " + num + " 反序数");
} else {
System.out.println(num + " 的9倍不是 " + num + " 反序数");
}
System.out.println("这种数有:");
traversal();
} catch (NumberFormatException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static boolean reversal(int num) {
boolean flag = false;
String str = "";
int i = num;
while (i != 0) {
int a = i % 10;
str = str + a;
i = i / 10;
}
if (num == Integer.parseInt(str) / 9) {
flag = true;
}
return flag;
}
// 遍历
public static void traversal() {
for (int i = 1000; i < 9999; i++) {
if (reversal(i)) {
System.out.println(i);
}
}
}
}
package zuoye3;
import java.util.Random;
public class T7 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int [] s1={3,7,0,1,0,2,7,7,0,1,0,2,4,1,3};
//int [] s2={3,7,0,1,0,2,7,6,0,8,0,7,9,1,1};
//int [] s3={3,7,0,1,0,2,6,6,0,8,1,0,1,1,4};
int [] s4 = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
for(int i=0;i<6;i++){
s4[i]=s1[i];
}
s4[6]=1;
s4[7]=9;
for(int i=8;i<17;i++){
s4[i]=s1[i-2];
}
s4[17]=random();
for(int i=0;i<17;i++){
System.out.print(s4[i]);
}
}
public static int random(){
Random rand =new Random(25);
int i=rand.nextInt(11);
return i;
}
}
package zuoye3;
public class T8 {
public static void main(String[] args) {
// TODO Auto-generated method stub
char ch = '+';
validate(ch);
}
public static void validate(char ch) {
if (Character.isJavaIdentifierStart(ch)) {
System.out.println(ch + " 允许作为 Java 标识符中的首字符");
} else {
System.out.println(ch + " 不允许作为 Java 标识符中的首字符");
}
}
}
package zuoye3;
import java.util.Scanner;
public class T9 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个整数:");
int num = sc.nextInt();
System.out.println("十进制:"+num);
trans(num);
}
public static void trans(int num){
String num2 = Integer.toBinaryString(num);
System.out.println("二进制位:" + num2);
String num8 = Integer.toOctalString(num);
System.out.println("八进制位:" + num8);
String num16 = Integer.toHexString(num);
System.out.println("十六进制位:" + num16);
}
}
package zuoye3;
import java.util.Calendar;
public class T10 {
public static void main(String[] args) {
// TODO Auto-generated method stub
display(input(2013, 9, 20));
}
public static Calendar input(int year, int month, int date) {
Calendar calendar = Calendar.getInstance(); //单例模式就是一个类有且只有一个实例
calendar.set(year, month, date);
return calendar;
}
public static void display(Calendar calendar) {
int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
System.out.println("此日期是:一个月的第 " + dayOfMonth + "天");
int dayOfYear = calendar.get(Calendar.DAY_OF_YEAR);
System.out.println("此日期是:一年中的第 " + dayOfYear + "天");
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
System.out.println("此日期是:一周中的第 " + dayOfWeek + "天");
}
}
package zuoye3;
public class T11 {
public static void main(String[] args) {
// TODO Auto-generated method stub
String java_home = System.getProperty("java.home", "JAVA安装目录:");
System.out.println("JAVA安装目录:" + java_home);
String java_classpath = System.getProperty("java.class.path",
"JAVA的类库路径:");
System.out.println("JAVA的类库路径:" + java_classpath);
String user_dir = System.getProperty("user.dir", "用户当前目录:");
System.out.println("用户当前目录:" + user_dir);
String user_name = System.getProperty("user.name", "用户名:");
System.out.println("用户名:" + user_name);
String path_separator = System.getProperty("path.separator", "路径分隔符:");
System.out.println("路径分隔符:" + path_separator);
}
}
package zuoye3;
public class T13 {
public static void main(String[] args) {
// TODO Auto-generated method stub
StringBuffer sb = new StringBuffer();
for(int i = 97;i<=122;i++){
char ch = (char) i;
sb.append(ch);
}
System.out.println(sb);
}
}
T14
package zuoye3;
public class Card {
private Integer number;
private Integer shape;
private String king;
public Card(String king, Integer shape, Integer number) {
this.king = king;
this.shape = shape;
this.number = number;
}
public String getKing() {
return king;
}
public void setKing(String king) {
this.king = king;
}
public Integer getShape() {
return shape;
}
public void setShape(Integer shape) {
this.shape = shape;
}
public Integer getNumber() {
return number;
}
public void setNumber(Integer number) {
this.number = number;
}
}
package zuoye3;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Random;
public class Cardgame {
private ArrayList<Card> al1 = new ArrayList<Card>();
private ArrayList<Card> al2 = new ArrayList<Card>();
private Card card;
private Map<Integer, String> m ;
private Integer count1 = 0;
private Integer count2 = 0;
public Cardgame() {
this.init();
}
@SuppressWarnings("unused")
private void init(){
HashSet<Integer> set = new HashSet<Integer>();
int[] reult2 = randomArray(1,54,54);
for (int j : reult2) {
if(j == 54){
card= new Card("king",0,0);
addCard(j,card);
continue;
}
if(j == 53){
card= new Card("queen",0,0);
addCard(j,card);
continue;
}
int sp = j%4;
int num = (int) Math.ceil(j/4);
card= new Card("0",sp,num);
addCard(j,card);
}
}
public void print(){
m = new HashMap<Integer, String>();
m.put(0,"黑桃");
m.put(1,"红桃");
m.put(2,"方块");
m.put(3,"梅花");
System.out.println("A:");
Iterator<Card> itr = al1.iterator();
while(itr.hasNext()){
Card c = itr.next();
if (c.getKing().length()>1){
System.out.print(c.getKing()+", ");
}else{
System.out.print(""+m.get(c.getShape())+c.getNumber()+", ");
}
}
System.out.println();
System.out.println("B:");
Iterator<Card> itr2 = al2.iterator();
while(itr2.hasNext()){
Card c = itr2.next();
if (c.getKing().length()>1){
System.out.print(c.getKing()+", ");
}else{
System.out.print(""+m.get(c.getShape())+c.getNumber()+", ");
}
}
}
public static int[] randomArray(int min,int max,int n){
int len = max-min+1;
if(max < min || n > len){
return null;
}
int[] source = new int[len];
for (int i = min; i < min+len; i++){
source[i-min] = i;
}
int[] result = new int[n];
Random rd = new Random();
int index = 0;
for (int i = 0; i < result.length; i++) {
index = Math.abs(rd.nextInt() % len--);
result[i] = source[index];
source[index] = source[len];
}
return result;
}
public void play(){
for (int i =0;i<27;i++){
Card AS1 = al1.get(i);
Card AS2 = al2.get(i);
if("king".equals(AS1.getKing())){
System.out.println("A赢");
count1++;
continue;
}
if("king".equals(AS2.getKing())){
System.out.println("B赢");
count2++;
continue;
}
if("queen".equals(AS1.getKing())){
System.out.println("A赢");
count1++;
continue;
}
if("queen".equals(AS1.getKing())){
System.out.println("B赢");
count2++;
continue;
}
if(AS1.getNumber()>AS2.getNumber()){
System.out.println("A赢");
count1++;
continue;
}
if(AS1.getNumber()<AS2.getNumber()){
System.out.println("B赢");
count2++;
continue;
}
}
}
private void addCard(Integer j,Card card){
if(j%2 == 0){
al1.add(card);
}else if(j%2 == 1){
al2.add(card);
}
}
public Integer count(){
System.out.println("A获胜"+count1+"次");
System.out.println("B获胜"+count2+"次");
System.out.println((count1-count2>0?"A":"B")+"胜利");
return 0;
}
}
package zuoye3;
public class T14 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Cardgame AS = new Cardgame();
AS.print();
AS.play();
AS.count();
}
}