1 / 18

คำสั่งควบคุมการทำงาน

คำสั่งควบคุมการทำงาน. การเขียนโปรแกรมโดยปกติ มีทั้งให้ทำงานเป็นลำดับ ที่ละคำสั่ง บางครั้งมีการให้เปลี่ยนลำดับในการทำคำสั่ง เพื่อให้การเขียนโปรแกรมมีประสิทธิภาพสูงสุด ซึ่งในการควบคุมการทำงานของโปรแกรมพอแบ่งออกได้เป็น 3 รูปแบบคือ ตามลำดับ (Sequence Statements) ตัดสินใจ (Decision Statement)

jjesse
Télécharger la présentation

คำสั่งควบคุมการทำงาน

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. คำสั่งควบคุมการทำงาน • การเขียนโปรแกรมโดยปกติ มีทั้งให้ทำงานเป็นลำดับ ที่ละคำสั่ง บางครั้งมีการให้เปลี่ยนลำดับในการทำคำสั่ง เพื่อให้การเขียนโปรแกรมมีประสิทธิภาพสูงสุด ซึ่งในการควบคุมการทำงานของโปรแกรมพอแบ่งออกได้เป็น 3รูปแบบคือ • ตามลำดับ(Sequence Statements) • ตัดสินใจ (Decision Statement) • วนลูป (Loop Statement)

  2. import java.io.*; public class DemoIfElse2 { public static void main (String args[]) throws IOException { BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Press mounth (1 - 12) "); String input = " "; input = stdin.readLine(); int month = Integer.parseInt(input); String season; if (month ==1 || month ==2 || month==3) season = "Medium Hot"; else if (month ==4 || month ==5 || month==6) season = "Very Hot"; else if (month ==7 || month ==8 || month==9) season = "Hard Rainy"; else if (month ==10 || month ==11 || month==12) season = "Min Hot"; else season = "ERROR"; System.out.println ("Month " + month + " = " + season ); } }

  3. import javax.swing.*; import java.awt.*; public class SwitchControlSwing{ public static void main (String args[]) { JFrame frame = new JFrame("Switch..."); JPanel panel = new JPanel(); JLabel label; ImageIcon icon; int choice =2; char imgType ='s';// or "i" or Other switch(imgType){ case 's': icon = new ImageIcon("icon" + choice + ".png"); label = new JLabel("s=>"+choice ,icon, JLabel.RIGHT); panel.add(label); break; case 'i': icon = new ImageIcon("icon" + choice+ ".png"); label = new JLabel("s=>"+choice ,icon, JLabel.RIGHT); panel.add(label); break; default: icon = new ImageIcon("icon" + choice+ ".png"); label = new JLabel("s=>"+choice ,icon, JLabel.RIGHT); panel.add(label); break; } panel.setBackground(Color.white); frame.getContentPane().add(panel); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setVisible(true); } }

  4. การบ้าน

More Related