7.3作业
程序:
package com.homework;
import java.util.Scanner;
public class newSuzhi {
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
int[] nums = new int[] {8,4,2,1,23,344,12};
System.out.println("请输入要猜的数字:");
int newNum = sc.nextInt();
int sum = 0;
boolean flag = false; //标记
for(int b : nums) {
if(b==newNum) {
flag =true;
break; //判断出便不再执行
}
}
System.out.println("数列依次是:");
for(int b :nums) {
sum += b;
System.out.print(b+"\t");
}
System.out.println();
System.out.println("序列之和为"+sum);
if(flag) { //标记开始起作用
System.out.println("你猜到了有数字"+newNum);
}else {
System.out.println("对不起,你么有猜到");
}
}
}
package com.homework;
import java.util.Scanner;
public class chaZhi {
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
int[] scores = new int[6];
scores[0] = 99;
scores[1] = 85;
scores[2] = 82;
scores[3] = 63;
scores[4] = 60;
System.out.print("请输入要插入的成绩:");
int newScore = sc.nextInt();
int def = scores.length-1;
//寻找要插入的位置
for(int a = 0;a <scores.length;a++) {
if(newScore > scores[a]) {
def = a;
break;
}
}
System.out.println("应该放入的下标是:"+def);
//开始挪位置
for(int b = scores.length-2;b >= def;b--) {
scores[b+1] = scores[b];
}
scores[def] = newScore;
System.out.println("插完后的顺序为:");
for(int v : scores) {
System.out.print(v);
System.out.print("\t");
}
}
}
习题3.1