老师留的一个作业,就是输入一段字符:
例如:
输入EinsteinFest Rocks!显示如下:
"EinsteinFest Rocks!" contains 'i' at position 1 and position 6.
"EinsteinFest Rocks!" contains 'n' at position 2 and position 7.
"EinsteinFest Rocks!" contains 's' at position 3 and position 10 and position 17.
"EinsteinFest Rocks!" contains 't' at position 4 and position 11.
"EinsteinFest Rocks!" contains 'e' at position 5 and position 9.
"EinsteinFest Rocks!" contains 's' at position 10 and position 17.
我已经写好了一个了,但是输出的时候有点小问题,谁能帮助一下我啊,感激不尽啊!!
import java.util.*;
public class test5
{
public static void main(String args[])
{
Scanner myScanner = new Scanner (System.in);
System.out.println("Enter a single String : ");
String input = myScanner.nextLine();
String m="";
char letter;
char letter2;
for (int i=0; i<input.length(); i++)
{
letter = input.charAt(i);
String newStr = input.substring(i+1, input.length());
for( int j=i+1; j<input.length(); j++)
{
letter2 = input.charAt(j);
if (letter2==letter)
{
m += " and position " + j;
}
}
if (newStr.indexOf(letter) != -1)
{
System.out.print("\"" + input + "\"" + " contains letter '" + letter);
System.out.println(" at postion " + i + new String(m) + ".");
}
}
} // End main
} // End class