• 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+" ");
    }
    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(" ");
    }
    }
    }

     

     习题3.1

  • 相关阅读:
    Day015 PAT乙级 1013 数素数
    Day014 PAT乙级 1012 数字分类
    Day013 PAT乙级 1007 素数对猜想
    Day012 PAT乙级 1005 继续(3n+1)猜想
    Day011 PAT乙级 1003 我要通过
    Day010 PAT乙级 1002 写出这个数
    Day009 洛谷 P5707 上学迟到
    Day008 洛谷 P2181 对角线
    Day007 Java异常处理
    Fetch()
  • 原文地址:https://www.cnblogs.com/lev1/p/11133627.html
Copyright © 2020-2023  润新知