Advertisement
Guest User

testing dowhile

a guest
Jan 18th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.83 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package lab2_mob1013_ps05854_nguyendttam;
  7. import java.util.Scanner;
  8.  
  9. public class Lab2_ps05854_bai1 {
  10.  
  11.     public static void main (String args [])
  12.     {
  13.          
  14.         Scanner sc=new Scanner(System.in);
  15.     do {
  16.     try
  17.             {
  18.         System.out.println("     Xin chao den voi LAB 2    ");
  19.         System.out.println("________________________________");
  20.         System.out.println("|             MENU             |");
  21.         System.out.println("| 1. Giai phuong trinh bac nhat|");
  22.         System.out.println("| 2. Giai phuong trinh bac 2   |");
  23.         System.out.println("| 3. Tinh tien dien            |");
  24.         System.out.println("| 4. Ket thuc                  |");
  25.         System.out.println("________________________________");
  26.         System.out.print("Xin moi chon chuc nang: ");
  27.         int a=sc.nextInt();
  28.         if(a>5){System.out.println("Chi duoc nhap so tu 1 den 4");}
  29.      
  30.             switch(a)
  31.             {
  32.                 case 1:
  33.                 {
  34.                     Bai1();
  35.                 continue;
  36.                 }
  37.                 case 2:
  38.                 {
  39.                     Bai2();
  40.                 continue;
  41.                 }
  42.                 case 3:
  43.                 {
  44.                     Bai3();
  45.         continue;
  46.                 }
  47.                
  48.                 case 4: {
  49.             System.out.print("Ban da thoat khoi chuong trinh");
  50.             break;
  51.         }
  52.             }
  53.         }catch (Exception ex)
  54.         {
  55.         System.out.print("Xin kiem tra lai");
  56.         }
  57.     }while(true);
  58.  
  59.     }
  60.        
  61.     public static void Bai1 () {
  62.        try
  63.        {
  64.         System.out.println("---Giai phuong trinh bac 1---");
  65.            Scanner sc=new Scanner(System.in);
  66.        
  67.        System.out.print("Nhap he so a: ");
  68.        int a=sc.nextInt();
  69.        System.out.print("Nhap he so b: ");
  70.        int b=sc.nextInt();
  71.         if(a==0)
  72.         {
  73.             if(b==0)
  74.             {
  75.                 System.out.print("Phuong trinh co vo so nghiem! ");
  76.             }
  77.             else
  78.             {System.out.print("Phuong trinh vo nghiem!");}
  79.         }
  80.         else
  81.         {System.out.print("Phuong trinh co nghiem la: " + (-b/a));}
  82.     }  
  83.        catch(Exception ex)
  84.                {
  85.                    System.out.print("!!!Xin kiem tra lai he so da nhap!!!");
  86.                }
  87.      
  88.        
  89.     }
  90.     public static void Bai2()
  91.     {
  92.    
  93.         try
  94.         {
  95.             Scanner sc=new Scanner(System.in);
  96.             System.out.println("---Giai phuong trinh bac hai---");
  97.             System.out.print("Nhap he so a: ");
  98.             int a=sc.nextInt();
  99.             System.out.print("Nhap he so b: ");
  100.             int b=sc.nextInt();
  101.             System.out.print("Nhap he so c: ");
  102.             int c=sc.nextInt();
  103.             switch(a)
  104.             {
  105.                 case 0:
  106.                 {if(a==0)
  107.                     {
  108.                         if(b==0)
  109.                         {System.out.print("Phuong trinh co vo so nghiem! ");}
  110.                         else{System.out.print("Phuong trinh vo nghiem!");}
  111.                     }
  112.                   else{System.out.print("Phuong trinh co nghiem la: " + (-b/a));}
  113.                 }
  114.                 break;
  115.             default:
  116.             {
  117.                 double Delta=Math.pow(b,2)-4*a*c;
  118.                 if (Delta<0){System.out.print("Phuong trinh vo nghiem");}
  119.                 else if (Delta==0)
  120.                 {System.out.print("Phuong trinh co nghiep kep x la: " + (-b/(2*a)));}
  121.                 else
  122.                 {
  123.                 System.out.println("Phuong trinh co 2 nghiem la:");
  124.                 System.out.print("x1=" + ((-b+Math.sqrt(Delta))/(2*a)));
  125.                 System.out.print("x2=" + ((-b-Math.sqrt(Delta))/(2*a)));
  126.                 }
  127.                 break;
  128.             }
  129.             }
  130.        
  131.         }   catch(Exception ex)
  132.                {
  133.                    System.out.print("!!!Xin kiem tra lai he so da nhap!!!");
  134.                }  
  135.                    
  136.             }
  137.    
  138.      public static void Bai3()
  139.        {
  140.            try
  141.            {
  142.               System.out.println("---Tinh tien dien---");
  143.                Scanner sc=new Scanner(System.in);
  144.                System.out.print("Nhap vao so dien: ");
  145.                int soDien=sc.nextInt();
  146.                if(soDien<50)
  147.                {
  148.                  System.out.print("So tien phai tra la: "+soDien*1000);
  149.                }
  150.                else{System.out.print("So tien phai tra la: "+soDien*1200);}
  151.            }
  152.            catch(Exception ex)
  153.            {
  154.            System.out.print("!!!Xin kiem tra lai!!!");
  155.            }    
  156.        }
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement