java 输入一串单词 输出其中重复的单词
展开全部
实现思路:就是单词之间通过空格分隔的,所以只需要通过split方法,进行截取,之后循环判断每个单词出现的次数,大于1的输出即可。
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class TestScanner
{
public static void main ( String[] args )
{
List<String> list = new ArrayList<String> ();
Scanner scanner = new Scanner (System.in);
String next = null;
System.out.println ("输入多个单词,空格分开: ");
while (scanner.hasNextLine ())
{
next = " " + scanner.nextLine () + " ";
String[] array = next.split ("\\s+");
for ( String string : array )
{
String str = " " + string + " ";
if (!"".equals (string) && next.indexOf (str) != next.lastIndexOf (str))
{
list.add (string);
}
}
if (list.size () > 0)
{
System.out.println ("重复的单词是:" + list.toString ().replaceAll ("\\[|\\]", "").replaceAll (",\\s+", " "));
}
else
{
System.out.println ("没有重复的");
}
}
}
}
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class TestScanner
{
public static void main ( String[] args )
{
List<String> list = new ArrayList<String> ();
Scanner scanner = new Scanner (System.in);
String next = null;
System.out.println ("输入多个单词,空格分开: ");
while (scanner.hasNextLine ())
{
next = " " + scanner.nextLine () + " ";
String[] array = next.split ("\\s+");
for ( String string : array )
{
String str = " " + string + " ";
if (!"".equals (string) && next.indexOf (str) != next.lastIndexOf (str))
{
list.add (string);
}
}
if (list.size () > 0)
{
System.out.println ("重复的单词是:" + list.toString ().replaceAll ("\\[|\\]", "").replaceAll (",\\s+", " "));
}
else
{
System.out.println ("没有重复的");
}
}
}
}
展开全部
package zhidao;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/**
* @author yugi111
*/
public class TestScanner
{
public static void main ( String[] args )
{
List<String> list = new ArrayList<String> ();
Scanner scanner = new Scanner (System.in);
String next = null;
System.out.println ("输入一行单词,空格分开: ");
while (scanner.hasNextLine ())
{
next = " " + scanner.nextLine () + " ";
String[] array = next.split ("\\s+");
for ( String string : array )
{
String str = " " + string + " ";
if (!"".equals (string) && next.indexOf (str) != next.lastIndexOf (str))
{
list.add (string);
}
}
if (list.size () > 0)
{
System.out.println ("重复的有:" + list.toString ().replaceAll ("\\[|\\]", "").replaceAll (",\\s+", " "));
}
else
{
System.out.println ("没有重复的");
}
list.clear ();
System.out.println ("继续输入:");
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
package test;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Test {
public static List list = new ArrayList();
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String str = scanner.nextLine();
String[] strs = new String[10];
strs = str.split(" ");
for (int i = 0; i < strs.length; i++) {
list.add(strs[i]);
}
for (int i = 0; i < list.size(); i++) {
for (int j = i+1; j < strs.length; j++) {
if (list.get(i).equals(list.get(j))) {
System.out.println(list.get(i));
}
}
}
}
}
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Test {
public static List list = new ArrayList();
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String str = scanner.nextLine();
String[] strs = new String[10];
strs = str.split(" ");
for (int i = 0; i < strs.length; i++) {
list.add(strs[i]);
}
for (int i = 0; i < list.size(); i++) {
for (int j = i+1; j < strs.length; j++) {
if (list.get(i).equals(list.get(j))) {
System.out.println(list.get(i));
}
}
}
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询