输出语句:
System.out.println("内容");//输出内容并换行
System.out.print("内容");//输出内容不换行
System.out.println(); //起到换行的作用
今日小练习:
求出1到100所有含5的和是5的倍数的所有数
package com.company.www;
public class Main {
public static void main(String[] args) {
for(int j=1;j<100;j++){
if(j%5==0||j/10%10==5||j%10==5)
//j%5==0是求出5的倍数
//j/10%10==5是求出十位数含5
//j%10==5是求出个位数含5的
System.out.print(j+" ");
}
}
}
程序运行结果为:5 10 15 20 25 30 35 40 45 50 51 52 53 54 55 56 57 58 59 60 65 70 75 80 85 90 95
百钱百鸡:鸡翁一值钱五,鸡母一值钱三,鸡雏三值钱一,百钱买百鸡,问鸡翁,鸡母,鸡雏各几何?
package com.company.www;
public class Main {
public static void main(String[] args) {
int x,y,z;
//先设x,y,z分别为鸡翁,鸡母,鸡雏的数量
for (x=0;x<20;x++){
//鸡翁数量最多为20
for (y=0;y<33;y++){
//鸡母数量最多为33
z=100-x-y;
//鸡雏数量为100减去鸡翁和鸡母的数量
if (z%3==0&&5*x+3*y+z/3==100){
System.out.println("鸡翁数量为:"+x+" 鸡母数量为:"+y+" 鸡雏数量为"+z);
}
}
}
}
}
程序运行结果如下
鸡翁数量为:0 鸡母数量为:25 鸡雏数量为75
鸡翁数量为:4 鸡母数量为:18 鸡雏数量为78
鸡翁数量为:8 鸡母数量为:11 鸡雏数量为81
鸡翁数量为:12 鸡母数量为:4 鸡雏数量为84
冒泡排序:
-
比较相邻的元素。如果第一个比第二个大,就交换他们两个。
-
先让第一个数与其他没排好顺序的数比较,然后交换,然后是第二个数和其他没排好顺序的数比较然后交换,依次比较就能实现排序
package com.company.www; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int [] arr=new int[5]; for(int i=0;i<5;i++){ //for循环输入数据到数组中 arr[i]=sc.nextInt(); } for (int i = 0; i < arr.length - 1; i++) { for (int j = 0; j < arr.length - i -1; j++) { //每一趟遍历,将一个最大的数移到序列末尾 if (arr[j] > arr[j + 1]) { int temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } for (int a:arr){ System.out.print(a+" "); } } }输入数字及排序结果
3
2
8
1
5
1 2 3 5 8



