多维数组元素的遍历
class Test{
public static void main(String[] args) {
int[][] arr={{3,8,2,9},{2,7},{9,0,1,6}};
for(int x=0;x<arr.length;x++){
for(int y=0;y<arr[x].length;y++){
System.out.print(arr[x][y]+",");
}
System.out.println();
}
}
}
3,8,2,9,
2,7,
9,0,1,6,
写一个长方形类,里面有求周长和面积的功能。
然后在测试类中进行测试。
import java.util.Scanner;
class RectTest {
public static void main(String[] args) {
Rect r = new Rect();
Scanner sc = new Scanner(System.in);
System.out.println("请输入长方形的长:");
r.length = sc.nextInt();
System.out.println("请输入长方形的宽:");
r.width = sc.nextInt();
int zhouChang = r.getZhouChang();
int area = r.getArea();
System.out.println("该长方形的周长是:"+zhouChang);
System.out.println("该长方形的面积是:"+area);
}
}
class Rect {
//长
int length;
//宽
int width;
//求周长
public int getZhouChang() {
return (length + width)*2;
}
//求面积
public int getArea() {
return length * width;
}
}
产生一个1-100之间的随机数
(int)(Math.random()*100)+1
*/
class MathDemo
{
public static void main(String[] args)
{
//public static double random()
//返回带正号的 double 值,该值大于等于 0.0 且小于 1.0
double d = Math.random();
System.out.println(d);
//改进:我要获取的数据是1-100之间。
int number = (int)(d*100)+1;
System.out.println(number);
}
冒泡排序
1. public class SortTest {
2.
3. /**
4. * @param args
5. */
6. public static void main(String[] args) {
7. // TODO Auto-generated method stub
8. int[] a = { 62, 54, 68, 73, 99, 22, 46, 83, 22, 70 };
9. sortMethod1(a);
10. //sortMethod2(a);
11. }
12.
13. public static void sortMethod1(int[] s) {
14. int temp;
15. for (int i = 0; i < s.length - 1; i++) {
16. for (int j = i + 1; j < s.length; j++) {
17. if (s[i] > s[j]) {
18. temp = s[i];
19. s[i] = s[j];
20. s[j] = temp;
21. }
22. for (int k = 0; k < s.length; k++) {//输出动态
23. System.out.print(s[k] + ", ");
24. }
25. System.out.println(" ");
26. }
27. System.out.println(" ");
28. }
29. }
30.
31. public static void sortMethod2(int[] s) {
32. int temp;
33. for (int i = 1; i <= s.length; i++) {
34. for (int j = 0; j < s.length - 1; j++) {
35. if (s[j] > s[j + 1]) {
36. temp = s[j];
37. s[j] = s[j + 1];
38. s[j + 1] = temp;
39. }
40. for (int k = 0; k < s.length; k++) {//输出动态
41. System.out.print(s[k] + ", ");
42. }
43. System.out.println(" ");
44. }
45. System.out.println(" ");
46. }
47. }
48. }
|