Advertisement
ตัวอย่าง โค้ดโปรแกรมคำนวณเกรดที่ใช้ภาษาจาวา (JAVA) ในการพัฒนา โดยรับค่าของคะแนนผ่านทางคีย์บอร์ด แล้วนำไปตรวจสอบตามเงื่อนไข โดยใช้ if – elseif
Advertisement
/** * DekDEV.com */ import java.util.Scanner; public class GPA { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Input your score : "); float score = sc.nextFloat(); if(score < 0 || score > 100){ System.out.println("Error input score 0 - 100"); } else if(score >= 80){ System.out.println("Score = "+score+" , You Grade : A"); } else if(score >= 75){ System.out.println("Score = "+score+" , You Grade : B+"); } else if(score >= 70){ System.out.println("Score = "+score+" , You Grade : B"); } else if(score >= 65){ System.out.println("Score = "+score+" , You Grade : C+"); } else if(score >= 60){ System.out.println("Score = "+score+" , You Grade : C"); } else if(score >= 55){ System.out.println("Score = "+score+" , You Grade : D+"); } else if(score >= 50){ System.out.println("Score = "+score+" , You Grade : D"); } else{ System.out.println("Score = "+score+" , You Grade : F"); } System.out.println("---------------------------------------"); System.out.println("Powered by dekdev.com"); } }
Advertisement