Java第六次作业

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

public class homework3 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        for (int i = 1; i <= 100; i++) {
            if (i % 3 == 0) {
                continue;
            }
            System.out.println(i);
        }
    }
}

 

2.

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

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

public class homework3 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        for (int i = 1; i <= 4; i++) {
            for (int a = 1; a <= 4 - i; a++) {
                System.out.print(" ");
            }
            for (int b = 1; b <= 2 * i - 1; b++) {
                System.out.print("*");
            }
            System.out.println();
        }

    }

}

 

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

public class homework3 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        for (int i = 1; i <= 5; i++) {

            for (int a = 1; a <= 6 - i; a++) {
                System.out.print("*");
            }
            System.out.println();
        }

    }

}

 

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

import java.util.Scanner;

public class homework3 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner input = new Scanner(System.in);
        System.out.println("请输入学生的姓名:");
        while (true) {
            String name = input.next();
            if ("Q".equals(name)) {
                System.out.println("程序结束");
                break;
            } else {
                System.out.println("继续输入");
                continue;
            }
        }

    }
}

 

 

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

 

import java.util.Scanner;

public class homework3 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner input = new Scanner(System.in);
        double sum = 0;
        System.out.println("请输入5个学生的成绩:");
        for (int i = 1; i <= 5; i++) {
            System.out.println("请输入第" + i + "个学生的成绩:");
            double a = input.nextDouble();
            if (a > 100 || a < 0) {

                System.out.println("提示:输入错误重新输入!");
                break;
            }
            sum += a;
        }
        System.out.println("输入5个学生的成绩和为" + sum);
    }

}

 

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

public class homework3 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        double a = 1;// 分字
        double b = 0;// 分母
        double sum = 0;
        for (int i = 1; i <= 10; i++) {
            b = b + i;
            sum = sum + a / b;
        }
        System.out.println(sum);
    }

}

 作业
1.产生一个1-99的随机数,猜数字,若大了就提示“大了点”,若小了就提示“小了点”直到猜对为止。

import java.util.Random;
import java.util.Scanner;

public class homework3 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner input = new Scanner(System.in);
        Random a = new Random();
        int b = a.nextInt(99);

        int i = 1;
        System.out.println("请猜一个数字(1~99):");
        for (i = 1; i <= 99; i++) {
            int num = input.nextInt();
            if (num > b) {
                System.out.println("大了点");
                continue;
            }
            if (num < b) {
                System.out.println("小了点");
                continue;
            }
            if (num == b) {
                System.out.println("猜对了");
                break;
            }
        }

    }

}

 

 2.输入一个数,判断是不是质数(只能被1和自身整除的数是质数)

import java.util.Scanner;

public class homework3 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner input = new Scanner(System.in);
        System.out.println("请输入一个数(该数大于1):");
        int num = input.nextInt();
        for (int i = 2; i <= num; i++) {
            if (num % i == 0 && num != i) {
                System.out.println(num + "不为质数,能被" + i + "整除");
                break;
            }

            else {
                System.out.println(num + "为质数!");
                break;
            }
        }
    }

}

 

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

public class homework3 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int a = 1, b = 1, c = 0;
        for (int i = 1; i <= 18; i++) {
            c = a + b;
            a = b;
            b = c;
        }
        System.out.println("裴波那契数列的第20项是:" + c);
    }

}

 

posted @ 2021-04-12 19:49  热血青年小刘  阅读(78)  评论(0编辑  收藏  举报