JAVA6次作业


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


 


 1 package G;
 2 
 3 public class G1 {
 4 
 5     public static void main(String[] args) {
 6         // TODO Auto-generated method stub
 7         int x=0;
 8         int y;
 9          for(y=1;y<=100;y++){
10              if(y%10!=3){
11                  x=x+y;
12              }
13          }
14          System.out.println("和为"+x);
15       }
16 
17   
18   
19     }

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

 

   *

  ***

 *****

*******

 

*****

****

***

**

*

 1 package G;
 2 
 3 public class G2 {
 4 
 5     public static void main(String[] args) {
 6         // TODO Auto-generated method stub
 7           int x,y,z;
 8           for(x=1;x<=4;x++){
 9                for(y=1;y<=4-x;y++){
10                  System.out.print(" ");            
11                }
12                for(z=1;z<=2*x-1;z++){
13                    System.out.print("*");
14                }
15                System.out.println();
16            }
17 
18         }
19 
20     
21     
22     }

 1 package G;
 2 
 3 public class G2 {
 4 
 5     public static void main(String[] args) {
 6         // TODO Auto-generated method stub
 7         int x,y,z;
 8         for(x=1;x<=5;x++){
 9             for(y=5;y<=5-x;y--){
10                 System.out.print(" ");            
11              }
12             for(z=x;z<=5;z++){
13                 System.out.print("*");
14              }
15              System.out.println();
16          }
17 
18       }
19 
20   
21   
22         }
23 
24     
25     

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

 1 package G;
 2 
 3 import java.util.Scanner;
 4 
 5 public class G3 {
 6 
 7     public static void main(String[] args) {
 8         // TODO Auto-generated method stub
 9         Scanner input=new Scanner(System.in);
10         System.out.println("输入学生姓名");
11         while(true){
12             String name=input.nextLine();
13             if(name.equals("Q")){
14                 System.out.println("程序结束");
15                 break;
16             }
17             System.out.println("输入学生姓名");
18         }
19       
20         }
21 
22     
23     
24     }

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

 1 package G;
 2 
 3 import java.util.Scanner;
 4 
 5 public class G4 {
 6 
 7     public static void main(String[] args) {
 8         // TODO Auto-generated method stub
 9         Scanner input=new Scanner(System.in);
10         System.out.println("输入学生成绩");
11        
12         int x=1;
13         int y=0;
14         while(x<=5){ 
15             int z=input.nextInt();
16             if(z<0||z>100){
17                 System.out.println("输入错误,从新输入");
18                 z=input.nextInt();
19             }
20             y=y+z;
21             x++;            
22             
23         }
24         System.out.println("总成绩为"+y);
25         }
26 
27     
28     
29 
30     }

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

 1 package G;
 2 
 3 public class G5 {
 4 
 5     public static void main(String[] args) {
 6         // TODO Auto-generated method stub
 7         double x=0;
 8         double y=0;
 9         double z=0;
10         for(z=1;z<11;z++){
11             y=y+z;
12             x=x+1/y;
13         }
14         System.out.println(x);
15         }
16 
17     
18     
19 
20     }

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

(判断猜的次数,如果1-3次,提示太棒了  如果5-8次 一般般  如果8次以上 太笨了)

 

 1 package G;
 2 
 3 import java.util.Random;
 4 import java.util.Scanner;
 5 
 6 public class G6 {
 7 
 8     public static void main(String[] args) {
 9         // TODO Auto-generated method stub
10         Random r=new Random();
11         int x=r.nextInt(100);
12         System.out.println(x);
13         try (Scanner input = new Scanner(System.in)) {
14             System.out.println("输入1-99之间数");
15             int y=0;
16             while(true){
17                 int z=input.nextInt();
18                 y++;
19             if(z==x){
20                     System.out.println("猜对了");
21                     break;
22             }else if(z>x){
23                 System.out.println("大了点");
24                 continue;
25             }else if(z<x){
26                 System.out.println("小了点");
27                 continue;
28             }
29             } 
30             
31             if(y>=1&&y<=3){
32                 System.out.println("太棒了");
33             } else if(y>=5&&y<=8){
34                 System.out.println("一般般");
35             }else if(y>=5&&y<=8){
36                 System.out.println("太笨了");
37             }
38         }
39 
40         }
41 
42     
43     
44 
45     }

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

 1 package G;
 2 
 3 import java.util.Scanner;
 4 
 5 public class G7 {
 6 
 7     public static void main(String[] args) {
 8         try (// TODO Auto-generated method stub
 9         Scanner input = new Scanner(System.in)) {
10             System.out.println("输入一个数");
11             int x=input.nextInt();
12             int y;
13             boolean z=true;
14             for (y=2;y<x-1;y++){
15                 if(x%y==0){
16                    z=false;
17             break;
18                 }        
19             }
20     if(z){
21              System.out.println("是质数");
22              
23    }else{
24              System.out.println("不是质数");
25  
26    }
27         }
28 
29         }

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

 1 package G;
 2 
 3 public class G8 {
 4 
 5     public static void main(String[] args) {
 6         // TODO Auto-generated method stub
 7         int x=1;
 8         int y=1;
 9         int z=0;
10         int a;
11          for (a= 3;a<21;a++){
12            z=x+y;
13            x=y;
14            y=z;   
15         }
16          System.out.println("第20项为:"+z);
17         }
18 
19     
20     
21     }

posted @ 2021-04-12 20:38  计算机1905geng  阅读(75)  评论(0编辑  收藏  举报