Advanced Programming-Week9

Advanced Programming-Week9

Summary

  1. 数组的声明与使用

Task60

package works2022.Week9;

import java.util.Scanner;

public class Task60 {
    public static void main(String []args)
    {
        System.out.println("Please enter the number:");
        Scanner in = new Scanner(System.in);
        int []stat=new int[10];
        while(in.hasNext())
        {
            int a=in.nextInt();
            if(a<1||a>100)
            {
                System.out.println("The number "+a+" not in a right range.");
            }else stat[a/10]=stat[a/10]+1;
        }
        for(int i=0;i<10;i++){
            System.out.format("%02d to %02d \t",(i*10+1),((i+1)*10));
            for(int j=0;j<stat[i];j++)
            {
                System.out.print("*");
            }
            System.out.println("("+stat[i]+")");
        }
        in.close();
    }
}

Task61

package works2022.Week9;

import java.util.Scanner;

public class Task61 {
    public static void main(String [] args)
    {
        String equivalences [] = {"", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten",
        "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen",
        "seventeen", "eighteen", "nineteen", "twenty", "twenty one",
        "twenty two", "twenty three", "twenty four", "twenty five", "twenty six",
        "twenty seven", "twenty eight", "twenty nine"};
        Scanner in = new Scanner(System.in);
        int hour=0,minute=0;
        System.out.println("Please enter the hour:");
        while(in.hasNext())
        {
            hour=in.nextInt();
            if(hour>=1&&hour<=12)
            {
                break;
            }else{
                System.out.println("The number you enter is not right, please enter a new number:");
            }
        }
        System.out.println("Please enter the minute:");
        while(in.hasNext())
        {
            minute=in.nextInt();
            if(minute>=0&&minute<=59)
            {
                break;
            }else{
                System.out.println("The number you enter is not right, please enter a new number:");
            }
        }
        if(minute%15==0)
        {
            switch(minute/15)
            {
                case 1:System.out.println("quarter past "+equivalences[hour]);break;
                case 2:System.out.println("half past "+equivalences[hour]);break;
                case 3:System.out.println("quarter to "+equivalences[hour+1]);break;
            }
        }else if(minute>30)
        {
            System.out.println(equivalences[60-minute]+" minute(s) to "+equivalences[hour+1]);
        }else if(minute==0)
        {
            System.out.println(equivalences[hour]+"o'clock");
        }else{
            System.out.println(equivalences[minute]+" minute(s) pass to "+equivalences[hour]);
        }
        in.close();
    }
}

Task62

package works2022.Week9;
import java.util.Scanner;
public class Task62 {

    private boolean validateDate(int year,int month,int day)
    {
        if(month<1||month>12) return false;
        int []m={0,31,28,31,30,31,30,31,31,30,31,30,31};
        if(month==2&&(year%4==0)&&(year%100==0)||(year%400==0))
        {
            if(day>29) return false;
            else return true;
        }else{
            if(day>m[month]) return false;
            else return true;
        }
    }
    public static void main(String []args)
    {
        Scanner in = new Scanner(System.in);
        Task62 t=new Task62();
        int year=0,month=0,day=0;
        System.out.println("Please enter the year:");
        year=in.nextInt();
        System.out.println("Please enter the month:");
        month=in.nextInt();
        System.out.println("Please enter the day:");
        day=in.nextInt();
        if(t.validateDate(year,month,day)) System.out.println("It is a right date."); 
        else System.out.println("It is not a right date.");
        in.close();
    }
}

Task63

package works2022.Week9;

import java.util.Scanner;

public class Task63 {

    public static boolean luhnFormula(int a[],int n)
    {
        int tot1=0,tot2=0;
        int temp=0;
        if(n==13&&a[0]!=4) return false;
        for(int i=0;i<n;i=i+2)
        {
            temp=a[i]*2;
            while(temp!=0)
            {
                tot1=tot1+temp%10;
                temp=temp/10;
            }
        }
        for(int i=1;i<n;i=i+2)
        {
            tot2+=a[i];
        }
        int ans=tot1+tot2;
        if(ans%10==0)
        {
            return true;
        }return false;
    }
    public static void main(String []args)
    {
        int []a=new int[16];
        Scanner in=new Scanner(System.in);
        String num_s;
        num_s=in.nextLine();
        for(int i=0;i<num_s.length();i++)
        {
            a[i]=num_s.charAt(i)-'0';
        }
        if(luhnFormula(a,num_s.length()))
        {
            System.out.println("The credit card number is true.");
        }else System.out.println("The credit card number is false.");
        in.close();
    }
}

暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇