以下代码:
public class CharExp {
public static String charCount(String [] a){
String chc="" ;
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length(); j++) {
char ch = a[i].charAt(j);
int charcout = a[i].length();
chc= Character.toString(ch)+""+Integer.toString(charcout)+" ";
// String chc= ch + "" + charcout + " ";
// return chc;
System.out.print(chc);
}
}
System.out.print(chc);
return null;
}
public static void main(String[] args) {
String [] umesh= {"1h","xa yb","2h","xax yk zn","3h","5h","6h","2h"};
for (String umesh1 : umesh) {
String[] guddi = umesh1.split(" ");
if((guddi[0].charAt(0)=='1')
||(guddi[0].charAt(0)=='2')
||(guddi[0].charAt(0)=='3')
||(guddi[0].charAt(0)=='4')
||(guddi[0].charAt(0)=='5')
||(guddi[0].charAt(0)=='6')
||(guddi[0].charAt(0)=='7')) {
String ra1 = guddi[0].replaceAll("1", "v");
String ra2 = ra1.replaceAll("2", "t");
String ra3 = ra2.replaceAll("3", "t3");
String ra4 = ra3.replaceAll("4", "f");
String ra5 = ra4.replaceAll("5", "f5");
String ra6 = ra5.replaceAll("6", "s");
System.out.println(ra6);
} else {
{
for(int j=0; j<guddi.length; j++) {
}
charCount(guddi);
}
}
}
}
}
给我以下输出:
vh
x2 a2 y2 b2 b2 th
x3 a3 x3 y2 k2 z2 n2 n2 t3h
f5h
sh
th
在第二行的输出中,不需要b2
的重复。同样,在第三行中,不需要重复n2
。换句话说,所需的输出应为:
vh
x2 a2 y2 b2 th
x3 a3 x3 y2 k2 z2 n2 t3h
f5h
sh
th
此外,我需要在单个字符串中输出:
String str= "vh x2 a2 y2 b2 th x3 a3 x3 y2 k2 z2 n2 t3h f5h sh th"
答案 0 :(得分:4)
我不知道你想用代码实现什么,但有些东西会被打印两遍,因为你要打印两次。在charCount()
:
public static String charCount(String [] a){
String chc="" ;
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length(); j++) {
char ch = a[i].charAt(j);
int charcout = a[i].length();
chc= Character.toString(ch)+""+Integer.toString(charcout)+" ";
// String chc= ch + "" + charcout + " ";
// return chc;
System.out.print(chc);
}
}
// The next line is the problem. Why do you print here?
System.out.print(chc);
return null;
}
还学习一些基本的功能。您正在定义返回类型函数并返回null
?如果您只想打印,请创建函数void
。
要在单个字符串中获取输出,请在开头定义一个字符串,并将其中的每个结果连接起来。这很难吗?
编辑:
以下是您需要的代码。请注意,有更好的方法来做这些事情但我给你最简单的事实,你是一个初学者。
public class CharExp {
private static String final_output="";
public static void charCount(String [] a){
String chc="" ;
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length(); j++) {
char ch = a[i].charAt(j);
int charcout = a[i].length();
chc= Character.toString(ch)+""+Integer.toString(charcout)+" ";
final_output += chc + " ";
}
}
}
public static void main(String[] args)
{
String [] stringlist= {"1h","xa yb","2h","xax yk zn","3h","5h","6h","2h"};
for (String string : stringlist)
{
String[] splitted_list = string.split(" ");
if((splitted_list[0].charAt(0)=='1')
||(splitted_list[0].charAt(0)=='2')
||(splitted_list[0].charAt(0)=='3')
||(splitted_list[0].charAt(0)=='4')
||(splitted_list[0].charAt(0)=='5')
||(splitted_list[0].charAt(0)=='6')
||(splitted_list[0].charAt(0)=='7'))
{
String ra1 = splitted_list[0].replaceAll("1", "v");
ra1 = ra1.replaceAll("2", "t");
ra1 = ra1.replaceAll("3", "t3");
ra1 = ra1.replaceAll("4", "f");
ra1 = ra1.replaceAll("5", "f5");
ra1 = ra1.replaceAll("6", "s");
final_output += ra1 + " ";
}
else
{
charCount(splitted_list);
}
}
System.out.print(final_output);
}
}