Java第六次作业
上机练习:
1.使用for循环计算1-100的和,除了以3结尾的那些数
1 package boke4月9日; 2 3 public class Best1 { 4 5 /** 6 * @param args 7 */ 8 public static void main(String[] args) { 9 // TODO Auto-generated method stub 10 int sum = 0; 11 for (int i = 1; i <= 100; i++) { 12 if (i % 10 != 3) { 13 sum += i; 14 } 15 } 16 System.out.println("sum=" + sum); 17 } 18 19 }
2.使用二重循环输出以下图形
*
***
*****
*******
===========================
*****
****
***
**
*
1 package boke4月9日; 2 3 public class Best2 { 4 5 /** 6 * @param args 7 */ 8 public static void main(String[] args) { 9 // TODO Auto-generated method stub 10 for (int i = 1; i <= 4; i++) { 11 for (int j = 1; j <= 4 - i; j++) { 12 System.out.print(" "); 13 } 14 for (int k = 1; k <= 2 * i - 1; k++) { 15 System.out.print("*"); 16 } 17 System.out.println(); 18 } 19 System.out.println("======================="); 20 21 for (int i = 1; i <= 5; i++) { 22 for (int j = 5; j >= i; j--) { 23 System.out.print("*"); 24 } 25 System.out.println(); 26 } 27 } 28 29 }
3.循环输入学生姓名,直到输入‘Q’程序结束。
1 package boke4月9日; 2 3 import java.util.Scanner; 4 5 public class Best3 { 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 System.out.println("end"); 20 } 21 22 }
4.输入5个学生成绩,求和,当输入不合法的时候,提示输入错误重新输入。
1 package boke4月9日; 2 3 import java.util.Scanner; 4 5 public class Best4 { 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 int sum = 0; 15 for (int i = 0;;) { 16 int grade = input.nextInt(); 17 if (grade >= 0 && grade <= 100) { 18 sum += grade; 19 i++; 20 } else { 21 System.out.println("输入错误重新输入:"); 22 grade = input.nextInt(); 23 if (grade >= 0 && grade <= 100) { 24 sum += grade; 25 i++; 26 } 27 } 28 if (i == 5) { 29 break; 30 } 31 } 32 System.out.println("sum=" + sum); 33 } 34 }
5.计算 1+1/(1+2) + 1/(1+2+3) +1/(1+2+3+4)+…………+1/(1+2+....+10)
1 package boke4月9日; 2 3 public class Best5 { 4 5 /** 6 * @param args 7 */ 8 public static void main(String[] args) { 9 // TODO Auto-generated method stub 10 double sum = 0; 11 double a = 0; 12 for (int i = 1; i <= 10; i++) { 13 a += i; 14 sum += 1 / a; 15 } 16 System.out.println("sum=" + sum); 17 } 18 19 }
作业:
1.产生一个1-99的随机数,猜数字,若大了就提示“大了点”,若小了就提示“小了点”直到猜对为止。
(判断猜的次数,如果1-3次,提示太棒了 如果5-8次 一般般 如果8次以上 太笨了)
1 package boke4月9日; 2 3 import java.util.Random; 4 import java.util.Scanner; 5 6 public class Best6 { 7 8 /** 9 * @param args 10 */ 11 public static void main(String[] args) { 12 // TODO Auto-generated method stub 13 Random r = new Random(); 14 int number = r.nextInt(99) + 1; 15 int s = 0; 16 while (true) { 17 Scanner input = new Scanner(System.in); 18 System.out.println("请输入你要猜的数据(1-100):"); 19 int guessNumber = input.nextInt(); 20 if (guessNumber > number) { 21 System.out.println("您猜的数字" + guessNumber + "大了"); 22 s++; 23 } else if (guessNumber < number) { 24 System.out.println("您猜的数字" + guessNumber + "小了"); 25 s++; 26 } else { 27 System.out.println("恭喜您猜中了!"); 28 s++; 29 if (s >= 1 && s <= 3) { 30 System.out.println("太棒了"); 31 } else if (s >= 4 && s <= 8) { 32 System.out.println("一般般"); 33 } else 34 System.out.println("太笨了"); 35 } 36 } 37 } 38 }
2.输入一个数,判断是不是质数(只能被1和自身整除的数是质数)
1 package boke4月9日; 2 3 import java.util.Scanner; 4 5 public class Best7 { 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 int num = input.nextInt(); 15 int i = 2; 16 for (; num % i != 0 && i * i < num; i++) 17 ; 18 if (i * i > num) { 19 System.out.println("是质数"); 20 } else { 21 System.out.println("不是质数"); 22 } 23 System.out.println("=============="); 24 25 for (; num % i != 0; i++) 26 ; 27 if (i != num) { 28 System.out.println("该数不是质数"); 29 } else { 30 System.out.println("该数是质数"); 31 } 32 } 33 }
3.求裴波那契数列的第20项是什么。裴波那契数列,1,1,2,3,5,8,13,21……
1 package boke4月9日; 2 3 public class Best8 { 4 5 /** 6 * @param args 7 */ 8 public static void main(String[] args) { 9 // TODO Auto-generated method stub 10 int num1 = 1; 11 int num2 = 1; 12 int sum = 0; 13 for (int i = 0; i < 18; i++) { 14 sum = num1 + num2; 15 num1 = num2; 16 num2 = sum; 17 } 18 System.out.println("裴波那契数列的第20项是"+num2); 19 } 20 }