4.9 第六次上机练习和作业
上机练习:
1.使用for循环计算1-100的和,除了以3结尾的那些数
1 package srgd; 2 public class word1 { 3 4 /** 5 * @param args 6 */ 7 public static void main(String[] args) { 8 // TODO Auto-generated method stub 9 int sum=0; 10 for (int i = 0; i <=100; i++) { 11 if (i%10!=3) { 12 sum+=i; 13 } 14 15 }System.out.println("和为"+sum); 16 } 17 18 }
2.使用二重循环输出以下图形
*
***
*****
*******
*****
****
***
**
*
1 package srgd; 2 public class word2 { 3 /** 4 * @param args 5 */ 6 public static void main(String[] args) { 7 // TODO Auto-generated method stub 8 9 for (int i = 1; i <=4; i++) { 10 for (int j = 1; j<=4-i; j++) { 11 System.out.print(" "); 12 } 13 for (int k = 1; k <=2*i-1; k++) { 14 System.out.print("*"); 15 }System.out.println(); 16 } 17 } 18 }
1 package srgd; 2 public class word2 { 3 /** 4 * @param args 5 */ 6 public static void main(String[] args) { 7 // TODO Auto-generated method stub 8 9 for (int i = 1; i <=5; i++) { 10 for (int j = 5; j>=i; j--) { 11 System.out.print("*"); 12 } 13 System.out.println(); 14 } 15 } 16 17 }
3.循环输入学生姓名,直到输入‘Q’程序结束。
String name=input.next();
if(name=='Q'){
}
错误。。字符串判断相等不能用==
if(name.equals("Q")){
}正确
zs
ls
ww
aa
bb
Q
程序结束
1 package srgd; 2 3 import java.util.Scanner; 4 5 public class word3 { 6 7 /** 8 * @param args 9 */ 10 public static void main(String[] args) { 11 // TODO Auto-generated method stub 12 Scanner input=new Scanner(System.in); 13 System.out.println("循环输入学生姓名"); 14 for(;;){ 15 String name=input.next(); 16 if(name.equals("Q")){ 17 break; 18 } 19 } 20 } 21 }
4.输入5个学生成绩,求和,当输入不合法的时候,提示输入错误重新输入。
1 package srgd; 2 3 import java.util.Scanner; 4 5 public class word4 { 6 /** 7 * @param args 8 */ 9 public static void main(String[] args) { 10 // TODO Auto-generated method stub 11 12 Scanner input=new Scanner(System.in); 13 int a=input.nextInt(); 14 int b=input.nextInt(); 15 int c=input.nextInt(); 16 int d=input.nextInt(); 17 int e=input.nextInt(); 18 while(a<0||a>100||b<0||b>100||c<0||c>100||d<0||d>100||e<0||e>100){ 19 a=input.nextInt(); 20 b=input.nextInt(); 21 c=input.nextInt(); 22 d=input.nextInt(); 23 e=input.nextInt(); 24 System.out.println("重新输入成绩:"); 25 } 26 System.out.println("五人成绩和为:"+(a+b+c+d+e)); 27 } 28 29 }
5.计算 1+1/(1+2) + 1/(1+2+3) +1/(1+2+3+4)+…………+1/(1+2+....+10)
1 package srgd; 2 3 public class dhdt { 4 5 /** 6 * @param args 7 */ 8 public static void main(String[] args) { 9 // TODO Auto-generated method stub 10 double a=0; 11 double sum=0; 12 for (int i = 1; i <=10; i++) { 13 a+=i; 14 sum+=1/a; 15 } 16 System.out.println(sum); 17 } 18 }
作业
1.产生一个1-99的随机数,猜数字,若大了就提示“大了点”,若小了就提示“小了点”直到猜对为止。
(判断猜的次数,如果1-3次,提示太棒了 如果5-8次 一般般 如果8次以上 太笨了)
1 import java.util.Random; 2 import java.util.Scanner; 3 public class Test { 4 5 /** 6 * @param args 7 */ 8 public static void main(String[] args) { 9 // TODO Auto-generated method stub 10 11 Scanner input=new Scanner(System.in); 12 System.out.println("猜数"); 13 int x=input.nextInt(); 14 Random r=new Random(); 15 int a=r.nextInt(99)+1; 16 int X=0; 17 while(x!=a){ 18 if (x>a) { 19 System.out.println("大了点"); 20 } 21 else if (x<a) { 22 System.out.println("小了点"); 23 } 24 x=input.nextInt(); 25 X++; 26 } 27 if (x==a) { 28 29 } 30 if (X<=3) { 31 System.out.println("太棒了"); 32 } 33 else if (X>3&&X<=8) { 34 System.out.println("一般般"); 35 } 36 else { 37 System.out.println("太笨了"); 38 } 39 } 40 41 }
2.输入一个数,判断是不是质数(只能被1和自身整除的数是质数)
1 package fgncfgj; 2 import java.util.Scanner; 3 public class dyjtgj { 4 5 /** 6 * @param args 7 */ 8 public static void main(String[] args) { 9 // TODO Auto-generated method stub 10 11 Scanner input=new Scanner(System.in); 12 System.out.println("输入一个大于1的数"); 13 int a=input.nextInt(); 14 while(a<=1){ 15 System.out.println("输入错误,重新输入"); 16 a=input.nextInt(); 17 } 18 boolean ZhiShu= true; 19 for (int i = 2; i <=a/2; i++) { 20 if (a%i==0) { 21 ZhiShu=false; 22 break; 23 } 24 } 25 System.out.println(a+"是质数"); 26 } 27 28 }
3.求裴波那契数列的第20项是什么。裴波那契数列,1,1,2,3,5,8,13,21……
1 package fgncfgj; 2 import java.util.Scanner; 3 public class dryjm { 4 /** 5 * @param args 6 */ 7 public static void main(String[] args) { 8 // TODO Auto-generated method stub 9 10 Scanner input=new Scanner(System.in); 11 int a=1; 12 int b=1; 13 int num=b; 14 for (int sum = 3; sum <=20; sum++) { 15 num=a+b; 16 a=b; 17 b=num; 18 } 19 System.out.println("第20项为"+num); 20 } 21 22 }