今天是周末 但还是写了好几个程序:
1、键盘输入,算出同学们的平均数
public class Array_AverageScores {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scanner=new Scanner(System.in);
System.out.println("输入班级学生的数量:");
int count=scanner.nextInt();
int []a=new int[count+1];
for (int i = 0; i <count; i++) {
System.out.println("请输入第"+(i+1)+"个同学的成绩");
int score=scanner.nextInt();
a[i]=score;
// System.out.println(a[i]);
}
int b=0;
//由大到小排序
for (int i = 0; i < count; i++) {
for (int j = 0; j < count; j++) {
if (a[j]<a[j+1]){
b=a[j];
a[j]=a[j+1];
a[j+1]=b;
}
}
}
System.out.print("由da到xiao排序:");
for (int i = 0; i <count; i++) {
System.out.print(a[i]+" ");
}
System.out.println(" ");//换行
float AverageScores,Sum=0;//累加
for (int i = 0; i < count; i++) {
Sum+=a[i];
}
AverageScores=Sum/count;
System.out.println("平均成绩为:"+AverageScores);
}
2、找最大最小值:
int max=0,min=0;
int []num={23,45,13,47,5,98,5,32,34,54};for (int i = 0; i < 10; i++) {
if(num[i]>num[max]){
max=i;
}
if (num[min]>num[i]) {
min=i;
}
}
for (int i = 0; i < 9; i++) {
}
System.out.println("最小值为:"+num[min]);
System.out.print("最大值为:"+num[max]);
}
3、猜数字
public static void main(String[] agrs){
Scanner scanner=new Scanner(System.in);
System.out.println("猜猜是什么数字?");
for (int j = 0; j <10 ; j++) {
int i=scanner.nextInt();
if(i==5){
System.out.println("猜对了");
break;
}
System.out.println("还有"+(9-j)+"次机会~");
}
}
4、彩票33选7:开始还想用两个数组,其实一个就可以了,多设点条件
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
// int a[]=new int [33];//给数组a赋值
int b[]=new int [8];//8个空间 给出7个装数值
// for (int i = 0; i < 33; i++) {
// a[i]=i+1;
// }
// System.out.println(a[0]+"~"+a[32]);
System.out.println("请在1~33之间选择7个不同的数:");
for (int i = 0; i <7; i++) {
int x=scanner.nextInt();
b[i]=x;
if(b[i]>33||b[i]<1) {//判断范围
System.out.println("请输入1到33之间的数!!");
break;
}
}
//判断重复
for (int j = 0; j < b.length-1; j++) {
if(b[j]>33||b[j]<1) {break;}
for (int j2 = 0; j2 < j; j2++) {
if(b[j]==b[j2]){
System.out.println("您选的第"+(j2+1)+"和"+(j+1)+"个数字重复了!!");
break;
}else{
continue;
}
}
}
System.out.println("选的数字为:");
for (int i = 0; i < b.length-1; i++) {
System.out.print(+b[i]+" ");
}
5、生成九位随机密码,有数字大小写字母,三种,自己写了个龙凤虎式的
public static void main(String[] args) {
// TODO Auto-generated method stub
String Password="";
//生成 数字的随机串
for (int i = 0; i < 3; i++) {
int a=new java.util.Random().nextInt(10);
a+=48;
Password=Password+(char)a;
}
//生成 大写字母随机串
for (int j = 0; j < 3; j++) {
int b=new java.util.Random().nextInt(26);
b+=65;
Password=Password+(char)b;
}
//生成 小写字母随机字符串
for (int k = 0; k < 3; k++) {
int c=new java.util.Random().nextInt(26);
c+=97;
Password=Password+(char)c;
}
System.out.println("新密码为:");
System.out.println(Password);
}
感觉刚刚开始学习代码,写得好慢啊,什么时候才能写好呢?加油吧!