英文:
How to find a particular name repeated how many number of times?
问题
我遇到了输出问题,有人能帮我吗?
这是一个用于查找特定名称重复了多少次的代码。
package example1;
import java.util.Scanner;
public class example1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int i,j,t,c;
String a[]=new String[10];
String b[]=new String[10];
Scanner sc=new Scanner(System.in);
for(i=0;i<10;i++)
{
a[i]=sc.nextLine();
b[i]=a[i];
}
for(i=0;i<10;i++)
{
c=0;
for(j=i+1;j<9;j++)
{
if(b[j]==b[i])
{
c++;
for(t=j;t<10;t++)
{
b[t]=b[t+1];
}
}
}
System.out.print(b[i]+" 被重复了 "+ c +" 次 ");
}
}
}
英文:
I'm recieving issue with the output can anyone help me with it?
It is a code for finding how many times a particular name has been repeated.
package example1;
import java.util.Scanner;
public class example1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int i,j,t,c;
String a[]=new String[10];
String b[]=new String[10];
Scanner sc=new Scanner(System.in);
for(i=0;i<10;i++)
{
a[i]=sc.nextLine();
b[i]=a[i];
}
for(i=0;i<10;i++)
{
c=0;
for(j=i+1;j<9;j++)
{
if(b[j]==b[i])
{
c++;
for(t=j;t<10;t++)
{
b[t]=b[t+1];
}
}
}
System.out.print(b[i]+" is repeated "+ c +" times ");
}
}
}
答案1
得分: 1
试试这个。
String[] arr = new String[] {"a", "b", "c", "d", "a", "b", "c", "a", "b", "a"};
List<String> list = Arrays.asList(arr);
Set<String> set = new HashSet<>(list);
for (String name : set)
System.out.println(name + " 重复了 " + Collections.frequency(list, name) + " 次");
如果你希望比较时不区分大小写,可以试试这个。
String[] arr = new String[] {"A", "B", "c", "D", "a", "b", "c", "a", "b", "a"};
List<String> list = new ArrayList<>(10);
for (String name : arr)
list.add(name.toLowerCase());
Set<String> set = new HashSet<>(list);
for (String name : set)
System.out.println(name + " 重复了 " + Collections.frequency(list, name) + " 次");
但会需要更多的内存空间。
英文:
Try this.
String[] arr = new String[] {"a","b","c","d","a","b","c","a","b","a"};
List<String> list = Arrays.asList(arr);
Set<String> set = new HashSet<>(list);
for(String name : set)
System.out.println(name + " is repeated " + Collections.frequency(list, name) + " times");
If you want your comparison to be case insensitive, then try this.
String[] arr = new String[] {"A","B","c","D","a","b","c","a","b","a"};
List<String> list = new ArrayList<>(10);
for(String name : arr)
list.add(name.toLowerCase());
Set<String> set = new HashSet<>(list);
for(String name : set)
System.out.println(name + " is repeated " + Collections.frequency(list, name) + " times");
Will require more space though.
答案2
得分: 0
以下代码应有助于确定重复项:
public class Repetition {
public static void main(String[] args) {
String string = "Sam sam is good boy james is sams friend clyra is clyra";
int count;
string = string.toLowerCase();
String words[] = string.split(" ");
System.out.println("重复的单词:");
for(int i = 0; i < words.length; i++) {
count = 1;
for(int j = i+1; j < words.length; j++) {
if(words[i].equals(words[j])) {
count++;
words[j] = "0"; // 为了以后的参考将其置为0
}
}
if(count > 1 && !words[i].equals("0"))
System.out.println(words[i]);
}
}
}
英文:
The following code should help for determining the repetition
public class Repetation {
public static void main(String[] args) {
String string = "Sam sam is good boy james is sams friend clyra is clyra";
int count;
string = string.toLowerCase();
String words[] = string.split(" ");
System.out.println("Duplicate words: ");
for(int i = 0; i < words.length; i++) {
count = 1;
for(int j = i+1; j < words.length; j++) {
if(words[i].equals(words[j])) {
count++;
words[j] = "0";//making it 0 for future reference
}
}
if(count > 1 && words[i] != "0")
System.out.println(words[i]);
}
}
}
答案3
得分: 0
public static int countWord(String input, String text) {
int index = input.indexOf(text);
if (index == -1 || text.isEmpty()) {
return 0;
}
int count = 0;
do {
count++;
index = input.indexOf(text, index + text.length());
} while (index > 0 && index < input.length());
return count;
}
This code will return 4
:
int total = countWord("abcs8abc88habcabci7h", "abc");
英文:
public static int countWord(String input, String text) {
int index = input.indexOf(text);
if (index == -1 || text.isEmpty()) {
return 0;
}
int count = 0;
do {
count++;
index = input.indexOf(text, index + text.length());
} while (index > 0 && index < input.length());
return count;
}
This code will return 4
:
int total = countWord("abcs8abc88habcabci7h", "abc");
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论