วันอังคารที่ 2 กุมภาพันธ์ พ.ศ. 2559

ตัวอย่างการเขียนโปรแกรมด้วยภาษา Java

คำนวณยอดชำระค่าน้ำประปา โดยมีเงื่อนไขดังนี้1 - 50 หน่วย คิดหน่วยละ 4.25 บาท51 - 100 หน่วย คิดหน่วยละ 3.25 บาทตั้งแต่ 101 หน่วยขึ้นไป คิดหน่วยละ 2.25 บาท


import java.util.Scanner;

public class Water {
  public static void main(String[] args) {
    Scanner Scan = new Scanner(System.in);
    
    int Water = 0;
    double Total = 0;

      
      
    System.out.print(" ป้อนจำนวนที่ใช้น้ำ(ต่อหน่วย) :  ");
      Water = Scan.nextInt();
      
      if ( Water <= 50) {
       Total =  Water  * 4.25;
      } else if (Water <= 100) {
       Total =  Water  *  3.25;
      } else {
       Total =  Water  *  2.25;
      }
        
    System.out.println(" ======================== ");    
    System.out.println("      ค่าน้ำ: " + Total + " บาท ");
    System.out.println(" ======================== ");
  }
}

___________________________________________________________________________________
บริษัทแห่งหนึ่งผลิตสินค้าส่งออกได้ 2000 ชิ้นต่อปี ในแต่ละปีกำลังการผลิตเพิ่มขึ้นปีละ 365 ชิ้น 

ให้คำนวณและแสดงผลลัพธ์ว่าใช้เวลากี่ปีบริษัทแห่งนี้จึงจะผลิตสินค้าได้มากกว่า 5000 ชิ้นต่อปี



public class Production {
  public static void main (String[] args) {
     int product = 2000;
     int year = 0;
     while (product <= 5000) {
        product = product + 365;
        year++;
        System.out.println("ปีที่ = " + year);
        System.out.println("สินค้าส่งออก = " + product);
     }
     System.out.println("ใช้เวลาทั้งหมด = " + year + " ปี ");
  }
}

จงคำนวณค่าจอดรถ โดยมีเงื่อนไขต่อไปนี้ ถ้าจอดน้อยกว่า 2 ชั่วโมงไม่คิดค่าจอดรถ 
ถ้าจอดรถน้อยกว่า 6 ชั่วโมงคิดค่าจอดรถ 20 บาท 
ถ้าจอดรถ 6 ชั่วโมงขึ้นไปคิดค่าจอดรถ 50 บาท




import java.util.Scanner;
public class Work29 {
  public static void main(String[] args) {
     int hour = 0, price = 0;
     Scanner Scan = new Scanner(System.in);
     System.out.print("จำนวนชั่วโมง = ");
     hour = Scan.nextInt();
     if (hour < 2){
       price = 0;
        System.out.println("ไม่เสียค่าจอดรถ");
     }else if (hour < 6){
       price = 20;
        System.out.println("ค่าจอดรถ = " + price+ " บาท ");
     }else{
        price = 50;
        System.out.println("ค่าจอดรถ = " + price + " บาท ");
     }

  }
}



หาราคาค่าเช่าห้องพักโดยคิดวันละ 500 บาทต่อจากราคาห้องพัก โดยมีเงื่อนไขต่อไปนี้ 
ถ้าพักไม่เกิน 5 วัน จะไม่มีส่วนลด
ถ้าพัก 5-8 วัน จะมีส่วนลด 5%
แต่ถ้าพักมากกว่า 8 วันขึ้นไปจะมีส่วนลด 10%


import java.util.Scanner;
public class Room {
  public static void main(String[] args) {
     int rest = 0, price = 500;
     int discount = 0, net = 0;
     Scanner oj = new Scanner(System.in);
     System.out.print("จำนวนวันที่เข้าพัก = ");
     rest = oj.nextInt();
     if (rest < 5)
        {
        price = price * rest;
        net = price;
        }
     else if (rest  <= 8)
        {
        price = price * rest;
        discount = price *5 / 100;
        net = price-discount;
        }
     else
        {
        price = price * rest;
        discount = price * 10 / 100;
        net = price-discount;
        }
       
        System.out.println("ค่าเช่า= " + net + "บาท");
  }

ไม่มีความคิดเห็น:

แสดงความคิดเห็น