華爲Java上機考試題

Java是面嚮對象語言,Java是一種可以撰寫跨平臺應用程序的面向對象的程序設計語言。下面yjbys小編爲大家提供的是Java上機考試題及答案,希望對大家有所幫助!

華爲Java上機考試題

1.程序實現目標: 輸入一個字符串,將其各個字符對應的ASCII值加5後,輸出結果。

程序要求:該字符串只包含小寫字母,若其值加5後的字符值大於'z',將其轉換成從a開始的字符。

[java] view plain?

package yond;

/**

* @author xcbeyond

* 2015-5-7下午10:37:43

* 1.程序實現目標: 輸入一個字符串,將其各個字符對應的ASCII值加5後,輸出結果。

* 程序要求:該字符串只包含小寫字母,若其值加5後的字符值大於'z',將其轉換成從a開始的字符。

*/

public class StringParseASCII {

public static void main(String[] args) {

t(stringParseASCII("abx"));

}

public static String stringParseASCII(String str){

StringBuffer result = new StringBuffer();

char tmp;

for(int i = 0;i

tmp = (char)(At(i)+5);

if(tmp > 'z') {

nd('a');

}else {

nd(tmp);

}

}

return ring();

}

}

2.程序實現目標:求一個整型數組中元素的平均值,並統計其中大於和小於此平均值的元素的個數。

程序要求:輸入:整型數組中的元素個數及各個元素。

輸出:整型數組中元素的'平均值,大於和小於此平均值的元素的個數。

[java] view plain?

package yond;

import ys;

/**

*

* @author xcbeyond

* 2015-5-7下午11:06:29

*2.程序實現目標:求一個整型數組中元素的平均值,並統計其中大於和小於此平均值的元素的個數。

*程序要求:

* 輸入:整型數組中的元素個數及各個元素。

* 輸出:整型數組中元素的平均值,大於和小於此平均值的元素的個數。

*/

public class CountAvg {

public static void main(String[] args) {

int[] array = {1,23,4,13,6};

tln(ring(array)+"的平均值:"+avg(array)+"n" +

"大於和小於平均值元素的個數分別爲:"+ring(countAvg(array)));

}

public static int[] countAvg(int[] array) {

int gt = 0; //grater than

int lt = 0; //less than

int[] result = {0,0};

int average = avg(array);

for(int i = 0;i

if(array[i]>average) {

gt++;

}else if(array[i]

lt++;

}

}

result[0] = gt;

result[1] = lt;

return result;

}

/**

* average

* @param array

* @return

*/

public static int avg(int[] array) {

int average = 0;

int sum = 0;

for(int i = 0 ;i

sum += array[i];

}

average = sum/th;

return average;

}

}

3、手動輸入一個存儲整數的數組,要求輸出數組裏面的2個最大值。

實例:

輸入:1,2,5,9,84,3,2

輸出:84,9

[java] view plain?

package yond;

import ys;

/**

* @author xcbeyond

* 2015-5-7下午11:35:13

*3、手動輸入一個存儲整數的數組,要求輸出數組裏面的2個最大值。

* 實例:

* 輸入:1,2,5,9,84,3,2

* 輸出:84,9

*/

public class FindMaxTwoNum {

public static void main(String[] args) {

int[] array = {1,2,5,9,84,3,2};

tln("數組"+ring(array)+"裏面最大的2個數爲:");

findMaxTwoNum(array);

//方法二:

//

}

public static void findMaxTwoNum(int[] array) {

int[] result = {0,0};

for(int i = 0 ;i

for(int j = 0;j

if(array[j]

int tmp;

tmp = array[j];

array[j] = array[j+1];

array[j+1] = tmp;

}

}

}

tln(array[0]+"、"+array[1]);

}

}

4、迴文數字判斷。

題目描述:

有這樣一類數字,他們順着看和倒着看是相同的數,例如:121,656,2332等,這樣的數字就稱爲:迴文數字。編寫一個函數,判斷某數字是否是迴文數字。

要求實現方法:

public String isPalindrome(String strIn);

【輸入】strIn: 整數,以字符串表示;

【返回】true: 是迴文數字;

false: 不是迴文數字;

【注意】只需要完成該函數功能算法,中間不需要有任何IO的輸入輸出

[java] view plain?

package yond;

import ner;

/**

* @author xcbeyond

* 2015-5-10下午03:46:56

*4、迴文數字判斷。

*題目描述:

* 有這樣一類數字,他們順着看和倒着看是相同的數,例如:121,656,2332等,這樣的數字就稱爲:

* 迴文數字。編寫一個函數,判斷某數字是否是迴文數字。

*/

public class IsPalindrome {

public static void main(String[] args) {

t("請輸入一個迴文數字:");

Scanner console = new Scanner();

String numStr = Line();

if(isPalindrome(numStr)) {

tln(numStr+"是迴文數字!");

}else{

tln(numStr+"不是迴文數字!");

}

}

public static boolean isPalindrome(String str){

boolean result = false;

for(int i = 0 ;i

if(At(i) == At(th()-1-i)) {

result = true;

}

}

return result;

}

}

5、要求:隨機打印50個隨機(4-10長度)的字符串,要求字符串包含的範圍是所有的英文字母包含大小寫和數字,按照編碼順序排序,每行打印4個,要求首字符對齊

[java] view plain?

package yond;

import Set;

import ;

/**

*

* @author xcbeyond

* 2015-5-10下午04:05:42

*5、要求:隨機打印50個隨機(4-10長度)的字符串,要求字符串包含的範圍是

* 所有的英文字母包含大小寫和數字,按照編碼順序排序,每行打印4個,要求首字符對齊

*/

public class RandomStr {

public static void main(String[] args) {

Set setStr = new HashSet();

for(int i = 0 ;i<50;i++) {

(randomStr(5));

}

int count = 1;

for(String i:setStr){

t(i+" ");

if(count%4 == 0) {

tln();

}

count++;

}

}

/**

* @param strLen:隨機字符串的長度

*/

public static String randomStr(int strLen) {

char[] str = new char[strLen];

int i = 0;

while(i

int f = (int)om()*3;

if(f == 0) {

str[i] = (char)('a' + om()*26);

}else if(f == 1) {

str[i] = (char)('A' + om()*26);

}else {

str[i] = (char)('0' + om()*10);

}

i++;

}

return new String(str);

}

}

6.手動輸入一個字符串,僅限小寫字母,統計並輸出每個字符在字符串中出現的次數,並輸出。提示(可以用Map)

實例:

輸入:aaabbbccc

輸出:a 3

b 3

c 3

[java] view plain?

package yond;

import Map;

import ;

/**

*

* @author xcbeyond

* 2015-5-10下午04:47:45

* 6.手動輸入一個字符串,僅限小寫字母,統計並輸出每個字符在字符串中出現的次數,並輸出。

* 提示(可以用Map)

* 實例:

* 輸入:aaabbbccc

* 輸出: a 3

* b 3

* c 3

*/

public class GetCharCount {

public static void main(String[] args) {

String str = "aaabbbrcc";

String reg = "^[a-z]*$";

if (hes(reg)) {

Map map = getCharCount(str);

for (y e : ySet()) {

tln(ey() + ": " + alue());

}

}else {

tln("輸入的字符不合法,不是小寫字母");

}

}

public static Map getCharCount(String str) {

Map map = new HashMap();

char[] arr = arArray();

for(int i = 0;i

if(!ainsKey(arr[i])) {

(arr[i], new Integer(1));

}else {

(arr[i],(arr[i])+1);

}

}

return map;

}

}

7、要求實現方法public String addTwoBigNumber(String s1,string s2)

大數相加,注意處理異常

public class Test{

public String addTwoBigNumber(String s1,string s2)

{

return "";

}

public static void main(String[] args)

{

Test test = new Test();

woBigNumber("123456789","987654321")

}

}

8、比較二維數組列最小值,組成一個新數組返回。(實現核心算法,不需要使用IO)

輸入:intArr = {{5,6,1,16},{7,3,9}}

輸出:intArrs ={1,3}

[java] view plain?

package yond;

import ys;

/**

* @author xcbeyond

* 2015-5-10下午09:09:20

*8、比較二維數組列最小值,組成一個新數組返回。(實現核心算法,不需要使用IO)

* 輸入:intArr = {{5,6,1,16},{7,3,9}}

* 輸出:intArrs ={1,3}

*/

public class GetColMin {

public static void main(String[] args) {

int[][] arr = {{5,6,1,16},{7,3,9}};

tln(ring(getColMin(arr)));

}

public static int[] getColMin(int[][] arr) {

int[] minArr = new int[th];

for(int i = 0;i

int[] tmp = arr[i];

(tmp);

minArr[i] = tmp[0];

}

return minArr;

}

}

9. 輸入:a aa,cat tiger.123dd

輸出: tiger

功能描述:鍵盤輸入一句話

輸出一句話中最常的單詞,如果最長的出現多次,返回第一個。

這句話只包含數字字母和標點。

[java] view plain?

package yond;

import yList;

import ner;

/**

*

* @author xcbeyond

* 2015-5-10下午09:45:03

*9. 輸入:a aa,cat tiger.123dd

* 輸出: tiger

* 功能描述:鍵盤輸入一句話

* 輸出一句話中最常的單詞,如果最長的出現多次,返回第一個。

* 這句話只包含數字字母和標點。

*/

public class GetLongString {

public static void main(String[] args) {

tln("請輸入一句話:");

Scanner console = new Scanner();

String str = Line();

tln("最長的單詞爲:"+getLongString(str));

}

public static String getLongString(String str) {

String[] wordStr = t("[ ,.0-9]");

int sum = 0;

ArrayList result = new ArrayList();

for(int i = 0;i

if(sum

sum = wordStr[i]th();

(wordStr[i]);

}

}

return (()-1);

}

}

10. 功能描述:將字符串中的字母全部替換成字母的下一個字母,

要是最後一位是z或Z則替換爲a或A。

輸入:aBxyZ

輸出:bCyzA

[java] view plain?

package yond;

/**

*

* @author xcbeyond

* 2015-5-10下午10:11:02

*10. 功能描述:

* 將字符串中的字母全部替換成字母的下一個字母,要是最後一位是z或Z則替換爲a或A。

* 輸入:aBxyZ

* 輸出:bCyzA

*/

public class NextString {

public static void main(String[] args) {

String str = "aBxyZ";

tln(nextString(str));

}

public static String nextString(String str) {

String result = "";

char[] arr = arArray();

for(int i = 0;i

if(arr[i] == 'z' || arr[i] == 'Z') {

arr[i] = (char)(arr[i]-25);

}else if(arr[i]<='z'&&arr[i]>='a' || arr[i]<='Z'&&arr[i]>='A') {

arr[i] = (char)(arr[i]+1);

}

}

return eOf(arr);

}

}