Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 8

P3.1: Insurance /* * Insurance.java * Chng trnh Tnh ton tin bo him (S dng if-else-if) * Bn quyn (c) 2009 http://ljbe-course.googlecode.

com/ */ import java.util.Scanner; public class Insurance { /** * Phng thc ny l im vo ca chng trnh * @param args - Cc i s ca dng lnh */ public static void main(String[] args) { // Khai bo v khi to cc bin double installment = 0; int amount, choice = 0, years; Scanner input = new Scanner(System.in); // Nhp vo s tin bn mun System.out.println("Enter the amount you want to insure for: $"); amount = input.nextInt(); // Nhp vo s nm bn mun System.out.println("Enter the number of years you want to insure for:"); years = input.nextInt(); // Nhp vo la chn bn mun System.out.println("The following plans are available. Enter your choice:\n" + " 1. Monthly \n 2. Half-Yearly \n 3. Yearly"); choice = input.nextInt(); // Tnh ton tin ng bo him ty thuc vo s la chn ca khch hng if(choice == 1) { installment = (amount / (years - 2)) / 12; } else if(choice == 2) { installment = (amount / (years - 2)) / 2; } else if(choice == 3) { installment = (amount / (years - 2)); } else { System.out.println("Invalid Choice. The valid choices are 1, 2 or 3."); System.exit(0); }

// Hin th cc thng tin chi tit chnh sch System.out.println("\nPolicy Details:"); System.out.printf("Insurance Amount: \t\t $%d \n", amount); System.out.printf("Insurance duration (years): \t %d \n", years); if(choice == 1) { System.out.println("Insurance Plan: \t\t Monthly \n"); } else if(choice == 2) { System.out.println("Insurance Plan: \t\t Half-Yearly \n"); } else if(choice == 3) { System.out.println("Insurance Plan: \t\t Yearly \n"); } System.out.printf("Installment : \t\t\t $%.2f \n", installment); } } P3.2: Recruitment /* * Recruitment.java * Chng trnh Tuyn dng (S dng while loop) * Bn quyn (c) 2009 http://ljbe-course.googlecode.com/ */ import java.util.Scanner; public class Recruitment{ /** * Phng thc ny l im vo ca chng trnh * @param args - Cc i s ca dng lnh */ public static void main(String[] args) { int choice; int experience = 0; int count = 1; Scanner input = new Scanner(System.in); /* Lp cho n khi la chn c 3 Project Managers c bng MBA v 5 nm kinh nghim */ while(count < 4) { // Nhp vo s nm kinh nghim System.out.println("\nEnter the years of Experience: "); experience = input.nextInt();

// Kim tra xem nhng kinh nghim ca ng c vin ln hn 5 nm if(experience > 5) { System.out.println("\nAre you an MBA? (1 = Yes, 2 = No): "); choice = input.nextInt(); /* Nu cc th sinh c hn 5 nm kinh nghim sau kim tra xem ng c vin c bng MBA khng? */ if(choice == 1) count++; else System.out.println("Sorry, you need to be an MBA as well."); } else { System.out.println("Sorry, you need to have more than 5 years" + " of experience."); } System.out.print("********************************************"); } // Hin th cc bi vit sau khi hon tt qu trnh tuyn dng. System.out.println("\nThe recruitment process is completed."); System.out.println("Three Project Managers have been selected.\n"); } } } Yu cu:

Vit li chng trnh ny s dng do..while

P3.3: Article /* * Article.java * Chng trnh ny m t vic nh gi ni dung ca Article.(S dng vng lp for) * Bn quyn (c) 2009 http://ljbe-course.googlecode.com/ */ import java.util.Scanner; public class Article{ /** * Phng thc ny l im vo ca chng trnh

* @param args - Cc i s ca dng lnh */ public static void main(String[] args) { // Khai bo v khi to gi tr cc bin int choice; int poorCount = 0, fairCount = 0, goodCount = 0, excellentCount = 0; Scanner input = new Scanner(System.in); // Hin th ch v cc tiu ch nh gi System.out.println("Tell us what do you think about the content of" + " the article \"Basics of Java\"."); System.out.println("1. Poor"); System.out.println("2. Fair"); System.out.println("3. Good"); System.out.println("4. Excellent"); System.out.println("5. Exit"); // Nhp vo la chn System.out.println("\nEnter your choice:"); choice = input.nextInt(); // Lp cho n khi ngi dng nhp vo 5 for(;choice != 5;) { /* Tng kt qu nh gi */ switch(choice) { case 1: poorCount++; break; case 2: fairCount++; break; case 3: goodCount++; break; case 4: excellentCount++; break; default: System.out.println("Invalid input!");

break; } System.out.println("\nEnter your choice:"); choice = input.nextInt(); } // Hin th kt qu nh gi ch System.out.println("\nThe article \"Basics of Java\" has received " + "the following ratings:"); System.out.println("Poor: " + poorCount); System.out.println("Fair: " + fairCount); System.out.println("Good: " + goodCount); System.out.println("Excellent: " + excellentCount); } } } Yu cu:

Vit li chng trnh ny s dng do..while hoc while Vit chng trnh tnh tng cc s chia ht cho 3 b hn 100

P3.4: GameCredit /* * GameCredit.java * Chng trnh ny m t vic mua bn hng (S dng vng lp lng nhau) * Bn quyn (c) 2009 http://ljbe-course.googlecode.com/ */ import java.util.Scanner; public class GameCredit{ /** * Phng thc ny l im vo ca chng trnh * @param args - Cc i s ca dng lnh */ public static void main(String[] args) { int credit = 5000, choice = 0; int itemPrice = 0; Scanner input = new Scanner(System.in); // Lp cho n khi khch hng chn (5. Exit)

for(;(credit > 0) && (choice != 5); ) { // Lp cho n khi khch hng thot(5.Exit) // hoc vt s tin trong th do { // Hin th danh sch cc mt hng khch hng mua // V vi mt ty chon thot (5.Exit) System.out.println("\n1. Engine - $3000"); System.out.println("2. Suspension - $1500"); System.out.println("3. Tyre - $500"); System.out.println("4. Music System - $3000"); System.out.println("5. Exit "); // Hin th s tin ca khch hng c trong th System.out.println("\nYour current credit is: $" + credit); // Nhp la chn ca khch hng System.out.println("\nEnter your choice? "); choice = input.nextInt(); // Nu khch hng chn 5.Exit th thot khi vng lp if(choice == 5){ break; } // Tnh tin khi khch hng chn cc mt hng trn switch(choice) { case 1: itemPrice = 3000; break; case 2: itemPrice = 1500; break; case 3: itemPrice =500; break; case 4: itemPrice = 3000; break; default:

System.out.println("Invalid choice\n"); } // Nu khch hng c tin mua, cp nht li // gi tr s tin trong th, hoc thot khi vng lp if (credit >= itemPrice) credit -= itemPrice; else if (itemPrice > credit) { System.out.println("Sorry, the item price(" + itemPrice + ") exceeds your current credit(" + credit + ")\n"); break; } } while(choice >= 1 && choice <= 4 ); } } Yu cu:

Vit chng trnh hin th mt tam gic *, hnh ch nht *, hnh vung *, hnh thoi *

Ch dn cch lm bi hnh thoi (v kh hn cc bi khc) * *** ***** ******* ***** *** * int a = 15; int i,j; for (i=1; i <= a/2; i++) { for (j=1; j <= a - (2*i - 1); j++) System.out.print(" "); for (j=1; j <= 2*i - 1; j++) System.out.print("* "); System.out.println(""); }

for (i=1; i <= a; i++) System.out.print("* "); System.out.println(""); for (i=a/2; i >= 1; i--) { for (j=1; j <= a - (2*i - 1); j++) System.out.print(" "); for (j=1; j <= 2*i - 1; j++) System.out.print("* "); System.out.println(""); }

You might also like