You are on page 1of 9

MR.

MAMPAN SANGSA 50052535

วิชา Computer Programming


ข้ อที่ 1)

โค้ ดที่ได้
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class TestArray1 {

/**
* @param args
*/
public static void main(String[] args) {
BufferedReader datain = new BufferedReader(new
InputStreamReader(
System.in));
String animal = "";
System.out.println("Please write name animal :" + animal);
try {
animal = datain.readLine();
} catch (IOException e) {
System.out.println("Error!");
}

// Display the name


System.out.println("Name is " + animal + "!");
String sex = "";
System.out.println("Please enter animal sex :" + sex);
try {
sex = datain.readLine();
} catch (IOException e) {
System.out.println("Error!");
}

// Display the name


System.out.println("Sex is " + sex + "!");

System.out.println("Please Enter name animal :");

// Read entered data into age variable


//try..catch is used for protection the program
try {
animal = datain.readLine();
} catch (IOException e) {
System.out.println("Error!");
}

// Display the name


System.out.println("Name is " + animal + "!");
String sex1 = "";
System.out.println("Please enter animal sex :" + sex1);
try {
sex1 = datain.readLine();
} catch (IOException e) {
System.out.println("Error!");
}

// Display the name


System.out.println("Sex is " + sex1 + "!");

ผลลัพธ์ ของโปรแกรม
Please write name animal :
Dog
Name is Dog!
Please enter animal sex :
Male
Sex is Male!
Please Enter name animal :
Cat
Name is Cat!
Please enter animal sex :
Female
Sex is Female!
ข้ อที่2

โค้ ดที่ด้
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class TestF3 {

/**
* @param args
*/
public static void main(String[] args) {
BufferedReader datain = new BufferedReader(new
InputStreamReader(
System.in));
String Money = "";
System.out.println("Please your money is :" + Money);
try {
Money = datain.readLine();
} catch (IOException e) {
System.out.println("Error!");
}
System.out.println("I have money is :" + Money + "Bath.");
String Money1 = "";
System.out.println("What do you need ? " + Money1);
try {
Money1 = datain.readLine();
} catch (IOException e) {
System.out.println("Error!");

}
System.out.println("You need money is :" + Money1 + "Bath");
System.out.println("Thank you" + "!");

ผลลัพธ์ ของโปรแกรม
Please your money is :
500
I have money is :500Bath.
What do you need ?
400
You need money is :400Bath
Thank you!

ข้ อที่3
โค้ ดที่ได้

public class TestFor {

/**
* @param args
*/
public static void main(String[] args) {
for (int count=0; count<=6; count++){
if (count!=5){
System.out.println(count);
}
else break;
}
System.out.println("End for");

ผลลัพธืของโปรแกรม
0
1
2
3
4
End for

ข้ อที่4

โค้ ดที่ได้
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class TestStudent {

/**
* @param args
*/
public static void main(String[] args) {
BufferedReader datain = new BufferedReader(new
InputStreamReader(
System.in));
String name = "";
System.out.println("Please enter your name is :");
try {
name = datain.readLine();
} catch (IOException e) {
System.out.println("Error!");
e.printStackTrace();
return;
}
System.out.println("My name is :" + name);
String age = "";
System.out.println("Please enter your age is :");
try {
age = datain.readLine();
} catch (IOException e) {
System.out.println("Error!");
e.printStackTrace();
return;
}
System.out.println("My age is :" + age + "ปี ");

BufferedReader dataIn = new BufferedReader(new


InputStreamReader(
System.in));
String str_piont = null;
System.out.println("Please your pionts is :");
try {
str_piont = dataIn.readLine();
} catch (IOException e) {
System.out.println("Error!");
e.printStackTrace();
return;
}
float points = Float.parseFloat(str_piont);
System.out.println("Your pionts is :" + points + "คะแนน");
char grade = 0;
if ((points >= 80) && (points <= 100)) {
grade = 'A';
} else if ((points >= 70) && (points <= 79)) {
grade = 'B';
} else if ((points >= 55) && (points <= 69)) {
grade = 'C';
} else if ((points >= 40) && (points <= 54)) {
grade = 'D';
} else if ((points >= 0) && (points <= 40)) {
grade = 'F';
} else {
return;
}

System.out.println("Your grade is :" + grade);

ผลลัพธ์ ของโปรแกรม
Please enter your name is :
John
My name is :John
Please enter your age is :
15
My age is :15 ปี
Please your points is :
80
Your points is :80.0 คะแนน
Your grade is :A

ข้ อที่5

โค้ ดที่ได้

public class TestWhile {

/**
* @param args
*/
public static void main(String[] args) {
int i=0;
while (i<10){
System.out.println(i+",");
i++;
}
System.out.println(i+",");

ผลลัพธ์ ของโปรแกรม

0,1,2,3,4,5,6,7,8,9,10,

You might also like