1. 程式人生 > >作業一學期二書十五章

作業一學期二書十五章

1.根據你的理解,簡要說明比較運算子(==)和equal()方法判斷兩個字串是否相等有什麼區別。

1.
==用於比較引用和比較基本資料型別時具有不同的功能:
比較基本資料型別,如果兩個值相同,則結果為true
而在比較引用時,如果引用指向記憶體中的同一物件,結果為true
2.
equals 方法(是String類從它的超類Object中繼承的)被用來檢測兩個物件是否相等,即兩個物件的內容是否相等,區分大小寫。
3.
s1 = new String("sony"); //建立的是字串物件
s1.equals("sony"); //返回true
s1 == "sony" //返回false
//如果
s1 =
"sony"; s1 == "sony" //返回true //如果 s1 = "sony"; s2 = "sony"; s1.equals(s2); 或者string.equals(s1,s2);//返回true

2.輸入五種水果的英文名稱(如葡萄grape、橘子orange、香蕉banana、蘋果apple、桃peach),編寫一個程式,輸出這些水果的名稱(按照在字典裡出現的先後順序輸出)。

import java.util.Arrays;
import java.util.Scanner;


public class DJ15word2 {
    @SuppressWarnings({ "resource"
, "unused" }) public static void main(String[] args) { String blank=""; String[] fruits=new String[5]; Scanner i=new Scanner(System.in); for(int a=0;a<fruits.length;a++){ System.out.print("請輸入第"+(a+1)+"種水果:"); fruits[a]=i.next(); } Arrays.sort(fruits); System.out
.println("\n這些水果在字典中出現的順序是:"); for(int d=0;d<fruits.length;d++){ System.out.println(fruits[d]); } } }

3.假設中國人的姓都是單個字,請隨機輸入一個人的姓名,然後輸入一個人的姓名,然後輸出姓和名。

import java.util.Scanner;


public class DJ15word3 {
    @SuppressWarnings("resource")
    public static void main(String[] args) {
        String name="";
        Scanner i=new Scanner(System.in);
        System.out.print("請輸入任意一個姓名:");
        name=i.next();
        System.out.println("姓氏:"+name.substring(0,1));   //name.charAt(0)和substring(0,1)都可以
        System.out.println("名字:"+name.substring(1,name.length()));
    }
}

4.錄入使用者的18位身份證號碼,從中提取使用者的生日。

import java.util.Scanner;


public class DJ15word4 {
    @SuppressWarnings("resource")
    public static void main(String[] args) {
        Scanner i=new Scanner(System.in);
        System.out.print("請輸入使用者的身份證號碼:");
        String id=i.next();
        if(id.length()==18){
            System.out.print("該使用者的生日是:");
            System.out.print(id.substring(6,10)+"年");
            System.out.print(id.substring(10,12)+"月");
            System.out.print(id.substring(12,14)+"日");
        }else{
            System.out.println("請輸入正確的身份證號碼!");
        }
    }
}

5.編寫一個字元瀏覽器,輸入字串及需要查詢的字元或字串,瀏覽器自動定位所有出現該字元或字串的位置。

import java.util.Scanner;


public class DJ15word5 {
    @SuppressWarnings("resource")
    public static void main(String[] args) {
        Scanner i=new Scanner(System.in);
        System.out.print("請輸入一段字元:");
        String one=i.next();
        System.out.print("請輸入要查詢的字元:");
        char two=i.next().charAt(0);
        System.out.print(two+"出現的位置是:");
        for(int a=0;a<one.length();a++){
            if(two==one.charAt(a)){
                System.out.print(a+"  ");
            }
        }
    }
}

6.對錄入的資訊進行有效性驗證。 錄入會員生日時,形式必須是“月/日”,如“09/12”。錄入的密碼位數必須為6~10位。允許使用者重複錄入,直到輸入正確為止。


public class DJ15word6judge {
    public String Birthday(String birthday){
        String a="";
        String prefix="該會員生日是:";
        if(birthday.indexOf('/')!=2){
            a="生日形式輸入錯誤!";
        }else{
            a=prefix.concat(birthday);
        }
        return a;
    }
    public String Password(String password){
        String b="";
        String prefix="該會員密碼是:";
        if(password.length()<6||password.length()>10){
            b="密碼形式輸入錯誤!";
        }else{
            b=prefix.concat(password);
        }
        return b;
    }
}
import java.util.Scanner;


public class DJ15word6 {
    @SuppressWarnings("resource")
    public static void main(String[] args) {
        DJ15word6judge l=new DJ15word6judge();
        Scanner i=new Scanner(System.in);
        String a="";
        String b="";
        do{
            System.out.print("請輸入會員生日<月/日:00/00>:");
            String birthday=i.next();
            System.out.println(l.Birthday(birthday));
            a=l.Birthday(birthday);
        }while("生日形式輸入錯誤!".equalsIgnoreCase(a));
        do{
            System.out.print("請輸入會員密碼<6~10位>:");
            String password=i.next();
            System.out.println(l.Password(password));
            b=l.Password(password);
        }while("密碼形式輸入錯誤!".equalsIgnoreCase(b));
    }
}