【程序1】某玩家的基本属性为:力量,敏捷,智力,精神。随机赋予每项属性20~40之间的一个数值,并通过属性值给出推荐职业(力量最高适合战士职业,敏捷最高适合盗贼职业,智力最高适合法师职业,精神最高适合牧师职业)。请用面向对象思想描述出上述过程。
【程序2】
题目:对任意10个数进行排序
1.程序分析:可以利用选择法,即从后9个比较过程中,选择一个最小的与第一个元素交换, 下次类推,即用第二个元素与后8个进行比较,并进行交换。
2.要求:用数组完成
答案:
第一题:
import java.util.Random;
public class Play {
private int strength;// 力量
private int dexterity;// 敏捷
private int intelligence;// 智力
private int energy;// 精神
Random random = new Random();
public Play() {
this.strength = random.nextInt(21) + 20;
this.dexterity = random.nextInt(21) + 20;
this.intelligence = random.nextInt(21) + 20;
this.energy = random.nextInt(21) + 20;
job();
}
public void job() {
if (strength > dexterity && strength > intelligence
&& strength > energy) {
System.out.println("你的力量为:" + strength);
System.out.println("你的敏捷为:" + dexterity);
System.out.println("你的智力为:" + intelligence);
System.out.println("你的精神为:" + energy);
System.out.println("你最适合做一个战士");
}
if (dexterity > strength && dexterity > intelligence
&& dexterity > energy) {
System.out.println("你的力量为:" + strength);
System.out.println("你的敏捷为:" + dexterity);
System.out.println("你的智力为:" + intelligence);
System.out.println("你的精神为:" + energy);
System.out.println("你最适合做一个盗贼");
}
if (intelligence > strength && intelligence > dexterity
&& intelligence > energy) {
System.out.println("你的力量为:" + strength);
System.out.println("你的敏捷为:" + dexterity);
System.out.println("你的智力为:" + intelligence);
System.out.println("你的精神为:" + energy);
System.out.println("你最适合做一个法师");
}
if (energy > strength && energy > dexterity && energy > intelligence) {
System.out.println("你的力量为:" + strength);
System.out.println("你的敏捷为:" + dexterity);
System.out.println("你的智力为:" + intelligence);
System.out.println("你的精神为:" + energy);
System.out.println("你最适合做一个牧师");
}
}
public static void main(String[] args) {
Play play = new Play();
}
}
----------第2题-----------------------------
package array;
/**
* 数组的泡排序
*
* @author VillionZhang
*
*/
public class BubbleSort {
public int[] bubbleSort(int a[]) {
int temp;
int intlength = a.length;
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < intlength - (i + 1); j++) {
if (a[j] > a[j + 1]) { //控制数组降序/升序排序!
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
return a;
}
public static void main(String[] args) {
BubbleSort bs = new BubbleSort();
int a[] = { 6, 4, 8, 2, 10, 12, 89, 68, 45, 37 };
System.out.println("排序前数组:");
for (int i = 0; i < a.length; i++)
System.out.print(a[i] + " ");
System.out.println();
System.out.println("排序后数组:");
bs.bubbleSort(a);// 执行数组泡排序!
for (int i = 0; i < a.length; i++)
System.out.print(a[i] + " ");
}
}
【程序2】
题目:对任意10个数进行排序
1.程序分析:可以利用选择法,即从后9个比较过程中,选择一个最小的与第一个元素交换, 下次类推,即用第二个元素与后8个进行比较,并进行交换。
2.要求:用数组完成
答案:
第一题:
import java.util.Random;
public class Play {
private int strength;// 力量
private int dexterity;// 敏捷
private int intelligence;// 智力
private int energy;// 精神
Random random = new Random();
public Play() {
this.strength = random.nextInt(21) + 20;
this.dexterity = random.nextInt(21) + 20;
this.intelligence = random.nextInt(21) + 20;
this.energy = random.nextInt(21) + 20;
job();
}
public void job() {
if (strength > dexterity && strength > intelligence
&& strength > energy) {
System.out.println("你的力量为:" + strength);
System.out.println("你的敏捷为:" + dexterity);
System.out.println("你的智力为:" + intelligence);
System.out.println("你的精神为:" + energy);
System.out.println("你最适合做一个战士");
}
if (dexterity > strength && dexterity > intelligence
&& dexterity > energy) {
System.out.println("你的力量为:" + strength);
System.out.println("你的敏捷为:" + dexterity);
System.out.println("你的智力为:" + intelligence);
System.out.println("你的精神为:" + energy);
System.out.println("你最适合做一个盗贼");
}
if (intelligence > strength && intelligence > dexterity
&& intelligence > energy) {
System.out.println("你的力量为:" + strength);
System.out.println("你的敏捷为:" + dexterity);
System.out.println("你的智力为:" + intelligence);
System.out.println("你的精神为:" + energy);
System.out.println("你最适合做一个法师");
}
if (energy > strength && energy > dexterity && energy > intelligence) {
System.out.println("你的力量为:" + strength);
System.out.println("你的敏捷为:" + dexterity);
System.out.println("你的智力为:" + intelligence);
System.out.println("你的精神为:" + energy);
System.out.println("你最适合做一个牧师");
}
}
public static void main(String[] args) {
Play play = new Play();
}
}
----------第2题-----------------------------
package array;
/**
* 数组的泡排序
*
* @author VillionZhang
*
*/
public class BubbleSort {
public int[] bubbleSort(int a[]) {
int temp;
int intlength = a.length;
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < intlength - (i + 1); j++) {
if (a[j] > a[j + 1]) { //控制数组降序/升序排序!
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
return a;
}
public static void main(String[] args) {
BubbleSort bs = new BubbleSort();
int a[] = { 6, 4, 8, 2, 10, 12, 89, 68, 45, 37 };
System.out.println("排序前数组:");
for (int i = 0; i < a.length; i++)
System.out.print(a[i] + " ");
System.out.println();
System.out.println("排序后数组:");
bs.bubbleSort(a);// 执行数组泡排序!
for (int i = 0; i < a.length; i++)
System.out.print(a[i] + " ");
}
}