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

 

转载于:https://www.cnblogs.com/lev1/p/11133627.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值