java Exercise 2

1. Known March 17, 2019 is a Sunday, to obtain user input 8-bit integer that represents the date of the week

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. obtain all even and less than 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 implementation: Output Celsius and Fahrenheit table, which requires a temperature of from 0 degrees to 250 degrees Celsius, 20 degrees every one, the entry in the table is not more than 10.

Conversion relationship: Fahrenheit Celsius * = 9/32 + 5.0; Tip: 1, cyclic operation: calculation Celsius, and outputs the control entry 2 cycling conditions: Entry <= 10 && Celsius <= 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. The output 100 can be less than the number of the front 57 of the divisible

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);

            }

        }

     }         

    }

}

The cylinder house a total of 50 liters. Existing 15 liters. Each can pick five liters. To pick a few times to pick over.

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. obtain 1! +2! + ... + n! How much? (Use while to do)

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. require users to enter a number, use the do-while it reversed in the past!

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 is obtained within all divisible by 4 and 5 and can not be divisible by 3 and the number of

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);

        }

        }   

     }

    }
Published 38 original articles · won praise 0 · Views 1123

Guess you like

Origin blog.csdn.net/weixin_45489155/article/details/102864234