# 第六次作业

1.使用for循环计算1-100的和，除了以3结尾的那些数

package sdsd;

import java.util.Scanner;

public class Test1111 {

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 sdsd;

import java.util.Scanner;

public class Test1111 {

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 m = 1; m <= i * 2 - 1; m++) {
System.out.print("*");
}
System.out.println("\n");
}
System.out.println("\n");
}

}

*****
****
***
**
*

package sdsd;

import java.util.Scanner;

public class Test1111 {

public static void main(String[] args) {
// TODO Auto-generated method stub
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= 6 - i; j++) {
System.out.print("*");
}
System.out.println("\n");
}
System.out.println("\n");
}

}

3.循环输入学生姓名，直到输入‘Q’程序结束。

package sdsd;

import java.util.Scanner;

public class Test1111 {

public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("输入学生姓名");
Scanner input = new Scanner(System.in);
String name = input.next();
while (name != "Q") {
name = input.next();
if (name.equals("Q"))
System.out.println("程序结束");
}
}
}

4.输入5个学生成绩，求和，当输入不合法的时候，提示输入错误重新输入。

package sdsd;

import java.util.Scanner;

public class Test1111 {

public static void main(String[] args) {
// TODO Auto-generated method stub
int sum=0;
Scanner input=new Scanner(System.in);
System.out.println("输入学生成绩");
for(int i=1;i<=5;i++){
System.out.println("输入错误重新输入");
}
}
System.out.println(sum);
}
}

5.计算 1+1/（1+2） + 1/（1+2+3） +1/（1+2+3+4）+…………+1/（1+2+....+10）

package sdsd;

import java.util.Scanner;

public class Test1111 {

public static void main(String[] args) {
// TODO Auto-generated method stub
double a = 1;
double b = 0;
double sum = 0;
for (int i = 1; i <= 10; i++) {
b = b + i;
sum = sum + a / b;
}
System.out.println(sum);
}
}

6.产生一个1-99的随机数，猜数字，若大了就提示“大了点”，若小了就提示“小了点”直到猜对为止。

package sdsd;

import java.util.Random;
import java.util.Scanner;

public class Test1111 {

public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
Random a = new Random();
int b = a.nextInt(99);

int i = 1;
System.out.println("请猜一个数字(1~99):");
for (i = 1; i <= 99; i++) {
int num = input.nextInt();
if (num > b) {
System.out.println("大了点");
continue;
}
if (num < b) {
System.out.println("小了点");
continue;
}
if (num == b) {
System.out.println("猜对了");
break;
}
}
}
}

7.输入一个数，判断是不是质数（只能被1和自身整除的数是质数）

package sdsd;

import java.util.Scanner;

public class Test1111 {

public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.println("请输入一个数(该数大于1):");
int num = input.nextInt();
for (int i = 2; i <= num; i++) {
if (num % i == 0 && num != i) {
System.out.println(num + "不为质数，能被" + i + "整除");
break;
}

else {
System.out.println(num + "为质数！");
break;
}
}
}
}

8.求裴波那契数列的第20项是什么。裴波那契数列，1,1,2,3,5,8,13，21……

package sdsd;

import java.util.Scanner;

public class Test1111 {

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("裴波那契数列的第20项是:" + c);
}
}

posted @ 2021-04-13 09:18  叶鑫宇  阅读(29)  评论(0编辑  收藏  举报