java第五次作业
1.分别使用for循环,while循环,do循环求1到100之间所有能被3整除的整数的和。
for循环
public class Text {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int i;
int sum = 0;
for (i = 1; i < 101; i++) {
if (i % 3 == 0) {
sum += i;
}
}
System.out.println(sum);
}
}

while循环
While
public class Text {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int i = 1;
int sum = 0;
while (i < 101) {
if (i % 3 == 0) {
sum += i;
}
i++;
}
System.out.println(sum);
}
}

do while循环
public class Text {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int i = 1;
int sum = 0;
do {
if (i % 3 == 0) {
sum += i;
}
i++;
}while (i < 101);
System.out.println(sum);
}
}

2.输出0-9之间的数,但是不包括5。
public class Text1 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int a=0,b=9,i;
i=a+1;
while (i<b) {
if (i!=5) {
System.out.println(i);
}
i++;
}
}
}

3.编写一个程序,求整数n的阶乘,例如5的阶乘是1*2*3*4*5
import java.util.Scanner;
public class Text4 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input=new Scanner(System.in);
int n=input.nextInt();
int i=1;
int sum=1;
for(i=1;i<=n;i++) {
sum=sum*i;
} System.out.println(sum);
}
}

4.编写一个程序,输入任意学生成绩,如果输入不合法(<0或者>100),提示输入错误,重新输入,直到输入合法程序结束
import java.util.Scanner;
public class Text4 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("学生成绩");
Scanner input = new Scanner(System.in);
int cj = input.nextInt();
while (cj < 1 || cj > 100) {
System.out.println("成绩有误,重新输入");
cj = input.nextInt();
}
System.out.println("成绩合法");
}
}

5.假设某员工今年的年薪是30000元,年薪的年增长率6%。编写一个Java应用程序计算该员工10年后的年薪,并统计未来10年(从今年算起)总收入。
public class Text4 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int a = 30000;
double b = 1.06;
double nx = a * b;
double s = 30000;
int c = 1;
while (c < 10) {
nx = nx*b;
s = s + nx;
c++;
}
System.out.println("十年后年薪是" + nx);
System.out.println("十年后总收入" + s);
}
}

作业1.打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身。
package fjggfuj;
public class njkl {
public static void main(String[] args) {
// TODO 自动生成的方法存根
int i = 100;
for (i=100;i <= 999;i++) {
int a = i % 10;
int b = i / 10 % 10;
int c = i / 100;
if (a * a * a + b * b * b + c * c * c == i)
System.out.println(i);
}
}
}

2.输入年月日,判断这是这一年中的第几天
package fjggfuj;
import java.util.Scanner;
public class njkl {
public static void main(String[] args) {
// TODO 自动生成的方法存根
Scanner input=new Scanner(System.in);
System.out.println("输入年");
int year=input.nextInt();
System.out.println("输入月");
int month=input.nextInt();
System.out.println("输入日");
int day=input.nextInt();
switch(month-1){
case 12: day+=31;
case 11: day+=30;
case 10: day+=31;
case 9: day+=30;
case 8: day+=31;
case 7: day+=31;
case 6: day+=30;
case 5: day+=31;
case 4: day+=30;
case 3: day+=31;
case 2:
if (year%4==0&&year%100!=0||year%400==0) {
day+=29;
} else {
day+=28;
}
case 1: day+=31;
}
System.out.println(day);
}
}

3.由控制台输入一个4位整数,求将该数反转以后的数,如原数为1234,反转后的数位4321
package fjggfuj;
import java.util.Scanner;
public class njkl {
public static void main(String[] args) {
// TODO 自动生成的方法存根
Scanner input=new Scanner(System.in);
System.out.println("输入数字");
int a=input.nextInt();
if(a >= 10000 || a < 1000){
System.out.println("输入错误");
while(a >= 10000 || a < 1000){
a=input.nextInt();
}
}
int b = a / 1000;
int c = a % 1000 / 100;
int d = a % 100 / 10;
int e = a % 10;
int f=e*1000+d*100+c*10+b;
System.out.print("原来的数为:" + a + " " + "反转后为:"+f);
}
}


浙公网安备 33010602011771号