java程序设计题(速度,急用)3到,

1. 编写一个输出“Say Hello!”的程序。
2. .编写一个名为MathTest类,此类具有一个名为exchange成员方法,此成员方法内具有两个整数类型的本地变量,交换这个变量的值,当要交换两个变量的值时,通常的做法是定义一个临时变量,然后再进行交换,那么请在此成员方法中实现不用临时变量而交换两个变量的值(即不得声明除了这两个变量之外的第三个变量)

3.假设有整型数组{13,12,45,1,98,5,31},请用实现一个算法将这个数组进行排序,排序的结果为非递减,排序算法可以采用冒泡排序算法、快速排序算法、插入排序算法或选择排序算法中的任意一个。

1.
public class Hello{
public static void main(String[] args){
System.out.println("Say Hello!");
}
}
2.
public class MathTest{
public static void main(String[] args){
exchange(123,321);
}
public static void exchange(int a,int b){
a-=b;
b+=a;
a=b-a;
System.out.println(a+","+b);
}
}
3.
public class Sort{
public static void main(String[] args){
int[] a=new int[]{13,12,45,1,98,5,31};
for(int i=0;i<a.length-1;i++){
for(int j=a.length-2;j>=i;j--){
if(a[j]>a[j+1]){
int temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
for(int i=0;i<a.length;i++){
System.out.print(a[i]+" ");
}
System.out.println();
}
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2009-10-30
sayhello程序:

public class Test
{
public void sayHello() {
System.out.println("Say Hello");
}

public static void main(String [] args) {
Test t = new Test();
t.sayHello();
}
}

/**
* 不使用临时变量 交换俩数 你改改写在一个方法里
*/

public class Change2Number {

public static void main(String[] args) {

int a = 2;
int b = 3;

a = a ^ b;
b = a ^ b;
a = a ^ b;

System.out.println(a);
System.out.println(b);
}

}

public class Test
{

public static void main(String[] args) {
int temp;
int[] a = {13,12,45,1,98,5,31};
for(int j = a.length; j>1;j--) {
for(int i = 0; i<j; i++) {
if(a[i+1] <a[i]) {
temp = a[i];
a[i] = a[i+1];
a[i+1] = temp;
}
}
}

输出a就行了
}
}
相似回答
大家正在搜