แสดงบทความที่มีป้ายกำกับ จาวา แสดงบทความทั้งหมด
แสดงบทความที่มีป้ายกำกับ จาวา แสดงบทความทั้งหมด

วันเสาร์ที่ 20 กรกฎาคม พ.ศ. 2556

โปรแกรมคิดเกรด A, B, C, D และ F โดยใช้คำสั่ง switch (ภาษาจาวา)

public class HW03_01 {


    public static void main(String[] args) {
        char grade = ' ';
        System.out.print("Input score: ");
        int score = new java.util.Scanner(System.in).nextInt();
        
        if ((score > 0) && (score < 101)) {
            switch (score / 10) {
                case 1:
                case 2:
                case 3:
                case 4: grade = 'F'; break;
                case 5: grade = 'D'; break;
                case 6: grade = 'C'; break;
                case 7: grade = 'B'; break;
                case 8:
                case 9:
                case 10: grade = 'A';
            }
            System.out.println("Grade: " + grade);
        } else
            System.out.println("Score over flow");
    }
}

โปรแกรมหาค่า z จากสมการที่กำหนด (ภาษาจาวา)

จงหาค่า Z จากสมการที่กำหนด ดังนี้
x > 0 : z = 2x^2
x < 0 : z = 2x + 4y
x = 0 : z = 1
public class HW03_08 {

    public static void main(String[] args) {
        int z;
        System.out.print("Input x: ");
        int x = new java.util.Scanner(System.in).nextInt();
        
        if (x > 0) {
            z = 2 * (x*x);
        } else if (x < 0) {
            System.out.print("Input y: ");
            int y = new java.util.Scanner(System.in).nextInt();
            z = (2 * x) + (4 * y);
        } else {
            z = 1;
        }
        
            System.out.println ("z = " + z);
    }
}

โปรแกรมการแสดงผลรูปสามเหลี่ยมติดกัน (ภาษาจาวา)




public class HW03_07 {

    public static void main(String[] args) {
       for (int r = 1; r <= 5; r++) {
            for (int c = 1; c <= r; c++) 
                System.out.print("*");
            for (int c = 1; c < (5*2)-(r*2); c++)
                System.out.print(" ");
            for (int c = 1; c <= r; c++)
                if (c != 5)
                    System.out.print("*");
            System.out.println();
        }
    }
}

วันเสาร์ที่ 6 กรกฎาคม พ.ศ. 2556

โปรแกรมการถอนเลขจำนวนเงิน (ภาษาจาวา)

โปรแกรมการถอนเลขจำนวนเงิน
import javax.swing.JOptionPane;

public class HW02 {

    public static void main(String[] args) {
        int m1000 = 0;
        int m500 = 0;
        int m100=0;
        int m50=0;
        int m20=0;
        int m10=0;
        int m5=0;
        int m2=0;
        int m1=0;
        
        int m = Integer.parseInt(JOptionPane.showInputDialog(null, "ระบุจำนวนเงิน", "Information", JOptionPane.INFORMATION_MESSAGE));
        String mStr = "จำนวนเงินที่ต้องระบุ: " + m;
        if (m > 0) {
            if (m >= 1000){
                m1000 = m / 1000;
                m %= 1000;
            }
            if (m >= 500) {
                m500 = m / 500;
                m %= 500;
            }
            if (m >= 100) {
                m100 = m / 100;
                m %= 100;
            }
            if (m >= 50) {
                m50 = m / 50;
                m %= 50;
           }
            if (m >= 20) {
                m20 = m / 20;
                m %= 20;
            }
            if (m >= 10) {
                m10 = m / 10;
                m %= 10;
            }
            if (m >= 5) {
                m5 = m / 5;
                m %= 5;
            }
            if (m >= 2) {
                m2 = m / 2;
                m %= 2;
            }
            m1 = m;
            
            mStr += "\n1,000 = " + m1000 + "\n500 = " + m500;
            mStr += "\n100 = " + m100 + "\n50 = " + m50;
            mStr += "\n20 = " + m20 + "\n10 = " + m10;
            mStr += "\n5 = " + m5 + "\n2 = " + m2;
            mStr += "\n1 = " + m1;
            JOptionPane.showMessageDialog(null, mStr, "Information", JOptionPane.INFORMATION_MESSAGE);
        } else
            JOptionPane.showMessageDialog(null, "ต้องระบุจำนวนเงินมากกว่า 0", "Information", JOptionPane.INFORMATION_MESSAGE);
    }
}

การแสดงข้อมูลโดยใช้ JOptionPane (ภาษาจาวา)

การแสดงข้อมูลโดยใช้ JOptionPane โดยใช้เมธอด (method) showMessageDialog
import javax.swing.JOptionPane;

public class HW01 {

    public static void main(String[] args) {
        String name = "Warawut";
        String surname = "Khangkhan";
        String department = "Information";
        
        JOptionPane.showMessageDialog(null, "Name: " + name + "  " + surname + "\nDepartment: " + department, "Information", JOptionPane.INFORMATION_MESSAGE);
    }
}