搜索
您的当前位置:首页正文

java字符串的联系

来源:爱go旅游网
public class Test {

public static void main(String[] args) { // first(); // second(); // third(); // fourth(); // fifthly(); // sixth(); // zimuduiying(); // Xunhuan(); Max(); }

/**

* \"根据以下要求,比较两个字符串的大小,并返回比较结果: 1、比较两字符串的大小。 2、忽视大小写 3、 按字典序 如果第一个字符串大于第二个字符串

* 返回大于0,如果第一个字符串等于第二个字符串 返回等于0 ,如果第一个字符串小于第二个字符串返回小于0。 4、例子

* compareToIgnoreCase(“HARD”,”hark”)的比较结 果返回小于0 。\" */

public static void first() { String a = \"HARK\"; String b = \"hello\";

if ((a.compareToIgnoreCase(b)) > 0) { System.out.println(\"1\");

} else if ((a.compareToIgnoreCase(b)) < 0) { System.out.println(\"-1\"); } else {

System.out.println(\"0\"); } }

/**

* \" 给一个二维数组inArr[ ][ ],写一个方法获取每一列的最小值,输出到一个一维数组outArr[ ]中。 如:inArr[ ][

* ]={{1,8,3},{6,5}},则输出outArr[ ] = {1,5,3} \" */

public static void second() {

int inArray[][] = { { 1, 8, 3 }, { 6, 5 } }; int outArray[] = new int[3];

int min = 0;

for (int col = 0; col < 3; col++) { int row = 0; try {

min = inArray[row][col];

} catch (ArrayIndexOutOfBoundsException e) { row++;// 如果越界row在加一行 min = inArray[row][col]; }

for (row = 0; row < outArray.length; row++) { try {

if (min > inArray[row][col]) { min = inArray[row][col]; }

} catch (Exception e) {

continue;// 当比较时如果列上没有数字就跳出来。进行下一行比较

}

}

outArray[col] = min; }

for (int i = 0; i < outArray.length; i++) { System.out.print(outArray[i] + \" \"); } }

/** * \"判断一个字符串是否是首字母大写,其余字母都是小写。 例如 输入:True 输出: true\" */

public static void third() {

System.out.println(\"请输入字母:\"); Scanner input = new Scanner(System.in); String ret = input.next();

// 把首字母拿到变成大写。

String s = String.valueOf(ret.charAt(0)).toLowerCase(); // 再把转换完的大写和原来的首字母替换掉

String res = ret.replace(ret.charAt(0), s.charAt(0));

System.out.println(res);

}

/** * \" 输入一个字符串,字符串是字母和数字的组合,编程实现输出一个新的字符串, 要求字母在前面,数字在后面,顺序不变,例如:2s7ess83a * 变成sessa2783 \" */

public static void fourth() {

System.out.println(\"请输入字符串加数字:\"); Scanner input = new Scanner(System.in); String ret = input.next();

StringBuffer oldstring = new StringBuffer(); StringBuffer newstring = new StringBuffer();

for (int i = 0; i < ret.length(); i++) {

if (ret.charAt(i) >= '0' && ret.charAt(i) <= '9') { oldstring.append(ret.charAt(i)); } else {

newstring.append(ret.charAt(i)); }

}

System.out.println(newstring.toString() + oldstring.toString());

}

/**

* \"一个字符串,获取最长的一个单词,如有多个相同长度的单词返回第一个单词。 如输入:“hello china”则返回 hello \" */

public static void fifthly() {

System.out.println(\"输入字符串:\");

Scanner input = new Scanner(System.in); String ret = input.nextLine();

String temp[] = ret.split(\" \"); int num = temp[0].length(); String newstring = temp[0];

for (int i = 0; i < temp.length; i++) {

if (num < temp[i].length()) {

newstring = temp[i];

}

}

System.out.println(newstring); }

/**

* 将一个字符里出现最多的字母截取,如,addcbbs变为acs。 */

public static void sixth() {

System.out.println(\"输入字符串:\"); Scanner input = new Scanner(System.in); String ret = input.next();

char Array[] = ret.toCharArray();

for (int i = 0; i < Array.length; i++) { System.out.print(Array[i]);

} }

/**

* 输入一数组,按照英文26个字母顺序,输出每个数字对应的字母, * 如果查不到,则输出?。如[1,3,30],输出ac?。 */

public static void zimuduiying() { System.out.println(\"一组数字\");

Scanner input = new Scanner(System.in); String ret = input.nextLine();

String inArray[] = ret.split(\" \");

for (int i = 0; i < inArray.length; i++) {

int temp = Integer.valueOf(inArray[i]) + 96; if (temp >= 97 && temp <= 122) { System.out.print((char) temp);

} else {

System.out.println(\"?\"); } } }

/**

* \"输入A-Z26个字母,输入一个大写字母后,输出该字母之后第5个字母的小写。

* 如输入A,输出f,输入Z,则输出e,超出Z是,超过1个,则返回a,超过两个,则返回b,以此类推\" */

public static void Xunhuan() {

System.out.println(\"输入大写字母\");

Scanner input = new Scanner(System.in); String ret = input.next();

char intnum = (char) (ret.charAt(0)); if (intnum >= 'V') {

int num = 'a' + 4 - ('Z' - intnum); System.out.println((char) num); } else {

char temp = (char) (ret.charAt(0) + 37); System.out.println(temp); } }

/*

* 一个维数组,求大于等于数组内的所有值的平均值的个数 例,4、5、6、7、8、9、10} 输出 5 */

public static void Max() {

int Array[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 }; float sum = 0; float sum1 = 0; int count = 0;

for (int i = 0; i < Array.length; i++) { sum += Array[i]; }

sum1 = sum / Array.length;

for (int i = 0; i < Array.length; i++) { if (sum1 <= Array[i]) { count++; } }

System.out.println(sum1); System.out.println(count);

、2、3、{1

因篇幅问题不能全部显示,请点此查看更多更全内容

Top