第六次作业
1.使用for循环计算1-100的和,除了以3结尾的那些数
package test6; public class a { /** * @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=" + sum); } }
2.使用二重循环输出以下图形
*
***
*****
*******
***
*****
*******
*****
****
***
**
*
package test6; public class b { /** * @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("======================="); for (int i = 1; i <= 5; i++) { for (int j = 5; j >= i; j--) { System.out.print("*"); } System.out.println(); } } }
3.循环输入学生姓名,直到输入‘Q’程序结束。
package test6; import java.util.Scanner; public class c { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Scanner input = new Scanner(System.in); System.out.println("输入学生姓名"); for (;;) { String name = input.next(); if (name.equals("Q")) break; } System.out.println("end"); } }
4.输入5个学生成绩,求和,当输入不合法的时候,提示输入错误重新输入。
package test6; import java.util.Scanner; public class d { /** * @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 = 0;;) { int grade = input.nextInt(); if (grade >= 0 && grade <= 100) { sum += grade; i++; } else { System.out.println("输入错误重新输入:"); grade = input.nextInt(); if (grade >= 0 && grade <= 100) { sum += grade; i++; } } if (i == 5) { break; } } System.out.println("sum=" + sum); } }
5.计算 1+1/(1+2) + 1/(1+2+3) +1/(1+2+3+4)+…………+1/(1+2+....+10)
package test6; public class e { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub double sum = 0; double a = 0; for (int i = 1; i <= 10; i++) { a += i; sum += 1 / a; } System.out.println("sum=" + sum); } }
1.产生一个1-99的随机数,猜数字,若大了就提示“大了点”,若小了就提示“小了点”直到猜对为止。
(判断猜的次数,如果1-3次,提示太棒了 如果5-8次 一般般 如果8次以上 太笨了)
package test6; import java.util.Random; import java.util.Scanner; public class f { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Random r = new Random(); int number = r.nextInt(99) + 1; int s = 0; while (true) { Scanner input = new Scanner(System.in); System.out.println("请输入你要猜的数据(1-100):"); int guessNumber = input.nextInt(); if (guessNumber > number) { System.out.println("您猜的数字" + guessNumber + "大了"); s++; } else if (guessNumber < number) { System.out.println("您猜的数字" + guessNumber + "小了"); s++; } else { System.out.println("恭喜您猜中了!"); s++; if (s >= 1 && s <= 3) { System.out.println("太棒了"); } else if (s >= 4 && s <= 8) { System.out.println("一般般"); } else System.out.println("太笨了"); } } } }
2.输入一个数,判断是不是质数(只能被1和自身整除的数是质数)
package test6; import java.util.Scanner; public class g { /** * @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(); int i = 2; for (; num % i != 0 && i * i < num; i++) ; if (i * i > num) { System.out.println("是质数"); } else { System.out.println("不是质数"); } System.out.println("=============="); for (; num % i != 0; i++) ; if (i != num) { System.out.println("该数不是质数"); } else { System.out.println("该数是质数"); } } }
3.求裴波那契数列的第20项是什么。裴波那契数列,1,1,2,3,5,8,13,21……
package test6; public class h { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int num1 = 1; int num2 = 1; int sum = 0; for (int i = 0; i < 18; i++) { sum = num1 + num2; num1 = num2; num2 = sum; } System.out.println("裴波那契数列的第20项是"+num2); } }