Java第三次作业
Posted ๓纯牛奶๓
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java第三次作业相关的知识,希望对你有一定的参考价值。
课堂练习:
1.分别使用for循环,while循环,do循环求1到100之间所有能被3整除的整数的和。(知识点:循环语句)
For循环:
package wen;
public class Test
public static void main(String[] args)
// TODO Auto-generated method stub
int sum=0;
for(int i=1;i<=100;i++)
if(i%3==0)
sum+=i;
System.out.println("1到100之间全能被3整除的整数和为:"+sum);
While循环:
package wen;
public class Test
public static void main(String[] args)
// TODO Auto-generated method stub
int sum=0,i=1;
while(i<=100)
if(i%3==0)
sum+=i;
i++;
System.out.println("1到100之间全能被3整除的整数和为:"+sum);
Do循环:
package wen;
public class Test
public static void main(String[] args)
// TODO Auto-generated method stub
int sum=0,i=1;
do
if(i%3==0)
sum+=i;
i++;
while(i<=100);
System.out.println("1到100之间全能被3整除的整数和为:"+sum);
2.输出0-9之间的数,但是不包括5。(知识点:条件、循环语句)
package wen;
public class Test
public static void main(String[] args)
// TODO Auto-generated method stub
for(int i=0;i<=9;i++)
if(i==5)
continue;
System.out.println(i);
3.编写一个程序,求整数n的阶乘,例如5的阶乘是1*2*3*4*5(知识点:循环语句)
package wen;
import java.util.Scanner;
public class Test
public static void main(String[] args)
// TODO Auto-generated method stub
Scanner input=new Scanner(System.in);
int sum=1;
System.out.println("请输入你想要求得几阶阶乘?");
int a=input.nextInt();
for(int i=1;a>0;a--)
sum*=i;
i++;
System.out.println("阶乘结果为:"+sum);
4.编写一个程序,输入任意学生成绩,如果输入不合法(<0或者>100),提示输入错误,重新输入,直到输入合法程序结束(知识点:循环语句)
package wen;
import java.util.Scanner;
public class Test
public static void main(String[] args)
// TODO Auto-generated method stub
Scanner input=new Scanner(System.in);
System.out.println("输入学生成绩:");
double score=input.nextDouble();
while (score<0||score>100)
System.out.println("输入错误,请重新输入");
score=input.nextDouble();
if(score>=0&&score<=100)
System.out.println("输入合法程序结束");
5.假设某员工今年的年薪是30000元,年薪的年增长率6%。编写一个Java应用程序计算该员工10年后的年薪,并统计未来10年(从今年算起)总收入。(知识点:循环语句)
package wen;
public class Test
public static void main(String[] args)
// TODO Auto-generated method stub
double sal=30000;
double sum=30000;
for (int year = 1; year <= 10; year++)
sal+=sal*0.06;
sum+=sal;
System.out.println("该员工10年后的年薪为:"+sal);
System.out.println("该员工未来10年的年薪为:"+sum);
作业:
1.打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身。例如:153是一个"水仙花数",因为153=1的三次方+5的三次方+3的三次方。(知识点:循环语句、条件语句)
package wen;
public class Test
public static void main(String[] args)
// TODO Auto-generated method stub
for(int i = 100; i <= 999; i++)
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.print(i + " ");
2.输入年月日,判断这是这一年中的第几天(知识点:循环语句、条件语句)
package wen;
import java.util.Scanner;
public class Test
public static void main(String[] args)
// TODO Auto-generated method stub
Scanner input=new Scanner(System.in);
int sum = 0;
System.out.println("输入年");
int year = input.nextInt();
System.out.println("输入月");
int mon = input.nextInt();
System.out.println("输入日");
int day = input.nextInt();
int[] yue = new int[]31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31;
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
for (int i = 0; i < mon - 1; i++)
sum += yue[i];
else
for (int i = 0; i < mon - 1; i++)
sum += yue[i];
sum -= 1;
sum += day;
System.out.println("今天是这一年的第" + sum + "天");
3.由控制台输入一个4位整数,求将该数反转以后的数,如原数为1234,反转后的数位4321(知识点:循环语句、条件语句)
package wen;
import java.util.Scanner;
public class Test
public static void main(String[] args)
// TODO Auto-generated method stub
Scanner input=new Scanner(System.in);
System.out.println("请输入一个4位整数");
int num=input.nextInt();
int num_q=num/1000;
int num_b=num/100%10;
int num_s=num/10%10;
int num_g=num%10;
System.out.println("原数字:"+num+"调换后:"+(num_g*1000+num_s*100+num_b*10+num_q));
第三次Java作业
public class liti1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Address a = new Address("中国", "辽宁省", "丹东市", "通江街",
123456);
System.out.println("国家" + a.getCountry() + "、省份" + a.getProvince()
+ "、城市" + a.getCity() + "、街道" + a.getStreet() + "、邮编"
+ a.getPost());
}
}
class Address {
String country;
String province;
String city;
String street;
int post;
public Address(String country, String province, String city, String street,
int post) {
this.setCountry(country);
this.setProvince(province);
this.setCity(city);
this.setStreet(street);
this.setPost(post);
}
public void setCountry(String country) {
this.country = country;
}
public void setProvince(String province) {
this.province = province;
}
public void setCity(String city) {
this.city = city;
}
public void setStreet(String street) {
this.street = street;
}
public void setPost(int post) {
this.post = post;
}
public String getCountry() {
return this.country;
}
public String getProvince() {
return this.province;
}
public String getCity() {
return this.city;
}
public String getStreet() {
return this.street;
}
public int getPost() {
return this.post;
}
}
public class liti2{
public static void main(String[] args) {
// TODO Auto-generated method stub
Employee me = new Employee("140201115", "闫昱颖", 5000, 0.1);
System.out.println("编号:" + me.getNumber() + " 姓名" + me.getName()
+ " 基本工资" + me.getBasal() + " 增长率" + me.getIncrease() + " 总工资"
+ me.salary());
}
}
class Employee {
private String number;
private String name;
private double basal;
private double increase;
public Employee(String number, String name, double basal, double increase) {
this.setNumber(number);
this.setName(name);
this.setBasal(basal);
this.setIncrease(increase);
}
public void setNumber(String number) {
this.number = number;
}
public void setName(String name) {
this.name = name;
}
public void setBasal(double basal) {
this.basal = basal;
}
public void setIncrease(double increase) {
this.increase = increase;
}
public String getNumber() {
return this.number;
}
public String getName() {
return this.name;
}
public double getBasal() {
return this.basal;
}
public double getIncrease() {
return this.increase;
}
public double salary() {
return basal * increase;
}
}
以上是关于Java第三次作业的主要内容,如果未能解决你的问题,请参考以下文章