第六次作业


1.使用for循环计算1-100的和,除了以3结尾的那些数

package School.Day7;

public class Test01 {
    public static void main(String[] args){
        int sum = 0;
        for(int i = 1;i<=100;i++){
            if(i%10 != 3){
                sum = sum + i;
            }
        }
        System.out.println(sum);
    }
}

  

2.使用二重循环输出以下图形


*
***
*****
*******


*****
****
***
**
*

package School.Day7;

public class Test02 {
    public static void main(String[] args){
        for(int i = 1;i <= 4;i++){
            for(int y = 4 - i;y > 0; y--){
                System.out.print(" ");
            }
            for(int z = 1; z <= (2*i)-1; z++){
                System.out.print("*");
            }
            System.out.println();
        }
        System.out.println("===========");
        for (int i = 5;i >= 1;i--){
            for (int y = i;y >= 1; y--){
                System.out.print("*");
            }
            System.out.println();
        }
    }
}

  

3.循环输入学生姓名,直到输入‘Q’程序结束。

package School.Day7;

public class Test03 {
    public static void main(String[] args){
        java.util.Scanner s = new java.util.Scanner(System.in);
        while(true){
            System.out.print("输入名字: ");
            String i = s.next();
            if ("Q".equals(i)){
                System.out.println("程序结束");
                System.exit(0);
            }
        }
    }
}

  


4.输入5个学生成绩,求和,当输入不合法的时候,提示输入错误重新输入。

package School.Day7;

import java.util.Scanner;
public class Test04 {
    public static void main(String[] args){
        Scanner s = new Scanner(System.in);
        int i = 0;
        int sum = 0;
        while(i <= 4){
            System.out.print("输入" + (i+1) + "号成绩: ");
            int x = s.nextInt();
            if (x >100 || x < 0){
                System.out.println("成绩不合法,请重新输入!");
            }else{
                sum = sum + x;
                i++;
            }
        }
        System.out.println("成绩总和是: " + sum);
    }
}

  

5.计算 1+1/(1+2) +  1/(1+2+3)  +1/(1+2+3+4)+…………+1/(1+2+....+10)

package School.Day7;

public class Test05 {
    public static void main(String[] args){
        double sum = 0;
        int j = 0;
        for (int i = 1;i <= 10; i++){
            for (int y = 1; y <= i; y++){
                j = j + y;
            }
            sum = sum + (1.0/j);
            j = 0;
        }
        System.out.println("sum = " + sum);
    }
}

  

6.产生一个1-99的随机数,猜数字,若大了就提示“大了点”,若小了就提示“小了点”直到猜对为止。
(判断猜的次数,如果1-3次,提示太棒了 如果5-8次 一般般 如果8次以上 太笨了)

package School.Day7;
import java.util.Random;
import java.util.Scanner;
public class Test06 {
    public static void main(String[] args) {
        Random r = new Random();
        int x = r.nextInt(98) + 1;
        Scanner s = new Scanner(System.in);
        int c = 0;
        while(c >= 0){
            System.out.print("第" +(c+1)+ "次猜数字: ");
            int i = s.nextInt();
            if (i > x) {
                System.out.println("数字大了!");
                c++;
            }
            else if (i < x) {
                System.out.println("数字小了!");
                c++;
            }else{
                System.out.println("恭喜您!猜对了!");
                c++;
                break;
            }
        }
        if (c <= 4) {
            System.out.println("太棒了!");
        }
        else if (c <= 8) {
            System.out.println("一般般!");
        }else{
            System.out.println("太笨了!");
        }
    }
}

  

7.输入一个数,判断是不是质数

package School.Day7;
import java.util.Scanner;
public class Test07 {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        System.out.print("输入一个数: ");
        int x = s.nextInt();
        if (x <= 1) {
            System.out.println(x + "不是质数");
        }
        else if (x == 2) {
            System.out.println(2 + "是质数");
        }else{
            for (int i = x-1; i > 1; i--) {
                if (x%i == 0) {
                    System.out.println(x + "不是质数");
                    return;
                }
            }
            System.out.println(x + "是质数");
            return;
        }
    }
}

  

8.求裴波那契数列的第20项是什么。裴波那契数列,1,1,2,3,5,8,13,21……

package School.Day7;

public class Test09 {
    public static void main(String[] args) {
        int x = 0;
        int y = 0;
        int k = 0;
        for (int i = 1; i <= 19 ; i++) {
            if (i == 1 || i ==2) {
                x = 1;
                y = 1;
            }
            k = x + y;
            x = y;
            y = k;
        }
        System.out.println(k);
    }
}

  


}


posted @ 2021-04-09 16:43  李芊  阅读(63)  评论(0编辑  收藏  举报