日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

基础测试

發布時間:2025/3/16 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 基础测试 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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元");
?? ?}
}

轉載于:https://www.cnblogs.com/In-order-to-tomorrow/p/3646895.html

總結

以上是生活随笔為你收集整理的基础测试的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。