Get me outta here!

September 24, 2015

Bài Tập Lập Trình JAVA số 1



  1.  Đếm ký tự hoa

package baitap_coban;

import java.util.Scanner;

public class Demki_tuhoa {

public static void main(String[] args) {
System.out.println("nhap vao mot chuoi: ");
Scanner scan = new Scanner(System.in);
String str = scan.nextLine();
int dem =0;
for(int i = 0;i<str.length();i++) {
if( str.charAt(i)>='A' && str.charAt(i)<='Z')
dem++;
//return dem ;
System.out.println(" "+dem);
}

}

2.  Tính điểm trong java

package baitap_coban;

import java.util.Scanner;

public class Diem {

public static void main(String[] args) {

System.out.println("nhap diem cua sinh vien: ");
Scanner nhap = new Scanner(System.in);
float a = nhap.nextFloat();
int b = nhap.nextInt();
if (a >= 3.6 && a <= 4.0)
System.out.println("hoc sinh xuat sac");
else if (a >= 3.2 && a < 3.6)
System.out.println("hoc sinh gioi");
else if (a >= 2.5 && a < 3.2)
System.out.println("hoc sinh kha");
else if (a >= 2.0 && a < 2.5)
System.out.println("hoc sinh trung binh");
else if (a >= 0.0 && a < 2.0)
System.out.println("hoc sinh yeu");
else
System.out.println("diem trung binh khong dung");

switch (b) {
case 1:
System.out.println("one");
break;
case 2:
System.out.println("three");
break;
default:
System.out.print("ket thuc");
}
}
}
3. Tính tiền karaoke
package baitap_coban;

import java.util.Scanner;

public class Karaoke {
public static void main(String[] args) {
int start, end;
Scanner nhap = new Scanner(System.in);
System.out.print("gio bat dau: ");
start = nhap.nextInt();
System.out.print("gio ket thuc: ");
end = nhap.nextInt();
if (end < 18) {
System.out.print("tien gio la: " + (end - start) * 45000 + "VND");
} else {
if (start < 18) {
System.out.print("tien gio la: "
+ (((18 - start) * 45000) + ((end - 18) * 60000))
+ "VND");
} else {
System.out.print("tien gio la: " + (end - start) * 60000
+ "VND");
}
}
}

}
4. Thực đơn
package baitap_coban;

import java.util.Arrays;
import java.util.Random;

public class Menu {
public String[] FoodName = { "Tao", "Cherry", "Cam", "Quyt", "Duahau",
"Chanh", "Kiwi", "Nho", "Melon", "DaoTien", "Chuoi", "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 0, 0, 0 },
{ 1, 1, 0, 0, 0, 0, 0, 0, 1, 0 }, { 1, 0, 0, 0, 1, 0, 1, 0, 0, 0 },
{ 1, 0, 0, 0, 0, 1, 0, 1, 0, 0 }, { 1, 1, 0, 0, 0, 1, 0, 0, 0, 0 },
{ 1, 1, 0, 0, 1, 1, 0, 1, 0, 0 } };

public int[] FoodValue = { 31, 66, 55, 70, 34, 50, 106, 38, 62, 49, 55, 82,
33, 13, 118, 54, 83, 15, 56, 123, 32, 40, 52, 63, 18, 74, 49, 62,
64, 55, 70, 100 };

public void run() {
khoitao();
for (int i = 0; i < 100; i++) {
danhgia();
print();
luachon();
laighep();
dotbien();
}
}

int N = 100;
int[][] cathes = new int[N][];
Random rand = new Random();

private void khoitao() {
for (int i = 0; i < N; i++) {
cathes[i] = new int[this.Food.length];
for (int j = 0; j < cathes[i].length; j++)
cathes[i][j] = rand.nextInt(2);
}
}

int[] f = new int[N];

private void danhgia() {
// giaca
for (int i = 0; i < N; i++) {
f[i] = 0;
for (int j = 0; j < cathes[i].length; j++)
if (cathes[i][j] == 1)
f[i] += this.FoodValue[j];

// đinh duong
for (int d = 0; d < 10; d++) {
boolean ok = false;

for (int j = 0; j < cathes[i].length; j++)
if (cathes[i][j] == 1)
if (this.Food[j][d] == 1)
ok = true;
if (!ok)
f[i] = f[i] + 30;
}
}
}

private void luachon() {
int[] temp = f.clone();
Arrays.sort(temp);
int nguong = temp[N * 80 / 200];
for (int i = 0; i < N; i++) {
if (f[i] > nguong) {
cathes[i] = cathes[rand.nextInt(N)].clone();
}
}
}

private void laighep() {
for (int i = 0; i < N / 5; i++) {
int vo = rand.nextInt(N);
int chong = rand.nextInt(N);
for (int j = 0; j < this.Food.length; j++)
if (rand.nextInt(2) == 1) {
int temp = cathes[vo][j];
cathes[vo][j] = cathes[chong][j];
cathes[chong][j] = temp;

}
}
}

private void dotbien() {
int ct = rand.nextInt(N);
int index = rand.nextInt(this.Food.length);
cathes[ct][index] = 1 - cathes[ct][index];
}

public static void main(String[] args) {
(new Menu()).run();
}

private void print() {
int[] temp = f.clone();
Arrays.sort(temp);
for (int i = 0; i < N; i++)
if (f[i] == temp[0]) {
for (int j = 0; j < cathes[i].length; j++)
if (cathes[i][j] == 1)
System.out.print(this.FoodName[j] + ",");
break;
}
System.out.println("" + temp[0]);
}

0 comments:

Post a Comment