java笔试手写代码 java面试手写算法( 二 )


}
public static void buddleSort(int[] arr) {
System.out.println("排序前的结果为:"
+Arrays.toString(arr));
for(int j=0;j=arr.length-2;j++){
for(int i=0;i=arr.length-2-j;i++){
System.out.println("第"+i+"次交换前:"
+Arrays.toString(arr));
if(arr[i]arr[i+1]){
int tmp=arr[i];
arr[i]=arr[i+1];
arr[i+1]=tmp;
}
System.out.println("第"+i+"次交换后:"
+Arrays.toString(arr));
}
System.out.println("排序的结果为:"
+Arrays.toString(arr));
}
}
public static void choiceSort(
int[] arr) {
for(int i=0;i=arr.length-2;i++){
for(int j=i+1; j=arr.length-1;
j++){
System.out.println("第"+
j+"次交换前:"+Arrays.toString(arr));
if(arr[i]arr[j]){
int tmp=arr[j];
arr[j]=arr[i];
arr[i]=tmp;
}
System.out.println("第"+
j+"次交换后:"+Arrays.toString(arr));
}
}
System.out.println("排序的结果为:"
+Arrays.toString(arr));
}
}
java编程题 , 自己觉得又点难(求高手写代码)//Color类
public class Color {
private String colorName;
final public void setColor(String color){
this.colorName = color;
}
public String getColor(){
return this.colorName;
}
}
//White类
public class White extends Color{
public White(){
this.setColor("white");
}
public String getColor() {
return super.getColor();
}
}
//Red类
public class Red extends White{
public Red(){
this.setColor("Red");
}
}
//Prism类
public class Prism {
static public void activePrism(Color c){
if(c.getColor().equals("white")){
Red r = new Red();
Blue b = new Blue();
System.out.println(r.getColor());
System.out.println(b.getColor());
}
else{
return;
}
}
}
//测试类
public class ColorTest {
public static void main(String[] args) {
White w = new White();
Prism.activePrism(w);
}
}
其他颜色自己写吧 。
求Java高手写道题import java.util.*;
public class Test24 {
public static void main(String[] args) {
int year1, month1, day1;
int num;
Scanner scan = new Scanner(System.in);
MyDate d1, d2;
System.out.println("请输入一个日期");
System.out.print("年java笔试手写代码:");
year1 = scan.nextInt();
System.out.print("月:");
month1 = scan.nextInt();
System.out.print("日:");
day1 = scan.nextInt();
try{
d1 = new MyDate(year1, month1, day1);
System.out.println(d1.toString() + " 是一个合法日期");
}
catch(Exception e){
System.out.println(" 非法日期");
}
System.out.println("请输入一个日期");
System.out.print("年:");
year1 = scan.nextInt();
System.out.print("月:");
month1 = scan.nextInt();
System.out.print("日:");
day1 = scan.nextInt();
System.out.println("时间间隔:");
num = scan.nextInt();
try{
d1 = new MyDate(year1, month1, day1);
d2 = d1.dateAdd(num);
System.out.println(d1.toString() + " 在" + num + "天后就是 " + d2.toString());
}
catch(Exception e){
System.out.println(" 非法日期");
}
try{
d1 = new MyDate(2009, 2, 27);
d2 = new MyDate(2011, 3, 1);
num = d1.dateDiff(d1, d2);
System.out.println(d1.toString() + " ~ " + d2.toString() + " 相隔 " + num + " 天");
}
catch(Exception e){
System.out.println(" 非法日期");
}
}
}
class MyDate{
private int year;
private int month;
private int day;
private int[] days = new int[]{31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

推荐阅读