Java第六次作业
1.使用for循环计算1-100的和,除了以3结尾的那些数
package Homework; public class H1 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int sum=0; for(int i=1;i<=100;i++){ if(i%10!=3) sum+=i; } System.out.println(sum); } }
2.使用二重循环输出以下图形
package Homework; public class H2 { /** * @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 k = 1; k <= 2 * i - 1; k++) { System.out.print("*"); } System.out.println(); } System.out.println(); System.out.println(); System.out.println(); System.out.println(); for (int i = 1; i <= 5; i++) { for (int j = 1; j <= 6 - i; j++) { System.out.print("*"); } System.out.println(); } } }
3.循环输入学生姓名,直到输入‘Q’程序结束
package Homework; import java.util.Scanner; public class H3 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Scanner input = new Scanner(System.in); while (true) { System.out.println("输入学生姓名"); String a = input.next(); if (a.equals("Q")) { System.out.println("输入的名字有效"); break; } else System.out.println("输入的名字无效,请重新输入"); } } }
4.输入5个学生成绩,求和,当输入不合法的时候,提示输入错误重新输入。
package Homework; import java.util.Scanner; public class H4 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Scanner input = new Scanner(System.in); System.out.println("输入学生成绩"); int sum = 0; for (int i = 1; i <= 5; i++) { int grade = input.nextInt(); while (grade < 0 || grade > 100) { System.out.println("输入的成绩无效,重新输入"); grade = input.nextInt(); break; } sum += grade; } System.out.println(sum); } }
5.计算 1+ 1/(1+2)+ 1/(1+2+3)+ 1/(1+2+3+4) +…………+1/(1+2+....+10)
package Homework; public class H5 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub double sum = 0; double j = 0; for (int i = 1; i <= 10; i++) { sum += i; j = 1 / sum + j; } System.out.println(j); } }
作业
1.产生一个1-99的随机数,猜数字,若大了就提示“大了点”,若小了就提示“小了点”直到猜对位置
package Homework; import java.util.Scanner; public class H6 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Scanner input = new Scanner(System.in); int number = (int) (Math.random() * 100); System.out.println("我这里有一个0到99之间的整数,你猜是多少?"); int a = 0; do { int b = input.nextInt(); if (b < number) { System.out.println("小了点!"); a++; } else if (b > number) { System.out.println("大了点!"); a++; } else { System.out.println("猜对啦!"); a++; break; } } while (true); System.out.println("这个数字是" + number); System.out.println("您猜的次数是" + a); if (a >= 1 && a <= 3) { System.out.println("太棒了!"); } else if (a >= 5 && a <= 8) { System.out.println("一般般!"); } else { System.out.println("太笨了!"); } } }
2.输入一个数,判断是不是质数(只能被1和自身整除的数是质数)
package Homework; import java.util.Scanner; public class H7 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Scanner input = new Scanner(System.in); System.out.println("输入一个数"); int num = input.nextInt(); boolean isPrime = true; if (num == 1) { isPrime = false; } for (int i = 2; i <= num - 1; i++) { if (num % i == 0) { isPrime = false; break; } } if (isPrime) { System.out.println(num + "是质数"); } else { System.out.println(num + "不是质数"); } } }
3.求裴波那契数列的第20项是什么。裴波那契数列,1,1,2,3,5,8,13,21……
package Homework; public class H9 { /** * @param args */ 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(c); } System.out.println("所以这个数列第20项是:"+c); } }