-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathJavaLAB
More file actions
164 lines (133 loc) · 8.44 KB
/
Copy pathJavaLAB
File metadata and controls
164 lines (133 loc) · 8.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
*** จำโจทย์ออกมา แล้วก็มาทำใหม่นะ อาจจะไม่เหมือนเดิม 5555
** จริงๆ มันไม่ตายตัว มีหลายวิธีไม่ตายตัวนะ -..-
1. สุ่มเลขสามตัว แสดงออกมา แล้วหาค่ากลาง
class Exam1{
public static void main(String[] args){
int a = ( (int)(Math.random() * (95+5+1)-5) );
int b = ( (int)(Math.random() * (95+5+1)-5) );
int c = ( (int)(Math.random() * (95+5+1)-5) );
System.out.println("a = "+a+"\nb = "+b+"\nc = "+c+"\n");
if (a > b) {
if (b > c) {
System.out.println("b ("+b+") is the median");
}
else if (a > c) {
System.out.println("c ("+c+") is the median");
}
else{
System.out.println("a ("+a+") is the median");
}
}
else{
if (a > c){
System.out.println("a ("+a+") is the median");
}
else if (b > c){
System.out.println("c ("+c+") is the median");;
}
else{
System.out.println("b ("+b+") is the median");
}
}
}
}
2. รับค่าเลขจำนวนเต็มหกหลัก เอาสองตัวกลาง สองตัวหลัง มา + - * และเฉลื่ย
import java.util.Scanner;
class Exam2{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.print("Enter Number : ");
int input = sc.nextInt();
int mid = input%10000/100;
int last = input%100;
System.out.println(mid+" + "+last+" = "+(mid+last));
System.out.println(mid+" - "+last+" = "+(mid-last));
System.out.println(mid+" * "+last+" = "+(mid*last));
System.out.println("Mean of "+mid+", "+last+" = "+((mid+last)/2.0));
}
}
3. สุ่มเบอร์ 10 หลัก 3 หลักแรกไม่มีเลข 8,9 สามหลักกลางอยู่ในช่วง100-888 ที่เหลือไม่กำหนด
import java.text.DecimalFormat;
class Exam3{
public static void main(String[] args){
DecimalFormat fmt = new DecimalFormat("0000");
int a = ((int)(Math.random() * (7+1)));
int b = ((int)(Math.random() * (7+1)));
int c = ((int)(Math.random() * (7+1)));
int d = ((int)(Math.random() * (888-100+1)+100));
String e = (fmt.format(Math.random() * (9999+1)));
System.out.println("Tel : "+a+b+c+"-"+d+"-"+e);
}
}
4. ถ้ากรอกตัวอักษรเล็กให้พิมพ์ตัวอักษรใหญ่ ถ้ากรอกตัวอักษรใหญ่ให้พิมพ์ตัวอักษรเล็ก
import java.util.Scanner;
class Exam4{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.print("Enter Letter : ");
String input = sc.nextLine();
int letter = ( (int)input.charAt(0) );
if ( letter>='A' & letter<='Z' ){
System.out.println( (char)(letter+32) );
}
else if ( letter>='a' & letter<='z' ){
System.out.println( (char)(letter-32) );
}
else{
System.out.println("Not an Alphabelt");
}
}
}
5. สุ่มตัวอักษร A-Z
class Exam5{
public static void main(String[] args){
System.out.println( (char)((int)(Math.random() * ('Z'-'A'+1)+'A')));
}
}
6. รับตัวอักษรจากผู้ใช้ ถ้า 0-9 แสดงว่า Numbers ถ้า a-z แสดงว่า LowerCase ถ้า A-Z แสดงว่า UpperCase ถ้าอย่างอื่นให้แสดงว่า Others ถ้าเกิน 1 ตัวให้บอกว่า Invalid Character
import java.util.Scanner;
import java.text.DecimalFormat;
class Exam6{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.print("Input : ");
String input = sc.nextLine();
int letter = ((int)input.charAt(0));
if( input.length() == 1 ){
if( letter>='0' & letter <= '9' ){
System.out.println("Numbers");
}
else if ( letter >='a' & letter <= 'z' ){
System.out.println("Lower Case");
}
else if ( letter >='A' & letter <= 'Z' ){
System.out.println("Upper Case");
}
else{
System.out.println("Others");
}
}
else{
System.out.println("Invaild Character");
}
}
}
7. รับค่า miles hours minutes seconds แล้วมาแสดงออกมาเป็นกิโลเมตรต่อชม.
import java.util.Scanner;
import java.text.DecimalFormat;
class Exam7{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.print("Enter Miles : ");
int miles = sc.nextInt();
System.out.print("Enter Hours : ");
int hr = sc.nextInt();
System.out.print("Enter Minutes : ");
int min = sc.nextInt();
System.out.print("Enter Seconds : ");
int sec = sc.nextInt();
double time = hr+(min/60.0)+(sec/60.0/60.0);
DecimalFormat fmt = new DecimalFormat("0.00");
System.out.println("Result : "+ fmt.format((miles*1.609344)/time)+" KM/Hr");
}
}