JAVA第六周作业0409
package com.homework6; //1.使用for循环计算1-100的和,除了以3结尾的那些数 public class Exe6_1_1 { public static void main(String[] args) { int sum =0; for (int i = 0; i <= 100; i++) { if (i %10 ==3) { continue; } sum +=i; } System.out.println(sum); } }
package com.homework6; /*2.使用二重循环输出以下图形 * *** ***** ******* ***** **** *** ** * */ public class Exe6_1_2 { public static void main(String[] args) { for (int i = 1; i <= 4; i++) { for (int k = 1; k <= 4-i; k++) { System.out.print(" "); } for (int j = 1; j <= 2*i-1; j++) { System.out.print("*"); } System.out.println(); } System.out.println("_____________________________________\n"); for (int i = 1; i <= 5; i++) { for (int j = 1; j <= 6-i; j++) { System.out.print("*"); } System.out.println(); } } }
package com.homework6; //3.循环输入学生姓名,直到输入‘Q’程序结束。 import java.util.Scanner; public class Exe6_1_3 { public static void main(String[] args) { Scanner input =new Scanner(System.in); System.out.println("请输入学生姓名:"); do{ String name =input.next(); if (name.equals("Q")) { System.out.println("已退出!"); break; } System.out.println(name +"\n录入成功,请继续输入,按Q可退出程序"); }while (true); } }
package com.homework6; //4.输入5个学生成绩,求和,当输入不合法的时候,提示输入错误重新输入。 import java.util.Scanner; public class Exe6_1_4 { public static void main(String[] args) { Scanner input =new Scanner(System.in); int sum =0; for (int i = 1; i <= 5; ) { System.out.println("请输入第"+i+"个学生成绩:"); int x =input.nextInt(); if (x >100 || x <0) { System.out.println("输入不合法,请重新输入:"); }else{ sum +=x; i++; } } System.out.println("成绩总和为:"+sum); } }
package com.homework6; //5.计算 1+1/(1+2) + 1/(1+2+3) +1/(1+2+3+4)+…………+1/(1+2+....+10) public class Exe6_1_5 { public static void main(String[] args) { double sum =0; int x =0; for (int i = 1; i <= 10; i++) { x +=i; sum +=(1.0/x); } System.out.println(sum); } }
package com.homework6; import java.util.Random; import java.util.Scanner; /*1.产生一个1-99的随机数,猜数字,若大了就提示“大了点”, 若小了就提示“小了点”直到猜对为止。 (判断猜的次数,如果1-3次,提示太棒了 如果5-8次 一般般 如果8次以上 太笨了)*/ public class Exe6_2_1 { public static void main(String[] args) { Random r = new Random(); Scanner input = new Scanner(System.in); System.out.println("猜数字0~100:"); int luck = r.nextInt(100); int i = 1; for (; i <= 100; i++) { System.out.println("请输入:"); int guess = input.nextInt(); if (luck < guess) { System.out.println("大了点!"); }else if(luck > guess){ System.out.println("小了点!"); }else{ System.out.println("恭喜你,猜对了!"); break; } } System.out.println(i + "次猜对:"); if (i <= 3) { System.out.println("太棒了!"); }else if (4 <= i && i<= 6) { System.out.println("一般般!"); }else{ System.out.println("你太笨了!"); } } }
package com.homework6; import java.util.Scanner; //2.输入一个数,判断是不是质数(只能被1和自身整除的数是质数 public class Exe6_2_2 { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("请输入一个数,判断它是不是质数:"); int luck = input.nextInt(); boolean r = true; if(luck==0||luck==1){ r = false; System.out.println(luck + "不是质数"); } for (int i = 2; i < luck; i++) { if(luck %i ==0){ System.out.println(luck + "不是质数"); r = false; break; } } if (r) { System.out.println(luck + "是质数"); } } }
package com.homework6; //3.求裴波那契数列的第20项是什么。裴波那契数列,1,1,2,3,5,8,13,21…… public class Exe6_2_3 { public static void main(String[] args) { int a=1; int b=1; int c=2; for (int i = 3; i <= 20; i++) { c = a+b; a = b; b = c; } System.out.println("第20项是" + c); } }