Java作业一 流程控制程序设计


掌握Java流程控制语句、数组的编程方法。

一、输出月份的英文名

编写一程序,将从键盘输入的每个月份数(整数)显示出其对应的英文,直至输入0结束,注意对非法数据的处理。 (while,switch语句)

//方法一,使用Systrm.in.read()
import java.io.IOException;

public class MonthName 
{
    public static void main(String args[]) 
    {
        int monthNum = 0;
        String Str = null;
        byte buf[] = new byte[20];

        try 
        {
            System.out.print("Please enter a month number:");
            System.in.read(buf);
        } 
        catch (IOException e) 
        {
            e.printStackTrace();
        }
        Str = new String(buf);
        monthNum = Integer.parseInt(Str.trim());

        while (monthNum != 0) 
        {
            switch (monthNum) 
            {
                case 1:
                    System.out.println("January");
                    break;
                case 2:
                    System.out.println("February");
                    break;
                case 3:
                    System.out.println("March");
                    break;
                case 4:
                    System.out.println("April");
                    break;
                case 5:
                    System.out.println("May");
                    break;
                case 6:
                    System.out.println("June");
                    break;
                case 7:
                    System.out.println("July");
                    break;
                case 8:
                    System.out.println("August");
                    break;
                case 9:
                    System.out.println("September");
                    break;
                case 10:
                    System.out.println("October");
                    break;
                case 11:
                    System.out.println("November");
                    break;
                case 12:
                    System.out.println("December");
                    break;
                default:
                    System.out.println("You entered a wrong number(not in 1~12)!");
                    break;
            }
            
            try 
            {
                System.out.print("Please enter a month number:");
                System.in.read(buf);
            } 
            catch (IOException e) 
            {
                e.printStackTrace();
            }
            Str = new String(buf);
            monthNum = Integer.parseInt(Str.trim());
        }
    }
}

//方法二,使用Scanner类
import java.util.Scanner;

public class MonthName 
{
    public static void main(String args[]) 
    {
        int monthNum = 0;
        System.out.print("Please enter month numbers:");
        Scanner scanner = new Scanner(System.in);
        
        if (scanner.hasNext())
        {
            monthNum = Integer.parseInt(scanner.next());
        }

        while (monthNum!=0) 
        {
            switch (monthNum) 
            {
                case 1:
                    System.out.println("January");
                    break;
                case 2:
                    System.out.println("February");
                    break;
                case 3:
                    System.out.println("March");
                    break;
                case 4:
                    System.out.println("April");
                    break;
                case 5:
                    System.out.println("May");
                    break;
                case 6:
                    System.out.println("June");
                    break;
                case 7:
                    System.out.println("July");
                    break;
                case 8:
                    System.out.println("August");
                    break;
                case 9:
                    System.out.println("September");
                    break;
                case 10:
                    System.out.println("October");
                    break;
                case 11:
                    System.out.println("November");
                    break;
                case 12:
                    System.out.println("December");
                    break;
                default:
                    System.out.println("Please enter the numbers 1~12!");
                    break;
            }
            
            if (scanner.hasNext())
            {
                monthNum = Integer.parseInt(scanner.next());
            }
        }
        scanner.close();
    }
}

二、打印水仙花数

打印出所有的“水仙花数”。所谓“水仙花数”是指一个三位数,其各位数字的立方和等于该数本身。例如153是一个“水仙花数”,因为153=13+53+33

public class NarcissisticNumber 
{
    public static void main(String args[])
    {
        System.out.println("The 3-digit narcissistic numbers:");
        
        for (int i = 100; i < 1000; i++) 
        {
            int unit = i % 10;
            int decade = i / 10 % 10;
            int hundred = i / 100;
            int judge = (int) (Math.pow(unit,3)+Math.pow(decade,3)+Math.pow(hundred,3));
            if (judge==i)  
            {
                System.out.println(i)
            };
        }
    }
}

三、统计特定数字

统计个位数是6,并且能被3整除的五位数共有多少个。

public class CountNumbers 
{
    public static void main(String args[])
    {
       /* int count = 0;
        for (int i = 1000; i <9999 ; i++)
        {
            String Str =""+i+6;
            int number = Integer.parseInt(Str);
            if (number%3==0) 
            {
                count++;
            }
        }
        System.out.print("The number is "+count);*/
        int count = 0;
        for (int i = 10006; i <99996 ; i+=10)
        {
            if (i%3==0) 
            {
                count++;
            }
        }
        System.out.print("The number is "+count);
    }
}

四、一维数组排序

编写一个程序,在其中建立一个有10个整数的数组,运行后从键盘输入10个数,然后排序(升序)后输出。

import java.util.Arrays;
import java.util.Scanner;

public class Sort 
{
    public static void main(String args[])
    {
        int array[] = new int[10];
        System.out.println("请输入10个整数:");
        Scanner scanner = new Scanner(System.in);
        
        for (int i = 0; i < 10; i++) 
        {
            if (scanner.hasNext())
            {
                array[i] = Integer.parseInt(scanner.next());
            }
        }
        System.out.println("排序结果:");
        Arrays.sort(array);
        for (int i = 0; i <10 ; i++) 
        {
            System.out.print(array[i]+" ");
        }
    }
}

五、计算矩阵每行之和

编写一个程序,计算出5行5列整数矩阵的每行之和。

import java.util.Scanner;

public class MatrixSum
{
    public static void main(String args[] )
    {
        Scanner scanner = new Scanner(System.in);
        int matrix[][] = new int[5][5];

        for(int i = 0;i < 5;i++)
        {
            for (int j = 0; j < 5; j++)
            {
                if (scanner.hasNext())
                {
                    matrix[i][j] = Integer.parseInt(scanner.next());
                }
            }
        }

        for(int i = 0;i < 5;i++)
        {
            int sum = 0;
            for (int j = 0; j < 5; j++)
            {
                sum = sum + matrix[i][j];
            }
            System.out.println(sum);
        }
    }
}
posted @ 2022-01-08 16:05  繁辰一梦  阅读(109)  评论(0编辑  收藏  举报