Java day 04丶29

Java第六天##

  1. 二维数组

    • 定义二维数组的语法:
    T[][] x = new T[size1][size2];
    T[][] y = new T[size][];
    T[][] z = {{v1, v2}, {v3, v4, v5, v6}, {v7}};
    
    • 二维数组的应用场景:表格、矩阵、棋盘、2D游戏中的地图。
  2. 字符串用法及常用方法

    • 字符串对象的创建
    	String s1 = "Hello";
        String s1 = new String("Hello");
    
    • 字符串常用方法
      • equals:比较两个字符串内容是否相同
      • equalsIgnoreCase:忽略大小写比较两个字符串内容是否相同
      • compareTo:比较两个字符串的大小
      • length:计算字符串的长度
      • concat:字符串连接
      • charAt:从字符串中取指定位置的字符
      • indexOf/lastIndexOf:字符串的匹配
      • trim:修剪字符串左右两端的空白
      • toUpperCase/toLowerCase:将字符串变成大写/小写
      • substring:从字符串中取指定位置的子串
      • startsWith/endsWith:判断字符串是否以指定的字符串开头/结尾
      • replace:将字符串中指定内容替换为另外的字符串

extra:字符串常用方法

package com.tara;
/**
 * 
 * @author tara
 *
 */
public class StringMethod {
	public static void main(String[] args) {
		String str1 = "Hello"; //存储在静态区(static)
		String str2 = new String("Hello"); //str2为引用类型,str2存储的是引用的String对象的地址,在栈(stack)中;String对象存储在堆(heap)中
		
		System.out.println((str1 == str2));
		
		System.out.println(str1.equals(str2));  //比较两个字符串
		System.out.println(str1.equalsIgnoreCase(str2)); //比较两个字符串,忽略大小写
		
		System.out.println(str1.length()); //返回字符串的长度
		
		System.out.println(str1.charAt(1));  //取字符串指定索引位置的一个字符
		
		System.out.println(str1.substring(1)); //从指定位置开始取字符串
		System.out.println(str1.substring(1,2)); //从指定位置开始取字符串,返回字符串长度为2-1
		
		System.out.println(str1.concat("World!")); //将指定字符串连接到此字符串的结尾。
		
		System.out.println(str1.indexOf("lo")); //返回指定字符串在此字符串中第一次出现处的索引。
		System.out.println(str1.indexOf("lo", 3)); //返回指定字符串在此字符串中从指定位置开始第一次出现处的索引。
		System.out.println(str1.lastIndexOf("lo")); //返回指定字符串在此字符串中最后一次出现处的索引。
		System.out.println(str1.indexOf('l')); //返回指定字符在此字符串中第一次出现处的索引。
		System.out.println(str1.lastIndexOf('l')); //返回指定字符在此字符串中最后一次出现处的索引。
		
		System.out.println(str1.trim()); //返回字符串的副本,忽略前导空白和尾部空白。
		
		System.out.println(str1.replace('e', 'p')); //返回一个新的字符串,用 newChar替换此字符串中出现的所有 oldChar得到的。
		System.out.println(str1.replace("e", "p"));  //...
		
		System.out.println(str1.toUpperCase()); //使用默认语言环境的规则将此 String 中的所有字符都转换为大写。
		System.out.println(str1.toLowerCase()); //使用给定 Locale 的规则将此 String 中的所有字符都转换为大写。
		
		System.out.println(str1.compareTo(str2));  //比较字符串首字符的Unicode编码大小
		
		String filename = "hello.exe";
		
		if(filename.endsWith(".exe")) {   //测试此字符串是否以指定的后缀结束。
			System.out.println("这是一个可执行文件!");
		}
		
		System.out.println(filename.startsWith("helo"));  //测试此字符串是否以指定的前缀开始。
		
	}
}

练习6:输入用户名和密码,验证用户身份。

package com.tara;

import java.util.Scanner;

/**
 * 系统只有一个用户 id:admin  pswd:123456
 * @author tara
 *
 */
public class StringPassward {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		boolean isLogin = false;
		do{
			System.out.println("id:");
			String id = sc.nextLine().trim();
			System.out.println("pswd:");
			String pswd = sc.nextLine().trim();
			isLogin = id.equals("admin") && pswd.equals("123456");
			if(!isLogin) {
				System.out.println("用户名或密码错误!");
			}
		}while(!isLogin);
		System.out.println("欢迎!");

		sc.close();
	}
}
package com.tara;

import java.util.Scanner;
/**
 * 系统有三个用户
 * @author tara
 *
 */
public class StringPasswards {
	
	public static int compareString(String[] x, String y) {
		for(int i = 0; i < x.length; ++i) {
			if(x[i].equals(y)) {
				return i;
			}
		}
		return -1;
	}
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		String[] ids = {"111222", "111333", "111444"};
		String[] pswds ={"abc123", "opq", "xyz1"};
		
		boolean isLogin = false;
		do {
			System.out.print("用户名:");
			String id = sc.nextLine().trim();
			System.out.print("密码:");
			String pswd = sc.nextLine().trim();
			int index = compareString(ids, id);
			if (index != -1) {
				if(pswd.equals(pswds[index])){
					isLogin = true;
				}
			}
			if(!isLogin){
				System.out.println("用户名或密码错误!请重新输入.");
			}
		} while (!isLogin);
		System.out.println("欢迎!");		
		
		
		
//		boolean isLogin = false;
//		do {
//			System.out.print("用户名:");
//			String id = sc.nextLine().trim();
//			System.out.print("密码:");
//			String pswd = sc.nextLine().trim();
//			int index = compareString(ids, id);
//			for (int i = 0; i < ids.length; ++i) {
//				if (id.equals(ids[i]) && pswd.equals(pswds[i])) {
//					isLogin = true;
//				}
//			}
//			if (!isLogin) {
//				System.out.println("用户不存在或密码错误!请重新输入.");
//			}
//		} while (!isLogin);
//		System.out.println("欢迎!");
		
		sc.close();
	}
}

练习7:跑马灯效果。

package com.tara;
/**
 * 
 * @author tara
 *
 */
public class Light {
	public static void main(String[] args) throws InterruptedException {
		String str = "欢迎来到狼窝.....";

		while(true) {
			System.out.println(str);
			str = str.substring(1) + str.charAt(0);
			Thread.sleep(200);
		}

	}
}

练习8:实现字符串倒转、字符串去掉空格、字符串大小写互换的方法。

package com.tara;
/**
 * 
 * @author tara
 *
 */
public class StringReverse{
	/**
	 * 字符串倒序输出
	 * @param str 待倒序字符串
	 * @return 倒序后的字符串
	 */
	public static String reverse(String str){
		String newStr = "";
		for(int i = str.length()-1; i >=0; --i) {
			newStr += str.charAt(i);
		}
		return newStr;
	}

	public static void main(String[] args) {
		System.out.println(reverse("hello"));
		System.out.println(reverse("malloc"));
		System.out.println(reverse("我爱你"));
		System.out.println(reverse("i love you"));
		
		System.out.println(trimAll("  sdg sgas gasgas sdB   "));
		
		System.out.println(switchUpperLower("Hell,World!"));
	}
	/**
	 * 去掉字符串中的空白字符
	 * @param str 待转换字符串
	 * @return 转换后的字符串
	 */	
	public static String trimAll(String str) {
		String newStr = "";
		for(int i = 0; i <str.length(); ++i) {
			if(str.charAt(i) != ' ')
			newStr += str.charAt(i);
		}
		return newStr;
	}
	/**
	 * 字符串大小写转换
	 * @param str 待转换字符串
	 * @return 转换后的字符串
	 */
	public static String switchUpperLower(String str) {
		String newStr = "";
		for(int i = 0; i <str.length(); ++i) {
			char ch = str.charAt(i);
			if(ch >= 'A' && ch <='Z') {
				ch += 32;  //#############
			}
			else if(ch >= 'a' && ch <='z') {
				ch -= 32;
			}
		}
		return newStr;
	}
	
}

posted @ 2015-04-29 20:42  悲伤丶才说爱  阅读(138)  评论(0编辑  收藏  举报