第五次作业
1.分别使用for循环,while循环,do循环求1到100之间所有能被3整除的整数的和。(知识点:循环语句)
For循环
package rty;
import java.util.Scanner;
public class Rty {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int sum=0;
int i;
for(i=1;i<101;i++){
if(i%3==0){
sum=sum+i;
}
else{
}
}
System.out.println(sum);
}
}
While循环
package rty;
import java.util.Scanner;
public class Rty {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int sum=0;
int i=1;
while(i<101){
if(i%3==0){
sum=sum+i;
i++;
}
else{
i++;
}
}
System.out.println(sum);
}
}
Do-while循环
package rty;
import java.util.Scanner;
public class Rty {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int sum=0;
int i=1;
do{
if(i%3==0){
sum+=i;
}
i++;
}while(i<101);
System.out.println(sum);
}
}
2.输出0-9之间的数,但是不包括5。(知识点:条件、循环语句)
package rty;
import java.util.Scanner;
public class Rty {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int i;
for(i=0;i<=9;i++){
if(i!=5){
System.out.println(i);
}
}
}
}
3、编写一个程序求整数n的阶乘,例如5的阶乘是1*2*3*4*5(知识点:循环语句)
package rty;
import java.util.Scanner;
public class Rty {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input=new Scanner(System.in);
System.out.println("输入一个整数");
int i=input.nextInt();
int x;
int sum=1;
for(x=1;x<=i;x++){
sum=sum*x;
}
System.out.println(sum);
}
}
4.编写一个程序,输入任意学生成绩,如果输入不合法(<0或者>100),提示输入错误,重新输入,直到输入合法程序结束(知识点:循环语句)
package rty;
import java.util.Scanner;
public class Rty {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input=new Scanner(System.in);
System.out.println("输入成绩");
int i=input.nextInt();
while(i>100||i<0){
System.out.println("输入错误从新输入");
i=input.nextInt();
}
System.out.println("输入正确");
}
}
5.假设某员工今年的年薪是30000元,年薪的年增长率6%。编写一个Java应用程序计算该员工10年后的年薪,并统计未来10年(从今年算起)总收入。(知识点:循环语句)
package rty;
import java.util.Scanner;
public class Rty {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
double a=30000;
int x=1;
double y=0;
while(x<10){
y=y+a;
a=(1+0.06)*a;
x++;
}
System.out.println("十年总收入"+y);
System.out.println("十年后的年薪"+a);
}
}
打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身。例如:153是一个"水仙花数",因为153=1的三次方+5的三次方+3的三次方。(知识点:循环语句、条件语句)
package rty;
import java.util.Scanner;
public class Rty {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int i = 100;
while (i <= 999) {
int ge = i % 10;
int shi = i / 10 % 10;
int bai = i / 100;
if (ge * ge * ge + shi * shi * shi + bai * bai * bai == i)
System.out.println(i);
i++;
}
}
}
2、输入年月日,判断这是这一年中的第几天(知识点:循环语句、条件语句)
package rty;
import java.util.Scanner;
public class Rty {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int year, month, day;
System.out.println("请输入年份:");
year = sc.nextInt();
System.out.println("请输入月份:");
month = sc.nextInt();
System.out.println("请输入日期:");
day = sc.nextInt();
switch (month - 1) {
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 % 400 == 0) || (year % 4 == 0 && year % 100 != 0)) {
day += 29;
} else {
day += 28;
}
case 1:
day+=31;
default:
System.out.println("输入月份");
break;
}
System.out.println("今天是"+year+"年的第"+day+"天");
}
}
3.由控制台输入一个4位整数,求将该数反转以后的数,如原数为1234,反转后的数位4321(知识点:循环语句、条件语句)
package rty;
import java.util.Scanner;
public class Rty {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
System.out.println("输入四位整数");
int x = sc.nextInt();
if(x>999 && x<=9999){
int ge = x%10;
int shi = x % 100 / 10;
int bai = x%1000/100;
int qian = x/1000;
int sum = qian + bai*10 +shi*100 +ge*1000;
System.out.println(sum);
}
else{
System.out.println("逆序");
}
}
}
package rty;
import java.util.Scanner;
public class Rty {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
System.out.println("输入四位整数");
int x = sc.nextInt();
if(x>999 && x<=9999){
int ge = x%10;
int shi = x % 100 / 10;
int bai = x%1000/100;
int qian = x/1000;
int sum = qian + bai*10 +shi*100 +ge*1000;
System.out.println(sum);
}
else{
System.out.println("逆序");
}
}
}