1. 快速排序、冒泡排序
public static void main ( String[ ] args) {
int [ ] array = makeArray ( 10 ) ;
System. out. print ( "排序前的10个数组:" ) ;
printArray ( array) ;
sort2 ( array) ;
System. out. print ( "排序后的10个数组:" ) ;
printArray ( array) ;
}
public static void sort2 ( int [ ] array) {
System. out. println ( "使用冒泡排序--sort2--" ) ;
for ( int i = 0 ; i < array. length - 1 ; i++ ) {
boolean flag = false ;
for ( int j = 0 ; j < array. length - 1 - i; j++ ) {
if ( array[ j + 1 ] < array[ j] ) {
int temp1 = array[ j + 1 ] ;
array[ j + 1 ] = array[ j] ;
array[ j] = temp1;
flag = true ;
}
}
if ( ! flag) {
break ;
}
}
}
public static void sort1 ( int [ ] array) {
System. out. println ( "使用快速排序--sort1--" ) ;
for ( int i = 0 ; i < array. length; i++ ) {
for ( int j = i + 1 ; j < array. length; j++ ) {
if ( array[ j] < array[ i] ) {
int temp1 = array[ j] ;
array[ j] = array[ i] ;
array[ i] = temp1;
}
}
}
}
public static int [ ] makeArray ( int size) {
Random rand = new Random ( ) ;
int [ ] array = new int [ size] ;
for ( int i = 0 ; i < array. length; i++ ) {
array[ i] = rand. nextInt ( 50 ) ;
}
return array;
}
public static void printArray ( int [ ] array) {
System. out. println ( Arrays. toString ( array) ) ;
}
2. x + 2y + 5 z = 100 有多少解?
public static void main ( String[ ] args) {
System. out. println ( "三重循环的结果:" ) ;
int sum1 = 0 ;
for ( int x = 0 ; x <= 100 ; x++ ) {
for ( int y = 0 ; y <= 50 ; y++ ) {
for ( int z = 0 ; z <= 20 ; z++ ) {
if ( x + 2 * y + 5 * z == 100 ) {
sum1++ ;
}
}
}
}
System. out. println ( sum1) ;
System. out. println ( "二重循环的结果:" ) ;
int sum2 = 0 ;
for ( int y = 0 ; y <= 50 ; y++ ) {
for ( int z = 0 ; z <= 20 ; z++ ) {
if ( 2 * y + 5 * z <= 100 ) {
sum2++ ;
}
}
}
System. out. println ( sum2) ;
System. out. println ( "一重循环的结果:" ) ;
int sum3 = 0 ;
for ( int z = 0 ; z <= 20 ; z++ ) {
sum3 += ( 100 - 5 * z) / 2 + 1 ;
}
System. out. println ( sum3) ;
}
3. 菱形
public static void main ( String[ ] args) {
lingxing1 ( 4 ) ;
}
public static void lingxing2 ( int n) {
int length = n + n - 1 ;
for ( int i = 0 ; i < length; i++ ) {
if ( i < n) {
for ( int j = 0 ; j < n - i - 1 ; j++ ) {
System. out. print ( " " ) ;
}
for ( int j = 0 ; j < 2 * i + 1 ; j++ ) {
System. out. print ( "*" ) ;
}
System. out. println ( ) ;
} else {
for ( int j = 0 ; j < i - n + 1 ; j++ ) {
System. out. print ( " " ) ;
}
for ( int j = 0 ; j < 4 * n - 2 * i - 3 ; j++ ) {
System. out. print ( "*" ) ;
}
System. out. println ( ) ;
}
}
}
public static void lingxing1 ( int n) {
for ( int i = 0 ; i < n; i++ ) {
for ( int j = 0 ; j < n - i - 1 ; j++ ) {
System. out. print ( " " ) ;
}
for ( int j = 0 ; j < 2 * i + 1 ; j++ ) {
System. out. print ( "*" ) ;
}
System. out. println ( ) ;
}
for ( int i = n - 2 ; i >= 0 ; i-- ) {
for ( int j = 0 ; j < n - i - 1 ; j++ ) {
System. out. print ( " " ) ;
}
for ( int j = 0 ; j < 2 * i + 1 ; j++ ) {
System. out. print ( "*" ) ;
}
System. out. println ( ) ;
}
}
4. 杨辉三角
public static void main ( String[ ] args) {
int [ ] [ ] res = generate ( 7 ) ;
for ( int i = 0 ; i < res. length; i++ ) {
for ( int j = 0 ; j < res[ i] . length; j++ ) {
System. out. print ( res[ i] [ j] + "\t" ) ;
}
System. out. println ( ) ;
}
}
public static int [ ] [ ] generate ( int numRows) {
int [ ] [ ] res = new int [ numRows] [ ] ;
for ( int i = 0 ; i < res. length; i++ ) {
res[ i] = new int [ i + 1 ] ;
res[ i] [ 0 ] = 1 ;
res[ i] [ i] = 1 ;
if ( i > 1 ) {
for ( int j = 1 ; j < res[ i] . length - 1 ; j++ ) {
res[ i] [ j] = res[ i - 1 ] [ j - 1 ] + res[ i - 1 ] [ j] ;
}
}
}
return res;
}
5. 打印字母
public static void main ( String[ ] args) {
byte [ ] [ ] hello = {
{ 1 , 0 , 0 , 1 , 0 , 1 , 1 , 1 , 1 , 0 , 1 , 0 , 0 , 0 , 0 , 1 , 0 , 0 , 0 , 0 , 0 , 1 , 1 , 0 } ,
{ 1 , 0 , 0 , 1 , 0 , 1 , 0 , 0 , 0 , 0 , 1 , 0 , 0 , 0 , 0 , 1 , 0 , 0 , 0 , 0 , 1 , 0 , 0 , 1 } ,
{ 1 , 1 , 1 , 1 , 0 , 1 , 1 , 1 , 1 , 0 , 1 , 0 , 0 , 0 , 0 , 1 , 0 , 0 , 0 , 0 , 1 , 0 , 0 , 1 } ,
{ 1 , 0 , 0 , 1 , 0 , 1 , 0 , 0 , 0 , 0 , 1 , 0 , 0 , 0 , 0 , 1 , 0 , 0 , 0 , 0 , 1 , 0 , 0 , 1 } ,
{ 1 , 0 , 0 , 1 , 0 , 1 , 1 , 1 , 1 , 0 , 1 , 1 , 1 , 1 , 0 , 1 , 1 , 1 , 1 , 0 , 0 , 1 , 1 , 0 } ,
} ;
letter ( hello, '*' ) ;
}
public static void letter ( byte [ ] [ ] bytes, char ch) {
for ( int i = 0 ; i < bytes. length; i++ ) {
for ( int j = 0 ; j < bytes[ i] . length; j++ ) {
if ( bytes[ i] [ j] == 1 ) {
System. out. print ( ch) ;
} else {
System. out. print ( " " ) ;
}
}
System. out. println ( ) ;
}
System. out. println ( ) ;
}
6. 打地鼠
一维数组版
public static void main ( String[ ] args) {
while ( true ) {
int [ ] map = new int [ 10 ] ;
int pos = new Random ( ) . nextInt ( 10 ) ;
map[ pos] = 1 ;
for ( int b : map) {
System. out. print ( b + " " ) ;
}
System. out. println ( ) ;
System. out. println ( "请输入地鼠出现的位置:" ) ;
int input = new Scanner ( System. in) . nextInt ( ) ;
if ( pos == input) {
System. out. println ( "打中了" ) ;
} else {
System. out. println ( "没打中" ) ;
}
try {
Thread. sleep ( 1000 ) ;
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
}
}
}
二维数组版
public static void main ( String[ ] args) {
while ( true ) {
int [ ] [ ] map = new int [ 10 ] [ 10 ] ;
int pos1 = new Random ( ) . nextInt ( 10 ) ;
int pos2 = new Random ( ) . nextInt ( 10 ) ;
map[ pos1] [ pos2] = 1 ;
for ( int i = 0 ; i < map. length; i++ ) {
for ( int j = 0 ; j < map[ i] . length; j++ ) {
System. out. print ( map[ i] [ j] + " " ) ;
}
System. out. println ( ) ;
}
System. out. println ( "请输入地鼠出现位置的行和列:" ) ;
int input1 = new Scanner ( System. in) . nextInt ( ) ;
int input2 = new Scanner ( System. in) . nextInt ( ) ;
if ( pos1 == input1 && pos2 == input2) {
System. out. println ( "打中了" ) ;
} else {
System. out. println ( "没打中" ) ;
}
try {
Thread. sleep ( 1000 ) ;
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
}
}
}
对象版
public class HitMouse {
public static void main ( String[ ] args) {
HoleMap holeMap = new HoleMap ( ) ;
Mouse mouse = new Mouse ( ) ;
Player player = new Player ( ) ;
while ( true ) {
mouse. createNewPos ( ) ;
holeMap. reflase ( mouse) ;
holeMap. show ( ) ;
player. getHitPos ( ) ;
holeMap. hitSuccess ( player) ;
}
}
}
class Player {
private int input;
public Player ( ) {
input = 0 ;
}
public void getHitPos ( ) {
System. out. println ( "输入打击位置:" ) ;
input = new Scanner ( System. in) . nextInt ( ) ;
}
public int getInput ( ) {
return input;
}
public void setInput ( int input) {
this . input = input;
}
}
class HoleMap {
private int [ ] map;
public HoleMap ( ) {
map = new int [ 10 ] ;
}
public void hitSuccess ( Player player) {
if ( map[ player. getInput ( ) ] == 1 ) {
System. out. println ( "恭喜你打中了!" ) ;
} else {
System. out. println ( "太可惜,就差一点点!" ) ;
}
try {
Thread. sleep ( 1000 ) ;
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
}
}
public void show ( ) {
for ( int b : map) {
System. out. print ( b + " " ) ;
}
System. out. println ( ) ;
}
public void reflase ( Mouse mouse) {
for ( int i = 0 ; i < map. length; i++ ) {
map[ i] = 0 ;
}
map[ mouse. getPos ( ) ] = 1 ;
}
public int [ ] getMap ( ) {
return map;
}
public void setMap ( int [ ] map) {
this . map = map;
}
}
class Mouse {
private int pos;
public Mouse ( ) {
pos = 0 ;
}
public void createNewPos ( ) {
pos = new Random ( ) . nextInt ( 10 ) ;
}
public int getPos ( ) {
return pos;
}
public void setPos ( int pos) {
this . pos = pos;
}
}
鼠蛇版
public class Controller {
public static void main(String[] args) {
Animal animal = new Animal();
Mouse mouse = new Mouse();
Snake snake = new Snake();
HoleMap holeMap = new HoleMap();
Player player = new Player();
while (true) {
int[] a = {1, 1, 1, 1, 1, 1, 1, 2, 2, 2};
int type = new Random().nextInt(10); // 1老鼠 2蛇
switch (a[type]) {
case 1: // 老鼠
animal = mouse;
break;
case 2: // 蛇
animal = snake;
break;
}
animal.createNewPos(); // 生成一个位置
holeMap.reflash(animal); // 7成概率老鼠 3成概率蛇
holeMap.show(); // 展示地图
System.out.println("总分为:" + player.totalScore + " 血量为:" + player.blood);
player.getHitPos(); // 打击位置
holeMap.hitSuccess(player, animal); // 判断打击是否成功
if (player.blood <= 0) {
System.out.println("GAME OVER" + " 你的血量为:" + player.blood);
System.out.println("得分为:" + player.totalScore);
break;
}
System.out.println("--------------------------------------------------");
}
}
}
class Player {
public int pos; // 打击位置
public int blood; // 血量
public int totalScore; // 总得分
public Player() {
pos = 0;
blood = 3;
totalScore = 0;
}
// 输入打击位置
public void getHitPos() {
System.out.println("请输入打击位置:");
pos = new Scanner(System.in).nextInt();
}
}
class Snake extends Animal {
public Snake() {
score = 3;
}
void beat() { // 重新实现打击方法,打击后发出惨叫声
System.out.println("Snake叫了一声---嘶嘶嘶");
}
}
class Mouse extends Animal {
public Mouse() {
score = 1;
}
void beat() { // 重新实现打击方法,打击后发出惨叫声
System.out.println("Mouse叫了一声---吱吱吱");
}
}
class Animal {
public int score; // 分数
public int pos;
public Animal() {
score = 0;
pos = 0;
}
void beat() { // 打击方法
System.out.println("Animal叫了一声");
}
public void createNewPos() { // 随机生成新位置
pos = new Random().nextInt(10);
}
}
class HoleMap {
public int[] map; // 默认0 1老鼠 2蛇
public HoleMap() {
map = new int[10];
}
// 更新地图 type :1老鼠类 2蛇类
public void reflash(Animal animal) {
if (animal instanceof Mouse) {
for (int i = 0; i < map.length; i++) {
map[i] = 0;
}
map[animal.pos] = 1;// 老鼠类
} else if (animal instanceof Snake) {
for (int i = 0; i < map.length; i++) {
map[i] = 0;
}
map[animal.pos] = 2;// 蛇类
} else {
for (int i = 0; i < map.length; i++) {
map[i] = 0;
}
}
}
// 绘制地图
public void show() {
for (int b : map) {
System.out.print(b + " ");
}
System.out.println();
}
// 该函数判断是否打中, 返回打中得分,同时打中蛇就会减少player血量
public void hitSuccess(Player player, Animal animal) {
if (map[player.pos] == 1) { // 打中地鼠
System.out.println("打中老鼠---分数+1分");
animal.beat();
player.totalScore += animal.score;
} else if (map[player.pos] == 2) { // 打中蛇
System.out.println("打中蛇---血量-1滴---分数+3分");
animal.beat();
player.blood--;
player.totalScore += animal.score;
} else {
System.out.println("没打中---");
}
}
}
鼠蛇炸版
public class Game {
public static void main(String[] args) {
HoleMap holeMap = new HoleMap();
Mouse mouse = new Mouse();
Snake snake = new Snake();
Player player = new Player();
Beatable beat = null;
while (true) {
int kind = new Random().nextInt(10);
int type = 0; // 1鼠 2蛇 3炸 50% 30% 20%
if (kind < 5) {
beat = new Mouse();
type = 1;
} else if (kind < 8) {
beat = new Snake();
type = 2;
} else {
beat = new Bomb();
type = 3;
}
// 地图创造显示
holeMap.reflash(mouse.createNewPos(), type);
holeMap.show();
// 玩家输入
int hitPos = player.getHitPos();
// 打击判断
int result = holeMap.hitSuccess(hitPos);
if (result > 0) {
beat.beat();
}
switch (result) {
case 1: // 鼠
player.totalScore += mouse.score;
break;
case 2: // 蛇
player.totalScore += snake.score;
player.boold--;
break;
case 3: // 炸
player.boold = 0;
break;
default:
break;
}
System.out.println("你的得分为:" + player.totalScore + "\n你的血量为:" + player.boold);
if (player.boold <= 0) {
break;
}
System.out.println("-----------------------------------------");
}
}
}
class Player {
int hitpos; // 打击位置
int boold; // 血量
int totalScore; // 总得分
public Player() {
boold = 3;
hitpos = 0;
totalScore = 0;
}
public int getHitPos() {
System.out.println("请输入打击位置:");
hitpos = new Scanner(System.in).nextInt();
return hitpos;
}
}
class HoleMap {
int[] map;
public HoleMap() {
map = new int[10];
}
public void reflash(int newpos, int type) {
for (int i = 0; i < map.length; i++) {
map[i] = 0;
}
map[newpos] = type;
}
public void show() {
for (int b : map) {
if (b == 1) {
System.out.print("鼠 ");
} else if (b == 2) {
System.out.print("蛇 ");
} else if (b == 3) {
System.out.print("炸 ");
} else {
System.out.print("0 ");
}
}
System.out.println();
}
public int hitSuccess(int hitpos) {
if (map[hitpos] > 0) {
return map[hitpos];
}
return 0;
}
}
class Bomb implements Beatable {
@Override
public void beat() {
System.out.println("boooooom~~~GANE-OVER");
}
}
class Snake extends Animal implements Beatable {
public Snake() {
score = 3;
}
@Override
public void beat() {
System.out.println("Snake叫了一声---嘶嘶嘶");
}
}
class Mouse extends Animal implements Beatable {
public Mouse() {
score = 1;
}
@Override
public void beat() {
System.out.println("Mouse叫了一声---吱吱吱");
}
}
interface Beatable {
void beat();
}
class Animal {
public int score; // 分数
public int newpos;
public Animal() {
score = 0;
newpos = 0;
}
public int createNewPos() {
newpos = new Random().nextInt(10);
return newpos;
}
}
鼠蛇炸版升级
public class Game {
public static void main ( String[ ] args) {
HoleMap holeMap = new HoleMap ( ) ;
while ( true ) {
holeMap. generateObject ( ) ;
holeMap. reFlash ( ) ;
holeMap. show ( ) ;
int hitPos = holeMap. getHitPos ( ) ;
int blood = holeMap. hitSuccess ( hitPos) ;
if ( blood <= 0 ) {
break ;
}
System. out. println ( "-----------------------------------------" ) ;
}
}
}
class Player {
int blood;
int totalScore;
public Player ( ) {
blood = 3 ;
totalScore = 0 ;
}
public int getHitPos ( ) {
System. out. println ( "请输入打击位置:" ) ;
int hitPos = - 1 ;
while ( hitPos < 0 ) {
try {
hitPos = new Scanner ( System. in) . nextInt ( ) ;
if ( hitPos > 9 || hitPos < 0 ) {
throw new RuntimeException ( ) ;
}
} catch ( Exception e) {
System. out. println ( "输入有误,请重新输入!" ) ;
}
}
return hitPos;
}
}
class HoleMap {
private int [ ] map;
private Mouse mouse;
private Snake snake;
private Bomb bomb;
private int type;
private Player player;
private Beatable beat;
public HoleMap ( ) {
map = new int [ 10 ] ;
mouse = new Mouse ( ) ;
snake = new Snake ( ) ;
bomb = new Bomb ( ) ;
type = 0 ;
player = new Player ( ) ;
}
public void generateObject ( ) {
int kind = new Random ( ) . nextInt ( 10 ) ;
if ( kind < 5 ) {
beat = mouse;
type = 1 ;
} else if ( kind < 8 ) {
beat = snake;
type = 2 ;
} else {
beat = bomb;
type = 3 ;
}
}
public void reFlash ( ) {
if ( beat == null) {
throw new RuntimeException ( "还未生成属蛇炸,请先调用generateObject()方法" ) ;
}
for ( int i = 0 ; i < map. length; i++ ) {
map[ i] = 0 ;
}
map[ beat. createNewPos ( ) ] = type;
}
public void show ( ) {
for ( int b : map) {
if ( b == 1 ) {
System. out. print ( "鼠 " ) ;
} else if ( b == 2 ) {
System. out. print ( "蛇 " ) ;
} else if ( b == 3 ) {
System. out. print ( "炸 " ) ;
} else {
System. out. print ( "0 " ) ;
}
}
System. out. println ( ) ;
}
public int getHitPos ( ) {
return player. getHitPos ( ) ;
}
public int hitSuccess ( int hitPos) {
int result = map[ hitPos] ;
if ( result > 0 ) {
beat. beat ( ) ;
}
switch ( result) {
case 1 :
player. totalScore += ( ( Animal) beat) . score;
break ;
case 2 :
player. blood-- ;
player. totalScore += ( ( Animal) beat) . score;
break ;
case 3 :
player. blood = 0 ;
break ;
default :
System. out. println ( "没打中---" ) ;
break ;
}
System. out. println ( "你的得分为:" + player. totalScore + " 你的血量为:" + player. blood) ;
return player. blood;
}
}
class Bomb implements Beatable {
@Override
public void beat ( ) {
System. out. println ( "boooooom~~~GANE-OVER" ) ;
}
@Override
public int createNewPos ( ) {
return new Random ( ) . nextInt ( 10 ) ;
}
}
class Snake extends Animal implements Beatable {
public Snake ( ) {
score = 3 ;
}
@Override
public void beat ( ) {
System. out. println ( "Snake叫了一声---嘶嘶嘶" ) ;
}
@Override
public int createNewPos ( ) {
return new Random ( ) . nextInt ( 10 ) ;
}
}
class Mouse extends Animal implements Beatable {
public Mouse ( ) {
score = 1 ;
}
@Override
public void beat ( ) {
System. out. println ( "Mouse叫了一声---吱吱吱" ) ;
}
@Override
public int createNewPos ( ) {
return new Random ( ) . nextInt ( 10 ) ;
}
}
interface Beatable {
void beat ( ) ;
int createNewPos ( ) ;
}
class Animal {
public int score;
public Animal ( ) {
score = 0 ;
}
}