java基础问题,那位高手帮个忙!!!!在线等~~!!

2024-12-15 19:50:44
推荐回答(2个)
回答1:

1按字母顺序排序输出
import java.util.Iterator;
import java.util.Scanner;
import java.util.TreeSet;
public class TsetOrder {
public static void main(String[] args) {
System.out.println("请输入数组长度:");
Scanner s = new Scanner(System.in);
int n = s.nextInt();
String[] str = new String[n];
for (int i = 0; i < str.length; i++) {
System.out.println("请输入第" + (i + 1) + "字符串元素:");
Scanner sc = new Scanner(System.in);
str[i] = sc.nextLine(); // 给数组添加元素
}
Arrays.sort(str); //进行升序排序
System.out.println(Arrays.toString(str));//打印该数组
}
}
2判断输出次数
import java.util.Arrays;
import java.util.Scanner;

public class JudeCount {
public static void main(String[] args) {
System.out.println("请输入你要判断的字符串:");
Scanner s = new Scanner(System.in);
String str = s.nextLine();
char[] ch = str.toCharArray();
Arrays.sort(ch); //对数组排序
char max = 'a'; //记录出现次数最多元素
int maxcount = 0; //记录最大出现次数
int count = 1; //中间传值参数 判断当前元素出现次数
for (int i = 0; i < ch.length - 1; i++) { //进行判断
if (ch[i] == ch[i + 1]) {
count++;
}
if (ch[i] != ch[i + 1]) {
if (count > maxcount) {
maxcount = count;
max = ch[i];
}
count = 1;
}
}
System.out.println("出现最多的元素是:" + max + " 次数为:"+maxcount);
}
}
3.写一个单例模式的例子
class Ar {
private Ar ar = null;
private Ar() { //构造方法私有
}
public Ar getAr() { //返回Ar
if (ar == null) {
ar = new Ar();
}
return ar;
}
}
4.通过键盘输入一个字符串字符间用逗号隔开,且每个字符都是数字,
如”10,9,5,2,6” 。可以选择根据数字升序或者将序排序 如下:
import java.util.Arrays;
import java.util.Scanner;

public class TestOrder {
public static void main(String[] args) {
System.out.println("请输入要排序的数:");
Scanner s = new Scanner(System.in);
String[] str = s.nextLine().split(","); // 分解字符串
int[] num = new int[str.length];
for (int i = 0; i < str.length; i++) { // 放入整型数组
num[i] = Integer.parseInt(str[i]);
System.out.println(num[i]);
}
Arrays.sort(num); // 进行升序排序
System.out.println(Arrays.toString(num));
}
}

回答2:

功能基本实现了,不符合你的意思的地方稍微改下··
1.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;

public class Test1 {

public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String input [] = new String[5];
for(int i=0;i input[i]=br.readLine();
}
Arrays.sort(input);
for(int i=0;i System.out.println(input[i]);
}
}
}

2.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class Test2 {

public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("输入要输入的字符串");
String tagit1 = br.readLine();
String tagit = tagit1.toLowerCase();//大小写都是一个认识为同一字符
int temp=0,posit=0;
int letter[] = new int[26];

for (int i = 0; i < tagit.length(); i++) {
char a = tagit.charAt(i);

letter[a - 97]++;
}

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

if(letter[i]>temp)
{
temp=letter[i];
posit=i;
}
}

System.out.println("字符出现次数最多的是"+(char)(posit+97)+"出现次数为"+temp);
}

}

3.
public class Test3 {

int x;
private Test3(){

}
private static Test3 test;

public static Test3 getInstance(){
if(test==null){
test = new Test3();
}
return test;
}
}

4.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Test4 {

public static void main(String args[]) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("输入要输入的字符串");
String tagit = br.readLine();
int count =1;

Pattern p = Pattern.compile(",");
Matcher m = p.matcher(tagit);
while(m.find()){
count++;
}
int a[]=new int [count];
Matcher m2=Pattern.compile("[0-9]+").matcher(tagit);
int i=0;
while(m2.find()){

a[i]=Integer.parseInt(m2.group());
i++;
System.out.println(m2.group());
};

Arrays.sort(a);
for(int j=0;j if(j==a.length-1){System.out.println(a[j]);}
else{System.out.print(a[j]+",");}
}
}
}