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

package zy;

public class z6 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

         int sum=0;
         
            for (int i = 0; i < 101; i++) {
                int a=i/10;
                if(a!=3){
                    sum+=i;
                }
                 
                 
                 
            }
    System.out.println(+sum);
    }

}

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

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

package zy;

public class z6 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        for (int i = 1; i <=4; i++) {
             
            for (int j = 1; j <=4-i; j++) {
                System.out.print(" ");
                 
                 
            }
            for (int j = 1; j <=2*i-1; j++) {
                 
                System.out.print("*");
            }
                 
            System.out.print("\n");
        }
         
    }

}

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

package zy;

public class z6 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        for (int i = 1; i <=4; i++) {
             
             
            for (int j = 1; j <=5-i; j++) {
                 
                System.out.print("*");
            }
                 
            System.out.print("\n");
        }
         
    }

}

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

package zy;
import java.util.Scanner;
public class z6 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

          Scanner input=new Scanner(System.in);
            System.out.println("请输入姓名");
            String name=input.next();
            while(true){
                 name=input.next();
                 
                 
                 if(name.equals("Q")){
                     System.out.println("结束");
                 }
             
                 
                 
                 
            }
         
     
    }

}

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

package zy;
import java.util.Scanner;
public class z6 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

           Scanner input=new Scanner(System.in);
            int sum=0;
            for (int i = 0; i < 5; i++) {
                System.out.println("输入成绩");
                int a=input.nextInt();
                if(a>100||a<0){
                    System.out.println("重新输入");
                    a=input.nextInt();
                }
                sum+=a;
                 
            }
            System.out.println("成绩总和为"+sum);
    }

}

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

 

package zy;
import java.util.Scanner;
public class z6 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        double sum = 0;
        double a=0;
        double b=0;
        double i=1;
    while(i<11){
        a+=i;
        b=1.0/a;
        sum+=b;
        i++;
         
         
         
    }
    System.out.println(+sum);
    }

}

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

package zy;
import java.util.Random;
import java.util.Scanner;
public class z6 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Random r = new Random();
        int x = 1 + r.nextInt(98);
        Scanner input = new Scanner(System.in);
        System.out.println("请输入1~99的随机数");
        int s = input.nextInt();
        int a = 1;
        while (a >= 1) {
            if (s > x) {
 
                System.out.println("大了点");
                int j = input.nextInt();
                s = j;
                a++;
            } else if (s < x) {
                System.out.println("小了点");
                int j = input.nextInt();
                s = j;
                a++;
            } else {
                a++;
                if (a <= 3)
                    System.out.println("太棒了");
                if (a >= 4 || a <= 8)
                    System.out.println("一般般");
                if (a > 8)
                    System.out.println("太笨了");
                break;
            }
        }
    }

}


7.输入一个数,判断是不是质数(只能被1和自身整除的数是质数)
package zy;
import java.util.Scanner;
public class z6 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

         Scanner input=new Scanner(System.in);
            System.out.println("请输入大于1的数");
            int a=input.nextInt();
            boolean sizhishu=true;
            for (int i = 2; i < a-1; i++) {
                if(a%i==0){
                    sizhishu=false;
                    break;
                }
                 
            }
         if (sizhishu) {
             System.out.println("是质数");
             
        } else {
            System.out.println("不是质数");
     
        }
    }

}

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

package zy;

public class z6 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

         int a=1;
            int b=1;
            int c=0;
    for (int i = 2; i < 20; i++) {
         c=a+b;
        a=b;
        b=c;
         
         
    }
    System.out.println("第20项为"+c);
    }

}

 

posted on 2021-04-12 13:24  郝梦瑶  阅读(50)  评论(0编辑  收藏  举报