public class Test1 {
/**
* 第1题:从键盘接受一个数字,列出该数字的中文表示格式,
例如:键盘输入123,打印出一二三;键盘输入3103,打印出三一零三。
*
*
@param args
*/
public static void main(String[]
args){
// 引用变量
BufferedReader br = null;
BufferedWriter bw = null;
try {
//
创建输入流
br =
new BufferedReader(new InputStreamReader(System.in));
// 创建输出流
bw = new BufferedWriter(new
OutputStreamWriter(System.out));
//
建立数组,0-9的角标对应数组内容
char[] chs = { ‘零‘, ‘一‘, ‘二‘, ‘三‘, ‘四‘, ‘五‘, ‘六‘, ‘七‘, ‘八‘, ‘九‘
};
String line =
null;
while ((line =
br.readLine()) != null) {
//
将读取的字符串转换成字符数组
char[] arr = line.toCharArray();
for (int i = 0; i
< arr.length; i++) {
//
将对应‘1’字符的AScII码值转换成字符串,通过parseint方法转换成int类型数值
int index = Integer.parseInt(new String(
new char[] { arr[i]
}));
bw.write(chs[index]);
bw.flush();
}
}
} catch (IOException e)
{
System.out.println(e.toString());
} finally {
if
(br != null)
try {
br.close();
} catch (IOException e1) {
throw new RuntimeException("输入流关闭失败");
}
if (bw != null)
try {
bw.close();
} catch (IOException e2) {
throw new RuntimeException("输出流关闭失败");
}
}
}
}
public class Test2 {
/**
* 第2题:编程计算3乘8等于几,什么方法效率更高?
*
* @param args
*/
public static void main(String[] args)
{
//
位运算符:3<<3相当于:3*(2*2*2);
int i = 3 << 3;
System.out.println(i);
}
}
public class
Test3 {
/**
* 第3题:求斐波那契数列第n项,n<30,斐波那契数列前10项为
1,1,2,3,5,8,13,21,34,55
*
* @param args
*/
public static void main(String[] args)
{
int n, a = 1, b = 1, c, d = 5, e =
0;
for (n = 1; n < 30; n++)
{
// 前两个数是1 ,打印1.
if (n <= 2) {
System.out.print(1 +
"\t");
} else
{
c = a + b;
System.out.print(c + "\t");
//
a+b=c;把b的值赋给a
a = b;
//
把c的值赋给b,运行上面的a+b=c
b = c;
// 每5个数换行一次
if (n % 5 == 0)
{
//
记录一行的个数
e++;
System.out.println("数的个数=" + d * e + "\t");
}
}
}
}
}
public class Test4 {
/**
* 第4题:定义一个二维int数组,编写代码获取最小元素。
*
* @param args
*/
public static void main(String[] args)
{
int[][] arr = { { 1, 2, -1 }, { 18,
-2 }, { 3 } };
getMin(arr);
}
static int
getMin(int[][] arr) {
//
初始化最小值
int Min =
arr[0][0];
//
遍历有几个一位数组
for (int i = 0; i <
arr.length; i++) {
// 遍历每个一维数组的长度
for (int j = 0; j <
arr[i].length; j++) {
//
遍历的过程中用Min记录住最小值
if (Min > arr[i][j])
Min = arr[i][j];
}
}
System.out.println(Min);
return Min;
}
}
public
class Test5 {
/**
* 第5题:编程列出一个字符串的全字符组合情况,原始字符串中没有重复字符, 例如:
原始字符串是"abc",打印得到下列所有组合情况: "a"
* "b""c" "ab" "ac" "ba" "bc"
"ca" "cb" "abc" "acb" "bac" "bca" "cab" "cba"
*
*
@param args
*/
public static String str =
"abc";
public static void main(String[] args)
{
show(0, new
String());
}
//
递归
public static void show(int current_recur,
String temp) {
if (current_recur <
str.length()) {
for
(int i = 0; i < str.length(); i++) {
if (!(temp.contains(str.substring(i,
i + 1)))) {
System.out.print(temp +
str.substring(i, i + 1) + " ");
show(current_recur + 1,
new String(temp + str.substring(i, i +
1)));
}
}
}
}
}
public class Test6 {
/**
* 第6题:编写程序接收键盘输入的5个数,装入一个数组,并找出其最大数和最小数。
*
* @param args
*/
public static void main(String[] args)
{
// 获取键盘录入对象。
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
// 定义一个数组
int[] arr = new int[5];
//
初始化数组中的一个角标
int max = 0, min =
0;
// 遍历数组
for (int i = 0; i < arr.length; i++) {
try {
System.out.print("请输入第" + (i + 1) + "个数:");
//
通过parseInt,字符串中的字符必须都是指定基数的数字
arr[i] =
Integer.parseInt(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
//
获取最值
for (int i = 0; i <
arr.length; i++) {
if (arr[i] > arr[max])
max = i;
if (arr[i] <
arr[min])
min = i;
}
System.out.println("Max=" +
arr[max]);
System.out.println("Min=" +
arr[min]);
}
}
public class Test7 {
/**
* 第7题:声明类Student,包含3个成员变量:name、age、score,
要求可以通过 new Student("张三", 22, 95)
*
的方式创建对象,并可以通过set和get方法访问成员变量
*
* @param args
*/
public static void main(String[] args)
{
Student stu = new Student("张三", 22,
95);
System.out.println(stu.getName() +
"\t" + stu.getAge() + "\t"
+ stu.getScore());
}
}
class Student {
//
封装对象
private String name;
private int age;
private int
score;
//
定义带参数的构造函数
public Student(String name, int age, int
score) {
this.name =
name;
this.age = age;
this.score = score;
}
//
定义方法
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int
getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getScore() {
return
score;
}
public void setScore(int
score) {
this.score =
score;
}
}
public class Test8 {
/**
*
第8题:在打印语句中如何打印这3个x变量? class A { int
x = 1; class B { int x = 2; void
* func() { int x = 3;
System.out.println( ? ); } } }
*
* @param args
*/
public static void main(String[] args)
{
A.B b = new A().new
B();
b.func();
}
}
class A {
int x = 1;
class B {
int x =
2;
void func() {
int x = 3;
//
本类功能内部使用了本类对象,都用类名.this表示
System.out.println(A.this.x);
System.out.println(this.x);
System.out.println(x);
}
}
}
public class
Test9 {
/**
*
第9题:写一个正则表达式,可以匹配尾号5连的手机号。
规则:
*
第1位是1,第二位可以是数字3458其中之一,后面4位任意数字,最后5位为任意相同的数字。
例如:18601088888、13912366666
*
* @param args
*/
public static void main(String[] args)
{
// 定义电话号码规则
String regex =
"[1][3-5[8]][0-9]{4}(\\d)\\1{4}";
// 使用户能够从 System.in 中读取一个数
Scanner sc = new Scanner(System.in);
boolean flag = true;
System.out.println("输入电话号码");
while (flag) {
String str = sc.next();
if ((str.toCharArray().length) == 11) {
if
(str.matches(regex)) {
flag = false;
System.out.println("匹配成功");
} else {
System.out.println("匹配错误_重新输入");
}
} else {
System.out.println("电话号码_位数错误_重新输入");
}
}
}
}
public class Test10 {
/**
* 第10题:小明的妈妈每天会给他20元零花钱。平日里,小明先花掉一半,再把一半存起来。每到周日,
*
小明拿到钱后会把所有零花钱花掉一半。请编程计算,从周一开始,小明需要多少天才能存够100元?
*
*
@param args
*/
public static void main(String[] args)
{
// 从第一天开始开始存钱
int day = 1;
int money = 0;
while (money <
100) {
if (day % 7
!= 0)
money += 10;
else if (day % 7 == 0)
money = (money + 20) /
2;
//
当存的钱大于或者等于100时,跳出循环
if (money >= 100)
break;
day++;
}
System.out.println("小明需要" + day
+ "天才能存够100元");
}
}
原文:http://www.cnblogs.com/In-order-to-tomorrow/p/3646895.html