Advanced Programming-Week4

Advanced Programming-Week4

Summary

  1. for 循环
  2. if 判断
  3. Integer 类
    1. 字符串转整型 Integer.parseInt()

Task21

import javax.swing.*;
import java.util.*;

public class Task21 {
    public static void main(String []args)
    {
        System.out.println("Please enter a number");
        Scanner keybord = new Scanner(System.in);
        int n;
        n=keybord.nextInt();
        int formula_ans=(n*(1+n)/2);
        System.out.println("The formula\'s anser is "+ formula_ans);
        int sum=0;
        for(int i=0;i<=n;i++)
        {
            sum+=i;
        }
        System.out.println("The sum from 1 to "+n+" is "+sum);
        String ans1="The formula is right.";
        String ans2="The formula is not right.";
        if(formula_ans==sum)
        {
            System.out.println(ans1);
        }else System.out.println(ans2);
    }
}

Task22

import javax.swing.*;
import java.util.*;

public class Task22 {
    public static void main(String []args)
    {
        int num;
        Scanner keybord = new Scanner(System.in);
        num=keybord.nextInt();
        JOptionPane.showMessageDialog(null,"The anser is "+(num*num*num));   
    }
}

 

Task23

import javax.swing.*;
import java.util.*;

public class Task23 {
    public static void main(String []args)
    {
        int time;
        System.out.println("Please enter the time:");
        Scanner keybord=new Scanner(System.in);
        time=keybord.nextInt();
        int hh,mm;
        hh=time/60;
        mm=time%60;
        JOptionPane.showMessageDialog(null,String.format("The time is %02d hours %02d minuts",hh,mm),"time", -1);
    }
}

Task24

import javax.swing.*;
import java.util.*;

public class Task24 {
    public static void main(String []args)
    {
        int length,width;
        length=Integer.parseInt(JOptionPane.showInputDialog(null,"Please enter the length:"));
        width=Integer.parseInt(JOptionPane.showInputDialog(null,"Please enter the width:"));
        int ans;
        ans=length*width;
        JOptionPane.showMessageDialog(null,"Your room space is " + ans,"Method 1",-1);
        
        Scanner keybord = new Scanner(System.in);
        length=keybord.nextInt();
        width=keybord.nextInt();
        ans=length*width;
        JOptionPane.showMessageDialog(null,"Your room space is " + ans,"Method 2",-1);
    }
}

Task25-26

import javax.swing.*;
import java.util.*;

public class Task25 {
    public static void main(String []args)
    {
        int origin;
        Scanner keybord=new Scanner(System.in);
        System.out.println("Please enter the num:");
        origin=keybord.nextInt();
        int n1,n2,n3,n4,n5,n6;
        n1=origin/100000;
        n2=origin/10000%10;
        n3=origin/1000%10;
        n4=origin/100%10;
        n5=origin/10%10;
        n6=origin%10;
        System.out.printf("The first num is %d\n",n1);
        System.out.printf("The second num is %d\n",n2);
        System.out.printf("The third num is %d\n",n3);
        System.out.printf("The fourth num is %d\n",n4);
        System.out.printf("The fifth num is %d\n",n5);
        System.out.printf("The last num is %d",n6);
        origin=Integer.parseInt(JOptionPane.showInputDialog(null,"Please enter a number of length 6:"));
        n1=origin/100000;
        n2=origin/10000%10;
        n3=origin/1000%10;
        n4=origin/100%10;
        n5=origin/10%10;
        n6=origin%10;
        JOptionPane.showMessageDialog(null,"The first num is "+ n1
                                          +"\nThe second num is "+n2
                                          +"\nThe third num is "+n3
                                          +"\nThe fourth num is "+n4
                                          +"\nThe fifth num is "+n5
                                          +"\nThe last num is "+n6);
    }
}

Task27

import javax.swing.*;
import java.util.*;

public class Task27 {
    public static void main(String []args)
    {
        Scanner keybord=new Scanner(System.in);
        System.out.println("Please enter the age and the resting heart rate:");
        int a,r;
        a=keybord.nextInt();
        r=keybord.nextInt();
        System.out.printf("Your training heart rate is %d",7*(200-a)+3*r);

        a=Integer.parseInt(JOptionPane.showInputDialog("Please enter your age"));
        r=Integer.parseInt(JOptionPane.showInputDialog("Please enter your resting heart rate"));
        JOptionPane.showMessageDialog(null,"Your training heart rate is "+(7*(200-a)+3*r),"Anser",-1);
    }
}

Task28

import javax.swing.*;
import java.util.*;

public class Task28 {
    public static void main(String []args)
    {
        int cyc_time;
        int jog_time;
        int swim_time;
        Scanner keybord = new Scanner(System.in);
        cyc_time=keybord.nextInt();
        jog_time=keybord.nextInt();
        swim_time=keybord.nextInt();
        System.out.printf("The pounds is %d",cyc_time*200+jog_time*475+swim_time*275);

        cyc_time=Integer.parseInt(JOptionPane.showInputDialog("Please enter the cycling time"));
        jog_time=Integer.parseInt(JOptionPane.showInputDialog("Please enter the jogging time"));
        swim_time=Integer.parseInt(JOptionPane.showInputDialog("Please enter the swimming time"));
        JOptionPane.showMessageDialog(null,"The pounds is " + (cyc_time*200+jog_time*475+swim_time*275));
    }
}

Task30

import javax.swing.*;
import java.util.*;

public class Task30 {
    public static void main(String []args)
    {
        int num;
        Scanner keybord=new Scanner(System.in);
        num=keybord.nextInt();
        int n1,n2,n3,n4;
        n1=(num/1000+7)%10;
        n2=(num/100%10+7)%10;
        n3=(num/10%10+7)%10;
        n4=(num%10+7)%10;
        System.out.print("The new number is "+n3+n4+n1+n2);

        num=Integer.parseInt(JOptionPane.showInputDialog("Please enter a number of lenght 4"));
        n1=(num/1000+7)%10;
        n2=(num/100%10+7)%10;
        n3=(num/10%10+7)%10;
        n4=(num%10+7)%10;
        JOptionPane.showMessageDialog(null,"The new number is "+n3+n4+n1+n2,"Anser",-1);       
    }
}

Task36

import javax.swing.*;
import java.util.*;

public class Task36 {
    public static void main(String []args)
    {
        int num;
        Scanner keybord=new Scanner(System.in);
        System.out.println("Please enter a number between 1 to 10");
        num=keybord.nextInt();
        String ans;
        switch(num)
        {
            case 1:ans="I";break;
            case 2:ans="II";break;
            case 3:ans="III";break;
            case 4:ans="IV";break;
            case 5:ans="V";break;
            case 6:ans="VI";break;
            case 7:ans="VII";break;
            case 8:ans="VIII";break;
            case 9:ans="IX";break;
            case 10:ans="X";break;
            default:ans="ERROR.The number is not betweem 1 to 10.";
        }
        System.out.println(ans);
    }
}

Task40

 import javax.swing.*;

public class Task40 {
    public static void main(String []args)
    {
        int mile;
        double cost;
        mile=Integer.parseInt(JOptionPane.showInputDialog("Please enter your total mileage"));
        if(mile>100)
        {
            cost=125+(mile-100)*0.3;
        }else cost=mile*1.25;
        JOptionPane.showMessageDialog(null,"The cost is " + cost,"Anser",-1);
    }
}

Task41

import javax.swing.JOptionPane;

public class Task41 {
    public static void main(String []args)
    {
        double [][]cost=new double [][]{{},{0,5600.5,5500.6000},{0,10600.50,11000,13500},{0,17000,22000,25000}};
        int x,y;
        String error_message1="The project is not find";
        String error_message2="The type is not find";
        x=Integer.parseInt(JOptionPane.showInputDialog("Please enter the project category."));
        if(x>3||x<1) 
        { 
            JOptionPane.showMessageDialog(null, error_message1); 
        }else{ 
            y=Integer.parseInt(JOptionPane.showInputDialog("Please enter the service type.")); 
            if(y>3||y<1)
            {
                JOptionPane.showMessageDialog(null, error_message2);
            }else{
                JOptionPane.showMessageDialog(null,"Your cost is " + cost[x][y]);
            }
        }
        

    }
}
暂无评论

发送评论 编辑评论


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