1.已知2019年3月17日是星期日,求出用户输入一个代表年月日的8位整数是星期几
import java.text.DateFormat;
import java.util.*;
public class T1 {
public static void main(String[] args)
{
Scanner input=new Scanner(System.in);
int a=input.nextInt();
int year;
int month;
int day;
year=a/10000;
month=a/100%100;
day=a%100;
Calendar calendar=Calendar.getInstance();
calendar.clear();
calendar.set(year, month-1, day);
DateFormat t=DateFormat.getDateInstance(DateFormat.FULL);
System.out.println(t.format(calendar.getTime()));
input.close();
}
}
2.求出100以内所有偶数的和
public class T1 {
public static void main(String[] args) {
int s=0;
for(int i=0;i<=100;i++)
{
if(i%2==0)
s=s+i;
if(i==100)
System.out.println(s);
}
}
}
3.使用do-while实现:输出摄氏温度与华氏温度的对照表,要求它从摄氏温度0度到250度,每隔20度为一项,对照表中的条目不超过10条。
转换关系:华氏温度 = 摄氏温度 * 9 / 5.0 + 32 ;提示: 1、循环操作:计算摄氏温度,并输出对照条目 2、循环条件:条目<=10 && 摄氏温度 <= 250
public class T1 {
public static void main(String[] args) {
double fahrenheit;
int degreeCelsius=0;
int x=1;
do
{
if((degreeCelsius%20==0))
{
fahrenheit=degreeCelsius* 9 / 5.0 + 32;
x++;
System.out.println("华氏温度:" + fahrenheit+"摄氏温度 :"+degreeCelsius);
}
degreeCelsius++;
}while((x<=10)&&(degreeCelsius<=250));
}
}
4.输出100以内能被7整除的前5个数
public class T1 {
public static void main(String[] args)
{ int c=0;
for(int i=0;i<100;i++)
{
if(i%7==0)
{ c++;
if(c<=5)
{
System.out.println(i);
}
}
}
}
}
5.缸子里一共有50升水。现有15升。每次能挑5升。要挑几次才能挑满。
public class T1 {
public static void main(String[] args)
{ int water=50;
int nowWater=15;
int spurWater=5;
int x;
x=(water-nowWater)/ spurWater;
System.out.println("要挑"+x+"次才能挑满");
}
}
6.求出1!+2!+…+n!是多少?(使用while来做)
import
java.util.*;
public class T1 {
public static void main(String[] args)
{
int s=1;
int i=1;
int a=0;
Scanner
input=new Scanner(System.in);
System.out.println("请输入n:");
int n=input.nextInt();
while(i<=n)
{
s=s*i;
i++;
a=s+a;
}
System.out.println(a);
input.close();
}
}
7.要求用户输入一个数,使用do-while将它反转过去!
import java.util.*;
public class T1 {
public static void main(String[] args)
{
Scanner input=new Scanner(System.in);
String a=input.next();
System.out.println(new StringBuilder(a).reverse().toString());
input.close();
}
}
8…求出1000以内所有能被4和5整除并且不能被3整除的数之和
public class T1 {
public static void main(String[] args)
{
int c=0;
int a=0;
for(int i=1;i<=1000;i++)
{
if(i%3!=0)
if((i%4==0)&&(i%5==0))
{
c=i;
a=c+a;
if(i==1000)
System.out.println(a);
}
}
}
}