字符串比较用equals方法,例:
public class StringTry {
public static void main(String[] args) {
// TODO Auto-generated method stub
String name="zg";
String password="123456";
Scanner input=new Scanner(System.in);
for(int i=0;i<3;i++) {
System.out.println("请输入姓名");
String Fname=input.next();
System.out.println("请输入密码");
String Fpassword=input.next();
if(name.equals(Fname)&&password.equals(Fpassword)){
System.out.println("你输入的姓名和密码都正确");
break;
}else {
System.out.println("输入错误,请重新输入");
}
if(i==2) {
System.out.println("你的机会用完了");
}
// TODO Auto-generated method stub
String name="zg";
String password="123456";
Scanner input=new Scanner(System.in);
for(int i=0;i<3;i++) {
System.out.println("请输入姓名");
String Fname=input.next();
System.out.println("请输入密码");
String Fpassword=input.next();
if(name.equals(Fname)&&password.equals(Fpassword)){
System.out.println("你输入的姓名和密码都正确");
break;
}else {
System.out.println("输入错误,请重新输入");
}
if(i==2) {
System.out.println("你的机会用完了");
}
}
}
}
charAt方法返回下标,用来遍历字符串,例:
public class StringTry2 {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.println("请输入一个字符串");
String aaa=input.next();
for(int i=0;i<aaa.length();i++) {
System.out.println(aaa.charAt(i));
}
int bigCount=0;
int smallCount=0;
int numberCount=0;
for(int i=0;i<aaa.length();i++) {
char ch=aaa.charAt(i);
if(ch>='a'&&ch<='z') {
smallCount++;
}else if(ch>='A'&&ch<='Z') {
bigCount++;
}else if(ch>='1'&&ch<='9') {
numberCount++;
}
}
System.out.println("大写字母"+bigCount+"小写字母"+smallCount+"数字"+numberCount);
}
}
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.println("请输入一个字符串");
String aaa=input.next();
for(int i=0;i<aaa.length();i++) {
System.out.println(aaa.charAt(i));
}
int bigCount=0;
int smallCount=0;
int numberCount=0;
for(int i=0;i<aaa.length();i++) {
char ch=aaa.charAt(i);
if(ch>='a'&&ch<='z') {
smallCount++;
}else if(ch>='A'&&ch<='Z') {
bigCount++;
}else if(ch>='1'&&ch<='9') {
numberCount++;
}
}
System.out.println("大写字母"+bigCount+"小写字母"+smallCount+"数字"+numberCount);
}
}
字符串大小一旦被声明,就改不了了,但是增强字符串可以比如StringBuilder
例:
public class StringBuilderTry {
public static void main(String[] args) {
StringBuilder sb=new StringBuilder();
sb.append(121214444).append('q');//链式编程,添加东西的方法
sb.reverse();//反转字符串
System.out.println(sb);
}
}
StringBuilder sb=new StringBuilder();
sb.append(121214444).append('q');//链式编程,添加东西的方法
sb.reverse();//反转字符串
System.out.println(sb);
}
}
StringBuilder实现字符串反转:
ublic class StringBuilderTry02 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input=new Scanner(System.in);
System.out.println("请输入一个字符串");
String s=input.next();
System.out.println("反转后:"+Huan(s));
}
public static String Huan(String y) {
StringBuilder sb=new StringBuilder(y);
sb.reverse();
String ss=sb.toString();
return ss;
}
}
// TODO Auto-generated method stub
Scanner input=new Scanner(System.in);
System.out.println("请输入一个字符串");
String s=input.next();
System.out.println("反转后:"+Huan(s));
}
public static String Huan(String y) {
StringBuilder sb=new StringBuilder(y);
sb.reverse();
String ss=sb.toString();
return ss;
}
}