6天
1.减肥计划if版
import java.util.Scanner;
public class ArrayDemo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入星期数显示今天的活动:");
int week = sc.nextInt();
if(week==1){
System.out.println("跑步");
}else if(week==2){
System.out.println("游泳");
}else ifweek==3){
System.out.println("慢走");
}else if(week==4){
System.out.println("动感单车");
}else if(week==5){
System.out.println("拳击");
}else if(week==6){
System.out.println("爬山");
}else if(week==7){
System.out.println("好好吃一顿");
}else{
System.out.println("你家有这个星期?");
}
}
}
2.减肥计划switch版:
import java.util.Scanner;
public class ArrayDemo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入星期数显示今天的活动:");
int week = sc.nextInt();
switch (week) {
case 1:
System.out.println("跑步");
break;
case 2:
System.out.println("游泳");
break;
case 3:
System.out.println("慢走");
break;
case 4:
System.out.println("动感单车");
break;
case 5:
System.out.println("拳击");
break;
case 6:
System.out.println("爬山");
break;
case 7:
System.out.println("好好吃一顿");
break;
default:
System.out.println("你家有这个星期?");
}
}
}
3.逢7过
public class ArrayDemo {
public static void main(String[] args) {
for(int i=1;i<=100;i++){
if(i % 10==7 || i % 7 ==0 || i/10%10 ==7){
System.out.println(i);
}
}
}
}
4.不死神兔
public class ArrayDemo {
public static void main(String[] args) {
int[] arr = new int[20];
arr[0] = 1;
arr[1] = 1;
for(int i = 2; i<arr.length ;i++){
arr[i] = arr[i-2]+arr[i-1];
}
System.out.println("第二十个月兔子的对数是:"+arr[19]);
}
}
5.百钱白鸡
public class ArrayDemo {
public static void main(String[] args) {
for(int x =0;x<=20;x++){
for(int y =0;y<=33;y++){
int z = 100-x-y;
if(z%3==0 && 5*x+3*y+z/3==100){
System.out.println("鸡翁"+x+"只"+","+"鸡母"+y+"只"+","+"鸡雏"+z+"只");
}
}
}
}
}
6.数组元素求和
public class ArrayDemo {
public static void main(String[] args) {
int[] arr = {68, 27, 95, 88, 171, 996, 51, 210};
int sum = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] / 10 % 10 != 7 && arr[i] % 10 != 7 && arr[i] % 2 == 0) {
sum += arr[i];
}
}
System.out.println(sum);
}
}
7.数组内容相同
public class ArrayDemo {
public static void main(String[] args) {
int[] arr = {10};
int[] arr2 = {10};
boolean getArray = array(arr,arr2);
System.out.println(getArray);
}
public static boolean array(int[] arr,int[] arr2){
if(arr.length !=arr2.length){
return false;
}
for(int i=0;i<arr.length;i++){
if(arr[i] != arr2[i]){
return false;
}
}
return true;
}
}
8.查找
import java.util.Scanner;
public class ArrayDemo {
public static void main(String[] args) {
int[] arr = {19,28,37,46,50};
int index =-1;
int getarray =array(index,arr);
System.out.println(getarray);
}
public static int array(int index,int[] arr){
Scanner sc = new Scanner(System.in);
System.out.println("请输入要查找的数据:");
int number = sc.nextInt();
for(int i=0;i<arr.length;i++){
if(arr[i]==number){
index = i;
break;
}
}
return index;
}
}
9反转
public class ArrayDemo {
public static void main(String[] args) {
int[] arr = {19,28,37,46,50};
for(int start = 0,end=arr.length-1;start<=end;start++,end--){
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
}
printArray(arr);
}
public static void printArray(int[] arr){
System.out.print("[");
for(int x=0;x<arr.length;x++){
if(x==arr.length-1){
System.out.print(arr[x]);
}else{
System.out.print(arr[x]+",");
}
}
System.out.print("]");
}
}
10.评委打分
import java.util.Scanner;
public class ArrayDemo {
public static void main(String[] args){
int[] arr = new int[6];
Scanner sc = new Scanner(System.in);
for(int x =0;x<arr.length;x++){
System.out.println("请输入第"+(x+1)+"个评委打分");
arr[x]= sc.nextInt();
}
int max = getMax(arr);
int min =getMin(arr);
int sum =getSum(arr);
int avg = (sum - max - min) / (arr.length-2);
System.out.println("平均分是:" +avg);
}
public static void printArray(int[] arr){
System.out.print("[");
for(int x=0;x<arr.length;x++){
if(x==arr.length-1){
System.out.print(arr[x]);
}else{
System.out.print(arr[x]+", ");
}
}
System.out.print("]");
}
public static int getMax(int[] arr){
int max = arr[0];
for(int x=1;x<arr.length;x++){
if(arr[x]>max){
max = arr[x];
}
}
return max;
}
public static int getMin(int[] arr){
int min = arr[0];
for(int x=1;x<arr.length;x++){
if(arr[x]<min){
min =arr[x];
}
}
return min;
}
public static int getSum(int[] arr){
int sum = 0;
for( int x = 0;x<arr.length;x++){
sum += arr[x];
}
return sum;
}
}
|