第六周Java作业
上机练习
1
package Class;
public class Class1 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int sum = 0;
for (int i = 1; i <= 100; i++) {
int j = i%10;
if(j!=3){
sum = sum+i;
}
}
System.out.println(sum);
}
}
2
package Class;
public class Class1 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
for (int i = 1; i < 5; i++) {
for (int j = 4-i; j >0; j--) {
System.out.print(" ");
}
for (int j = 1; j <= i*2-1; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
package Class;
public class Class1 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
for (int i = 1; i < 6; i++) {
for (int j = 5-i; j >= 0; j--) {
System.out.print("*");
}
System.out.println();
}
}
}
3
package Class;
import java.util.Scanner;
public class Class1 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.println("输入一个学生的名字");
String name = input.next();
while(true){
if (name.equals("Q")) {
System.out.println("程序结束");
break;
}else{
name = input.next();
}
}
}
}
4
package Class;
import java.util.Scanner;
public class Class1 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.println("输入第一个学生的成绩");
int a = input.nextInt();
if (a>100||a<0) {
System.out.println("输入错误,重新输入");
a = input.nextInt();
}
System.out.println("输入第二个学生的成绩");
int b = input.nextInt();
if (b>100||b<0) {
System.out.println("输入错误,重新输入");
b = input.nextInt();
}
System.out.println("输入第三个学生的成绩");
int c = input.nextInt();
if (c>100||c<0) {
System.out.println("输入错误,重新输入");
c = input.nextInt();
}
System.out.println("输入第四个学生的成绩");
int d = input.nextInt();
if (d>100||d<0) {
System.out.println("输入错误,重新输入");
d = input.nextInt();
}
System.out.println("输入第五个学生的成绩");
int e = input.nextInt();
if (e>100||e<0) {
System.out.println("输入错误,重新输入");
e = input.nextInt();
}
int sum = 0;
sum = a+b+c+d+e;
System.out.println(sum);
}
}
5
package Class;
import java.util.Scanner;
public class Class1 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
double sum = 0;
double a = 0;
for (double i = 1; i < 11; i++) {
a=a+i;
sum=sum+1/a;
}
System.out.println(sum);
}
}
作业
1
package Class;
import java.util.Random;
import java.util.Scanner;
public class Class1 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Random i = new Random();
int a = i.nextInt(99)+1;
int b = 0;
while(true){
if(a>88){
System.out.println("大了点");
a = i.nextInt(99)+1;
b++;
}
if(a<88){
System.out.println("小了点");
a = i.nextInt(99)+1;
b++;
}
if(a==88){
System.out.println("猜对了");
break;
}
}
if(b<3){
System.out.println("太棒了");
}
if(b>5&&b<=8){
System.out.println("一般般");
}
if(b>8){
System.out.println("太笨了");
}
}
}
2
package Class;
import java.util.Random;
import java.util.Scanner;
public class Class1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
//2.输入一个数,判断是不是质数(只能被1和自身整除的数是质数)
Scanner input = new Scanner(System.in);
System.out.println("输入一个数");
int n = input.nextInt();
int j = (int) Math.sqrt(n);
for (int i = 2; i < j; i++) {
if(n%i == 0) {
System.out.println("这个数不是质数");break;
}
else {
System.out.println("这个数是质数");break;
}
}
}
}
3
package Class;
import java.util.Random;
import java.util.Scanner;
public class Class1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
//3.求裴波那契数列的第20项是什么。裴波那契数列,1,1,2,3,5,8,13,21……
int a[] = new int[20];
for (int i = 2; i < 20; i++) {
a[0] = 1;
a[1] = 1;
a[i] = a[i-1]+a[i-2];
}
System.out.println(a[19]);
}
}