1.
package com.day05; import java.util.*; public class Demo1 { static Scanner sc = new Scanner(System.in); public static void main(String[] args) { int[] nums = new int[]{8,4,2,1,23,344,12}; int sum = 0; boolean ak = false; System.out.println("请猜第一个数字:"); int guess = sc.nextInt(); for(int num : nums){ if(num==guess){ ak = true; break; } } System.out.println("数列依次是:"); for (int num : nums) { sum += num; System.out.print(num+" "); } System.out.println(); System.out.println("数列之和是:"+sum); if(ak){ System.out.println("你猜到了有数字"+guess); }else{ System.out.println("你没猜到"); } } }
2.
package com.day05; import java.util.*; public class Demo2 { static Scanner sc = new Scanner(System.in); public static void main(String[] args) { int[] scores = new int[6]; scores[0]=99; scores[1]=92; scores[2]=85; scores[3]=63; scores[4]=60; System.out.println("请输入插入的成绩:"); int newScore = sc.nextInt(); int index = -1; for (int i = 0; i < scores.length; i++) { if(newScore>scores[i]){ index = i; break; } } System.out.println("应该放入的下标是:"+index); for (int i = scores.length - 2; i >= index; i--) { scores[i+1]=scores[i]; } scores[index] = newScore; for (int i : scores) { System.out.println(i); } } }
3.
package com.day05; import java.util.*; public class Demo3 { static Scanner sr= new Scanner(System.in); public static void main(String[] args) { System.out.println("请输入四家店的价格"); int price [] = new int[4]; for(int i = 0;i<price.length;i++){ System.out.println("第"+(i+1)+"家店的价格是:"); price[i] = sr.nextInt(); } int min=price[0]; for(int i = 0;i<price.length;i++){ if(min>price[i]){ min=price[i]; } } System.out.println("最低价格是:"+min); } }
4.
package com.day05; import java.util.*; public class Demo4 { static Scanner sr= new Scanner(System.in); public static void main(String[] args) { System.out.println("请输入一个整数(输入0时程序结束):"); int mun = sr.nextInt(); int max =mun; int min =mun; while(mun!=0){ System.out.println("请输入一个整数(输入0时程序结束):"); mun = sr.nextInt(); if(mun==0){ break; } if(mun<min){ min = mun; } if(mun>max){ max = mun; } } System.out.println("最小值为:"+min+" "+"最大值为:"+max); } }