代码不全,没看懂
import java.util.Scanner;
public class vv {
public static void main (String []args) {
Scanner scanner = new Scanner(System.in);
String str = "******Tom88987566461";
char[] chars = str.toCharArray();
int x=str.length()-13;
System.out.print("My name is ");//把这句放在循环之前就可以了
for(int i = 0; i < chars.length-x; i ++) {
if( ('a' <= chars[i] && chars[i] <= 'z') || ('A' <= chars[i] && chars[i] <= 'Z')) {
System.out.print(chars[i]);
}
}
System.out.println();//最后记得换行
}}
package test.any;
import java.util.Scanner;
public class vv {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String str = "******Tom88987566461";
char[] chars = str.toCharArray();
int x = str.length() - 13;
System.out.print("My name is ");//放到循环外边
for (int i = 0; i < chars.length - x; i++) {
if (('a' <= chars[i] && chars[i] <= 'z') || ('A' <= chars[i] && chars[i] <= 'Z')) {
System.out.print(chars[i]);
}
}
}
}
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in); //既然是输入的,为什么不用
String str = "******Tom88987566461";
char[] chars = str.toCharArray();
int x = str.length() - 13;//为什么要做这个处理
System.out.print("My name is ");//把语句放在外面可以达到你说的效果
for (int i = 0; i < chars.length - x; i++) {
if (('a' <= chars[i] && chars[i] <= 'z')
|| ('A' <= chars[i] && chars[i] <= 'Z')) {
System.out.print(chars[i]);//此时输出字母
}
}
}
}
不太明白你想干什么,但是你那样输出的原因是因为你的输出语句被循环多次
StringBuilder sb = new StringBuilder();
for(int i = 0; i < chars.length-x; i ++) {
if( ('a' <= chars[i] && chars[i] <= 'z') || ('A' <= chars[i] && chars[i] <= 'Z')) {
sb.append(chars[i]);
}
}
System.out.print("My name is "+ sb.toString());
代码贴错了吧。。
使用
Scanner sc=new Scanner(System.in);
String str=sc.next();
这样就可以从终端输入一个以enter结尾的字符串了,并且把它放到str中