Upload
shen
View
33
Download
0
Embed Size (px)
DESCRIPTION
การโปรแกรมเชิงวัตถุด้วยภาษา JAVA. input from keyboard. มหาวิทยาลัยเนชั่น http:// www. nation. ac.th. บุรินทร์ รุจจนพันธุ์ . ปรับปรุง 6 กรกฎาคม 2550. Get value from user. การรับค่าจากผู้ใช้เข้า Parameter ทำได้หลายวิธี 1. Argument from Command Line 2. System.in.read() - PowerPoint PPT Presentation
Citation preview
http://www.thaiall.com/class Page: 1
การโปรแกรมเชิงวั ตถุ�ด้�วัยภาษา JAVA
บุ�รนทร� ร�จจนพั นธุ์�� .ปรั�บปรั�ง 6
กรักฎาคม 2550
input from keyboard
http://www.thaiall.com/class Page: 2
Get value from user
การร บุค่�าจากผู้!�ใชิ�เข้�า Parameter ท$าได้�หลายวัธุ์( 1. Argument from Command Line
2. System.in.read()
3 . readLine()from BufferedReader
http://www.thaiall.com/class Page: 3
Argument in Command Line(1/2)
class x { public static void main(String args[]){ System.out.println(args.length); }}
DOS>java x0
DOS>java x y z2
http://www.thaiall.com/class Page: 4
Argument in Command Line(2/2)
class x { public static void main(String args[]){ System.out.println(args[0 ]+args[1 ]); }}
DOS>java x error : arrayindexoutofbounds
DOS>java x y zyz
http://www.thaiall.com/class Page: 5
System.in.read (1/5)
import java.io.*; class x {
public static void main(String args[]) throws IOException { char buf; buf = (char)System.in.read(); System.out.println("Output is "+buf); }}
โปรแกรมน()จ$าเป*นต�อง import java.io.* เพัราะเร(ยกใชิ� IOException
http://www.thaiall.com/class Page: 6
System.in.read (2/5)
char buf1;
int buf2;
buf1 = (char)System.in.read();
System.out.println(" Output is "+buf1 );
2buf = System.in.read();
SSSSSS SSSSSSSSSSSSSS Output is 2
โปรแกรมน()ไม�ใชิ� import java.io.* แต�เร(ยกตรง ๆ เพั(ยงค่ร )งเด้(ยวัเม./อกรอกข้�อม!ลให�พัมพั� ab และ enter จะพับุ a และ 98
http://www.thaiall.com/class Page: 7
System.in.read (3/5)
char buf1,buf2;
buf1 = (char)System.in.read();
2buf = (char)System.in.read();
System.out.println(buf1 + buf2);195//
เม./อกรอกข้�อม!ลให�พัมพั� ab และ enter จะพับุ 195
http://www.thaiall.com/class Page: 8
System.in.read (4/5)
char b1,b2;
b1 = (char)System.in.read();
b2 = (char)System.in.read();
System.out.print(b1+""+b2 ); //ab
System.out.print((char)b1+(char)b2);195//
เม./อกรอกข้�อม!ลให�พัมพั� ab และ enter จะพับุ ab195
http://www.thaiall.com/class Page: 9
System.in.read (5/5)
char b1,b2; b1 = (char)System.in.read();
b2 = (char)System.in.read(); 3 1 2// error: String b = b + b ; 3 1 2// error: String b = (char)b + (char)b ;
String b3= b1 +""+ b2; 4 = . ( 1 + 2 ) ;String b Integer toString b b 5String b =
Integer.toString((char)b1+b2); . . (3 + 4 + 5 ) ;
เม./อกรอกข้�อม!ลให�พัมพั� ab และ enter จะพับุ ab97b97b
http://www.thaiall.com/class Page: 10
.readLine (1/4) import java.io.*;
class x { public static void main(String args[]) throws IOException { BufferedReader stdin = new BufferedReader (new InputStreamReader(System.in));
String buf; buf = stdin.readLine(); System.out.println(buf);
}}
ร บุค่�า แล�วัแสด้งผู้ล
http://www.thaiall.com/class Page: 11
.readLine (2/4)
String buf;
buf = stdin.readLine();
System.out.println(buf);
buf = stdin.readLine();
System.out.println(buf);
กรอกข้�อม!ล 12 และ 34 จะพับุ 1234
http://www.thaiall.com/class Page: 12
.readLine (3/4)
String buf;
int i1,i2,i3;
buf = stdin.readLine();
i1 = Integer.parseInt(buf);
2i = Integer.parseInt(stdin.readLine());
i3 = i1 + i2;
1 2 3System.out.println(i + i + i );
กรอกข้�อม!ล 12 และ 34 จะพับุ 92
http://www.thaiall.com/class Page: 13
.readLine (4/4)
String buf; int t = 0,i; do {
buf = stdin.readLine(); i = Integer.parseInt(buf); t += i;
} while (i > 0 );System.out.println(t);
กรอกต วัเลข้ จะหาผู้ลรวัม จนกวั�าจะร บุเลข้ 0 จ1งจะพัมพั�ผู้ลรวัม