2022年3月26日

编程入门 行业动态 更新时间:2024-10-12 03:24:26

2022年3月26日

2022年3月26日

案例-猜数字游戏
1.游戏:
系统随机产生一个0-100内的随机数,用户输入数字猜!
如果猜大了则提示猜大了,如果猜小了则提示猜小了
如果猜对了,则打印所猜分数和次数
如果中途退出则输-1
满分100分,猜错一次扣3分
游戏难度选择,难度以产生随机数的大小为准,分为3个等级:1(0-100)、2(0-1000)、3(0-10000)

public class GuessingPro {public static void main(String[] args) {System.out.println("进行游戏");System.out.println("请选择游戏难度:1(0-100)、2(0-1000)、3(0-10000)");Scanner scanner = new Scanner(System.in);int rank = scanner.nextInt();int scope=0;if(rank == 1)scope=100;else if(rank==2)scope = 1000;else if(rank == 3)scope = 10000;int result = (int)(Math.random()*scope);int count = 0;int score = 100;while (true){count ++;System.out.println("请输入你的数字:");System.out.println("退出游戏请输入-1");int num =scanner.nextInt();if(num == -1){System.out.println("您退出了游戏!");System.out.println("结果是:"+result+" 您猜了"+(count-1)+"次");break;}if(num == result){System.out.println("猜对了!你猜了 "+count+" 次"+"分数为:"+score);break;}if(num > result){System.out.println("猜大了");score -= 3;}if(num < result){System.out.println("猜小了");score -= 3;}}}

编写程序,用户输入杨辉三角形的行数,在控制台打印对应的杨辉三角形

  • eg: 输入了2
*    ** * **/
public class Triangle {public static void main(String[] args) {Scanner scanner = new Scanner(System.in);System.out.println("请输入三角形的行数:");int line = scanner.nextInt();for (int i = 1; i <=line ; i++) {for (int j = 0; j <line-i ; j++) {System.out.print(" ");}for (int j = 0; j <i*2-1 ; j++) {System.out.print("*");}System.out.println();}}
public class RandomArray {public static void main(String[] args) {Scanner scanner = new Scanner(System.in);System.out.println("输入数组的长度:");int length = scanner.nextInt();System.out.println("输入数组的范围:");int  scope = scanner.nextInt();int [] res =arrayRadom(length,scope);System.out.print("生成的数组:"+Arrays.toString(res));//Arrays.sort(res);System.out.println();res = arraySort(res);System.out.print("排序后:"+Arrays.toString(res));double average = avg(res);System.out.println();System.out.println("平均数为:"+average);prime(res);System.out.println();odd(res);}public static int [] arrayRadom(int length,int scope){int [] arr= new int[length];for (int i = 0; i <arr.length ; i++) {arr[i] = (int)(Math.random()*scope);}return arr;}public static int [] arraySort(int [] arr){for (int i = 0; i <arr.length ; i++) {for (int j = 0; j <arr.length-i-1 ; j++) {int temp;if(arr[j]>arr[j+1]){temp = arr[j];arr[j] = arr[j+1];arr[j+1] = temp;}}}return arr;}public  static double avg(int[] arr){double sum=0;double avg=0;for (int i = 0; i <arr.length ; i++) {sum+=arr[i];}avg= sum/arr.length;return avg;}public static void prime(int[] arr){System.out.print("质数有: ");int j=0;for (int i = 0; i <arr.length ; i++) {for (j = 2; j <arr[i] ; j++) {if(arr[i] % j == 0){break;}}if(arr[i] == j){System.out.print(arr[i]+" ");}}}public static void odd(int [] arr){System.out.print("奇数有:");for (int i = 0; i <arr.length ; i++) {if(arr[i]%2!=0){System.out.print(arr[i]+" ");}}}

更多推荐

2022年3月26日

本文发布于:2024-03-04 16:54:12,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1709841.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!