编程实现
自由落体,100m高,每次返回一半高度,弹跳10次,求经过的路程?小球最后高度?
package com.fan.test;
public class GameDrop {
public static void main(String[] args) {
double height =100;
drop(height);
}
public static void drop(double height){
double sum =height;
for (int i = 0; i < 10; i++) {
height/=2;
if(i == 9){
sum = sum + height;
}else{
sum = sum + height*2;
}
}
System.out.println("小球经过的路程:"+sum);
System.out.println("小球最后的高度:"+height);
}
}
编程实现:字符串反转
- 这里我们借助StringBuffer的reserve()方法实现字符串倒转
package com.fan.test.String.StringBuffer;
public class ReverseDemo {
public static void main(String[] args) {
String str = "fanwenqian";
System.out.println(reverseStr(str));
}
public static String reverseStr(String str){
StringBuffer sbf = new StringBuffer(str);
return sbf.reverse().toString();
}
}
字符串转int类型
int a =Integer.parseInt("string");
冒泡排序算法
实现数组从小到大排列
package com.fan.test.sort;
import java.util.Arrays;
public class DemoSort {
public static void main(String[] args) {
//冒泡排序算法
int[] numArrary = new int[]{1,5,8,7,2,3,9,4};
for (int i = 0; i < numArrary.length-1; i++) {
for (int j = 0; j < numArrary.length-1-i; j++) {
if(numArrary[j]>numArrary[j+1]){
int temp = numArrary[j];
numArrary[j] = numArrary[j+1];
numArrary[j+1] = temp;
}
}
}
System.out.println(Arrays.toString(numArrary));
}
}