Advanced Programming-Week5

Advanced Programming-Week5

Summary

  1. switch 选择
  2. 数字运算
  3. 字符串拼接

Task42

import java.util.Scanner;

public class Task42 {
    public static void main(String []args)
    {
        int score;
        Scanner keybord=new Scanner(System.in);
        System.out.println("Please enter the score:");
        score=keybord.nextInt();
        if(score==100) score--;
        score/=10;
        char grade;
        switch(score)
        {
            case 9: grade='A';break;
            case 8: grade='B';break;
            case 7: grade='C';break;
            case 6: grade='D';break;
            default: grade='F';break;
        }
        System.out.println("Your grade is "+grade);
    }
}

Task43

/*
Task 43: 
Write a program that accepts a time on a 24-hour clock 
(using 2 integer variables, the first denoting hours and 
the second denoting minutes). Your program should convert 
this input to its equivalent time as it would appear on 
a 12-hour clock and display the result. In your output you 
should have a value for hours, minutes and a designator 
(where a designator in this context is am or pm or noon or midnight).
*/

import java.util.*;

public class Task43 {
    public static void main(String []args)
    {
        int hh,mm;
        String times="AM";
        Scanner keybord = new Scanner(System.in);
        hh=keybord.nextInt();
        mm=keybord.nextInt();
        if(hh>12)
        {
            hh-=12;
            times="PM";
        }
        System.out.format("%02d:%02d %s",hh,mm,times);
    }
}

Task44

/* 
Task 44: 
Write a program that accepts a time on a 12-hour clock 
(using 3 integer variables, the first denoting hours, 
the second denoting minutes and the third is a designator). 
Your program should convert this input to its equivalent time 
as it would appear on a 24-hour clock and display the result. 
In your output it is only necessary to display hours and minutes.
*/

package works2022.Week5.Task44;

import java.util.Scanner;

public class Task44 {
    public static void main(String []args)
    {
        int hh,mm;
        int times;
        Scanner keybord = new Scanner(System.in);
        hh=keybord.nextInt();
        mm=keybord.nextInt();
        System.out.println("Please enter 0 or 1 to instand the AM and PM");
        times=keybord.nextInt();
        if(times==1)
        {
            hh+=12;
        }
        System.out.format("%02d:%02d",hh,mm);
    }
}

Task45

import java.util.*;

public class Task45 {

    public static int cal(int q,int m,int j,int k)
    {
        int ans=0;
        ans=(q+26*(m+1)/10+k+k/4+j/4+5*j)%7;
        return ans;
    }
    public static void main(String []args)
    {
        int q,m,y,j,k;
        String error="is not a right number";
        String []week={"Saturday","Sunday","Monday","Tuesday","Wednesday","Thursday","Friday"};
        Scanner keybord=new Scanner(System.in);
        System.out.println("Please enter the day of the month:");
        q=keybord.nextInt();
        System.out.println("Please enter the month:");
        m=keybord.nextInt();
        System.out.println("Please enter the year:");
        y=keybord.nextInt();
        keybord.close();
        if(m>12)
        {
            error=m+" "+error;
            System.out.println(error);
            System.exit(0);
        }else{
            if(q<32&&q>0)
            {
                if(m==1||m==2)
                {
                    m+=12;
                    y--;
                }
                j=y/100;
                k=y%100;
                int day=cal(q,m,j,k);
                System.out.println(week[day]);
            }else{
                error=q+" "+error;
                System.out.println(error);
                System.exit(0);
            }
        }
    }
}

Task46

public class Task46 {
    public static void main(String []args)
    {
        int a=9,b=3,c=2,d=8;
        d=a++ +c*++a/b%d+3+a;
        System.out.println(++a+"\t"+b++ +"\t"+--c+"\t"+d--);
        a=a+b++*c+9-d++;
        System.out.println(a+"\t"+b--+"\t"+--c+"\t"+d+2);
        c+=2+3*4;
        System.out.println(a/3+3*1-1+"\t");
        d=b*c+(2%4);
        System.out.println(a+"\t"+b+"\t" + ++c+"\t"+--d);
    }
}

Task47

import java.util.Scanner;

public class Task47 {
    public static void main(String []args)
    {
        String k;
        int times;
        Scanner keybord=new Scanner(System.in);
        System.out.println("Please enter a character:");
        k=keybord.nextLine();
        System.out.println("Please enter the times that the character dispaly:");
        times=keybord.nextInt();
        for(int i=0;i<times;i++)
        {
            System.out.print(k);
        }
    }
}

Task48

import java.util.Scanner;

public class Task48 {
    public static void main(String []args)
    {
        Scanner keybord=new Scanner(System.in);
        int t,sum=0;
        System.out.println("Please enter ten numbers:");
        for(int i=0;i<10;i++)
        {
            t=keybord.nextInt();
            sum+=t;
        }
        System.out.println("The sum is "+sum);
    }
}

Task49

import java.util.Scanner;

public class Task49 {
    public static void main(String []args)
    {
        int num;
        Scanner keybord=new Scanner(System.in);
        System.out.println("Please enter a number:");
        num=keybord.nextInt();
        int sum=0;
        for(int i=1;i<=num;i++)
        {
            sum+=i*i;
        }
        System.out.println("The sum of number of squares is "+sum);
    }
}

评论

  1. Tr
    2 年前
    2022-3-28 9:27:28

    催更

发送评论 编辑评论


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