必备单词
equals 相等的 lower降低的
ignore 忽略 last最后 trim修剪 concatenate连接 buffer缓冲 区 final最后的
字符串长度 调用length()方法 语法:字符串1.length(); (返回字符串的长度)
列:
<pre name="code" class="java">public class c{
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
String name,password;
System.out.println("请输入用户名:");
name=input.next();
System.out.println("请输入密码:");
password=input.next();
if(password.length()>6){
System.out.println("注册成功!");
}else{
System.out.println("密码长度不能小于6位!");
}
}
}
2.字符串的比较
字符串1.equals(字符串2);
比较两个字符串是否相同,返回boolean类型的值,如果相同则返回true不同则返回false。
示例:
<pre name="code" class="java">public class f{
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
String name,password;
System.out.println("请输入用户名");
name=input.next();
System.out.println("请输入密码:");
password=input.next();
if(name.equals("tom")&&password.equals("123456")){
System.out.println("恭喜您登录成功");
}else{
System.out.println("用户名和密码不匹配登录失败!");
}
}
}
package practice;
import java.util.Scanner;
public class p299 {
public static void main(String[] args) {
}
public boolean hao(String shen, String shou, String zuo) {
String str[] = zuo.split("-");
System.out.println(str[0]);
System.out.println(str[1]);
boolean f = false;
if (shen.length() != 16 && shen.length() != 18) {
System.out.println("身份证号必须是十六位或者十八位!");
} else if (shou.length() != 11) {
System.out.println("手机号码必须是十一位!");
} else if (str[0].length() != 4 && str[1].length() != 7) {
System.out.println("座机号码区号必须为四位,电话号码必须是七位");
} else {
System.out.println("注册成功!");
f=true;
}
return f;
}
}
package practice;
import java.util.Scanner;
public class showp299 {
/**
* @param args
*/
public static void main(String[] args) {
p299 g=new p299();
boolean d;
System.out.println("******欢迎登陆注册系统******");
System.out.println("请输入身份证号:");
Scanner input = new Scanner(System.in);
String zheng = input.next();
System.out.println("请输入手机号:");
String ji = input.next();
System.out.println("请输入座机号:");
String zouji=input.next();
d=g.hao(zheng,ji,zouji);
}
}
p300
package practice;
import java.util.Scanner;
public class p300 {
public static void main(String[] args) {
int count=0;
System.out.println("请输入一串字符串");
Scanner input=new Scanner(System.in);
String chuan=input.next();
System.out.println("请输入要查找的字符:");
String zhao=input.next();
System.out.println("字符串为"+chuan);
for(int i=0;i<chuan.length();i++){
String g=chuan.substring(i,i+1);
if(g.equals(zhao)){
count++;
}
}
System.out.println("包含"+count+"个"+zhao);
}
}
package cn.jbit.classandobject;
import java.util.Scanner;
<span style="white-space:pre"> </span>/
public class Goods {
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>String[] goods=new String[]{"电风扇","洗衣机","电视机","冰 箱","空调机"};
<span style="white-space:pre"> </span>double[] price=new double[]{124.23,4500,8800.90,5000.88,4456,12000.46};
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>public boolean login(){
<span style="white-space:pre"> </span>boolean flag=false;
<span style="white-space:pre"> </span>Scanner input = new Scanner(System.in);<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>System.out.print("请输入用户名: ");
<span style="white-space:pre"> </span>String name=input.next();
<span style="white-space:pre"> </span>System.out.print("请输入密码: ");
<span style="white-space:pre"> </span>String pwd=input.next();<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>if(name.equals("TOM")&&pwd.equals("123")){
<span style="white-space:pre"> </span>System.out.println("登录成功! ");
<span style="white-space:pre"> </span>flag=true;
<span style="white-space:pre"> </span>}else{
<span style="white-space:pre"> </span>System.out.println("用户名或密码不匹配,登录失败!");
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>return flag;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>public StringBuffer change(double d){
<span style="white-space:pre"> </span>StringBuffer str=new StringBuffer(String.valueOf(d));<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>for(int i=str.indexOf(".")-3;i>0;i=i-3){
<span style="white-space:pre"> </span>str.insert(i,',');
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>return str;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>public void showGoods(){
<span style="white-space:pre"> </span>System.out.print("*********欢迎进入商品批发城*********");
<span style="white-space:pre"> </span>System.out.print("\n\t编号\t商品\t价格\n");
<span style="white-space:pre"> </span>for(int i=0;i<goods.length;i++){
<span style="white-space:pre"> </span>System.out.print("\t"+(i+1));
<span style="white-space:pre"> </span>System.out.print("\t"+goods[i]);<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>System.out.print("\t"+change(price[i])+"\n");<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>System.out.println("**********************************");
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>public static void main(String[] args) {
<span style="white-space:pre"> </span>Scanner input=new Scanner(System.in);<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>Goods g=new Goods();
<span style="white-space:pre"> </span>int serial,num;
<span style="white-space:pre"> </span>double totle=0;
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>if(g.login()){
<span style="white-space:pre"> </span>g.showGoods();
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>System.out.print("请输入您批发的商品编号:");
<span style="white-space:pre"> </span>serial=input.nextInt();
<span style="white-space:pre"> </span>System.out.print("请输入批发数量:");
<span style="white-space:pre"> </span>num=input.nextInt();
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>totle=g.price[serial-1]*num;//计算总金额<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>System.out.print("您需要付款:"+g.change(totle));
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>}
}
package practice;
public class p292 {
/**
* @param args
*/
public boolean yan(String name,String pass1,String pass2 ){
boolean c=false;
do{
if(name.length()<3||pass1.length()<6){
System.out.println("用户名长度不能小于3,密码长度不能小于6");
}else if(!pass1.equals(pass2)){
System.out.println("两次输入的密码不相同");
}else{
System.out.println("注册成功!请牢记用户名和密码!");
c=true;
}
return c;
}while(c);
}
}
package practice;
import java.util.Scanner;
public class showp292 {
/**
* @param args
*/
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
p292 n=new p292();
boolean v=false;
do{
System.out.println("请输入用户名:");
String name=input.next();
System.out.println("请输入密码:");
String pass1=input.next();
System.out.println("请再次输入密码:");
String pass2=input.next();
v=n.yan(name,pass1,pass2);
}while(v);
}
}