import java.io.*;//調用io包
public class SimpleCharInOut
{
public static void main(String args[])
{
char ch=' ';//定義個字符ch初始為‘ ’
System.out.println(" Enter a character please");//在屏幕上輸出Enter a character please
try {//你要監視的程式碼放在 try 區塊里即可。在 try 區塊之后緊接著在 catch 子句里指定你希望捕捉的例外型態
ch=(char)System.in.read();//將從鍵盤輸入的字符賦給ch
}
catch(IOException e) //如果上面的代碼有錯誤,這里就捕獲
{ } ;//錯誤后不進行操作
System.out.println("You're entered character:" + ch);// 在屏幕上輸出You're entered character:
//和ch的值
}
}
1.讀萬卷書
2.行千里路
3.閱人無數
public class SimpleCharInOut
{
public static void main(String args[])
{
char ch=' ';//定義個字符ch初始為‘ ’
System.out.println(" Enter a character please");//在屏幕上輸出Enter a character please
try {//你要監視的程式碼放在 try 區塊里即可。在 try 區塊之后緊接著在 catch 子句里指定你希望捕捉的例外型態
ch=(char)System.in.read();//將從鍵盤輸入的字符賦給ch
}
catch(IOException e) //如果上面的代碼有錯誤,這里就捕獲
{ } ;//錯誤后不進行操作
System.out.println("You're entered character:" + ch);// 在屏幕上輸出You're entered character:
//和ch的值
}
}
1.讀萬卷書
2.行千里路
3.閱人無數
class InputData { //定義從鍵盤輸入數據的類
static private String s = "";
static public void input() { //從鍵盤輸入一行字符,保存到字符串s中
BufferedReader bu = new BufferedReader(
new InputStreamReader(System.in)
);
try {
s = bu.readLine();
}
catch (IOException e) {} //捕獲輸入輸出異常
}
static public int getInt() { //靜態方法可直接用類名調用
input();
return Integer.parseInt(s); //將字符組成的字符串s轉化為整型數據后返回
}
}
class Result {
void print(int d) {
System.out.println(d + "的平方:" + d * d);
System.out.println(d + "的立方:" + d * d * d);
}
}
public class PrintResult {
public static void main(String[] args) {
Result result = new Result();
System.out.println("請輸入一個整數:");
int a = InputData.getInt();
result.print(a);
}
}