Created
August 17, 2017 11:33
-
-
Save naijab/850b0d4cd60410250f8ed75551ebcf42 to your computer and use it in GitHub Desktop.
Home Work Week 1 ( 15 - 8 - 2560 )
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*3. โปรแกรมเครื่องตรวจบัตรนักศึกษาจำต้องใช้เลข 3 ตัวท้าย ของบัตรประจำตัวนักศึกษา | |
ให้เราเขียนโปรแกรมเพื่อนำเลข 3 หลักสุดท้ายออกมาโดย กำหนดตัวแปร long เป็นรหัสนักศึกษา */ | |
import java.util.Scanner; | |
public class FindLastThreeStudentNumber { | |
private static long studentCode; | |
private static Scanner input; | |
public static void main(String[] args) { | |
initScanner(); | |
show("Enter student code: "); | |
findLast3Number(studentNumber()); | |
} | |
private static void initScanner() { | |
input = new Scanner(System.in); | |
} | |
private static long studentNumber() { | |
return studentCode = input.nextLong(); | |
} | |
private static void findLast3Number(long studentCode) { | |
String student = String.valueOf(studentCode); | |
show("Last 3 number: "); | |
show(student.substring(8, 11)); | |
} | |
private static void show(String message) { | |
System.out.print(message); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 2. จงเขียนโปรแกรมเพื่อคำนวนหาค่าเลขกลับของเลขจำนวนเต็มใด ๆ ในช่วง 100 ถึง 999 เช่น เลข 123 ผลลัพธ์คือ 321 | |
(Hint: ใช้ operator % และ / ในการคำนวน) */ | |
import java.util.InputMismatchException; | |
import java.util.Scanner; | |
public class FindNumberReverse { | |
private static int number; | |
private static int reverse = 0; | |
private static Scanner input; | |
public static void main(String[] args) { | |
initScanner(); | |
show("Enter number: "); | |
fillNumber(); | |
reverseNumber(number); | |
} | |
private static void initScanner() { | |
input = new Scanner(System.in); | |
} | |
private static void fillNumber() { | |
try { | |
number = input.nextInt(); | |
} | |
catch (InputMismatchException e) { | |
show("Sorry, You fill number very overload."); | |
} | |
} | |
private static void reverseNumber(int number) { | |
if (number > 100 && number <= 999) { | |
while (number != 0) { | |
reverse = reverse * 10; | |
reverse = reverse + number % 10; | |
number = number / 10; | |
} | |
show("Reverse n1umber: " + reverse); | |
} else { | |
show("Error, you must fill integer 101 - 999"); | |
} | |
} | |
private static void show(String message) { | |
System.out.print(message); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 1. จงเขียนโปรแกรมเพื่อแปลงหน่วยกิโลเมตรให้เป็นไมล์ โดยให้นักศึกษาสมมติค่ากิโลเมตรเก็บในตัวแปรชื่อ kilo คำนวน | |
โดยใช้สูตร miles=kilometers/1.609 ผลลัพธ์ที่ได้เก็บในตัวแปรชื่อ miles */ | |
import java.util.Scanner; | |
public class KiloToMile { | |
private static int kilo = 0; | |
private static float mile = 0f; | |
private static float kiloToMile = 1.609f; | |
private static Scanner input; | |
public static void main(String[] args) { | |
initScanner(); | |
show("Enter kilometer: "); | |
kilo = input.nextInt(); | |
show("Mile: "+ kiloToMile(kilo)); | |
} | |
private static void initScanner() { | |
input = new Scanner(System.in); | |
} | |
private static float kiloToMile(int kilo) { | |
mile = kilo / kiloToMile; | |
return mile; | |
} | |
private static void show(float mile) { | |
System.out.println(mile); | |
} | |
private static void show(String message) { | |
System.out.print(message); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment