public class Sample1 {
//練習取得字串的長度與文字
public static void main(String[] args) {
String str = "Hello";
//取得字串的第一個字, 索引值一樣從0開始
char char1 = str.charAt(0);
char char2 = str.charAt(1);
//傳回字串的長度
int len = str.length();
System.out.println(str+"第1個字是"+char1);
System.out.println(str+"第2個字是"+char2);
System.out.println(str+"的長度是"+len);
}
}
import java.io.*;
public class Sample2 {
//轉換大小寫
public static void main(String[] args) throws IOException
{
System.out.println("請輸入英文字");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = br.readLine();
//轉成大寫
String stru = str.toUpperCase();
//轉成小寫
String strl = str.toLowerCase();
System.out.println("轉成大寫"+stru);
System.out.println("轉成小寫"+strl);
}
}
import java.io.*;
public class Sample3 {
//搜尋文字練習
public static void main(String[] args) throws IOException
{
System.out.println("請輸入字串");
BufferedReader br = new BufferedReader( new InputStreamReader(System.in));
String str1 = br.readLine();
System.out.println("請輸入要檢索的文字");
String str2 = br.readLine();
//取出輸入的第一個字
char ch = str2.charAt(0);
//indexOf 會傳回是字串的第幾個字, 一樣從0開始算 //如果找不到就會傳回 -1
int num = str1.indexOf(ch);
System.out.println("num="+num);
if ( num != -1 )
{
System.out.println(str1+"的第"+(num+1)+"個字["+ch+"]已經找到了");
}
else
{
System.out.println(str1+"中沒有["+ch+"]這個字");
}
}
}
import java.io.*;
public class Sample4 {
public static void main(String[] args) throws IOException
{
System.out.println("請輸入字串");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str1 = br.readLine();
System.out.println("請輸入要增加的字串");
String str2 = br.readLine();
//使用 StringBuffer 處理字串
StringBuffer sb = new StringBuffer(str1);
//附加 str2
sb.append(str2);
System.out.println(str1+"後增加"+str2+"就等於"+sb);
}
}
import java.io.*;
public class Sample5 {
public static void main(String[] args) throws IOException
{
System.out.println("請輸入2個整數");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str1 = br.readLine();
String str2 = br.readLine();
//轉成 integer
int num1 = Integer.parseInt(str1);
int num2 = Integer.parseInt(str2);
int ans = Math.max(num1, num2);
System.out.println(num1+"與"+num2+"兩個其中較大的是"+ans);
}
}
package chapter10.lab6;
//練習 類別型態變數
class Car
{
private int num;
private double gas;
//建構元
public Car()
{
num = 0;
gas = 0.0;
System.out.println("初始化汽車");
}
public void setCar(int n, double g)
{
num = n;
gas = g;
System.out.println("設定車號為"+num+"\t改變油量為"+gas);
}
public void show()
{
System.out.println("車號為"+num+"\t油量為"+gas);
}
}
public class Sample6 {
public static void main(String[] args) {
//宣告 car1
Car car1;
System.out.println("宣告car1");
//產生一個新的物件, 並指定給 car1
car1 = new Car();
//設定初始值
car1.setCar(1234, 20.5);
Car car2;
System.out.println("宣告 car2");
//兩個類別變數 car1 與 car2 指到同一個汽車物件
car2 = car1;
System.out.println("將car1 指定到 car2");
System.out.println("car1 所指的");
car1.show();
System.out.println("car2 所指的");
car2.show();
}
}
package chapter10.lab7;
class Car
{
private int num;
private double gas;
public Car()
{
num = 0;
gas = 0;
System.out.println("初始化汽車");
}
public void setCar(int n, double g)
{
num = n;
gas = g;
System.out.println("設定車號為"+num+"\t設定油量為"+gas);
}
public void show()
{
System.out.println("車號為"+num+"\n油量為"+gas);
}
}
public class Sample7 {
public static void main(String[] args) {
Car car1;
System.out.println("宣告 car1");
car1 = new Car();
car1.setCar(1234, 20.5);
Car car2;
System.out.println("宣告 car2");
car2 = car1;
System.out.println("將 car1 指定給 car2");
System.out.println("car1 所指的");
car1.show();
System.out.println("car2所指的");
car2.show();
//藉由變更物件的內容, 來驗證兩個變數指到同一個物件
System.out.println("改變car1 的內容");
car1.setCar(2345, 30.5);
System.out.println("car1 所指的");
car1.show();
System.out.println("car2所指的");
car2.show();
}
}
package chapter10.lab8;//使用類別變數作為引數
class Car
{
private int num;
private double gas;
//使用類別型態變數
private String name;
public Car()
{
num = 0;
gas = 0;
name = "沒有名稱";
System.out.println("初始化汽車");
}
public void secCar(int n, double g)
{
num = n;
gas = g;
System.out.println("設定車號"+num+"設定油量為"+gas);
}
public void setName(String nm)
{
name = nm;
System.out.println("設定名稱為"+name);
}
public void show()
{
System.out.println("車號是"+num);
System.out.println("汽油量為"+gas);
System.out.println("名稱為"+name);
}
}
public class Sample8 {
public static void main(String[] args) {
Car car1;
car1 = new Car();
car1.show();
int number = 1234;
double gasoline = 20.5;
String str = "1號車";
car1.secCar(number, gasoline);
car1.setName(str);
car1.show();
}
}
沒有留言:
張貼留言