《Java语言程序设计(基础篇)(原书第10版)》第2~4章部分编程练习题代码

这内容只包括本人在上《Java语言程序设计》这门课时老师布置的题目。因此,这部分代码只是原书的一小部分编程题。
例:(2.6)意思是:第2章的第6题。

(2.6)(求一个整数各位数的和)

package javahomework;

import java.util.Scanner;

public class java0206 {
    public static void main (String[] args) {
        //Create a Scanner object 
        Scanner input =new Scanner(System.in);
        System.out.print("Enter a number between 0 and 1000:");
        int number = input.nextInt();
        int a = number/1000;//判断千位数
        int b = number/100-a*10;//统计百位数
        int c = number/10-a*100-b*10;//统计十位数
        int d = number-a*1000-b*100-c*10;//统计个位数
        int s = a+b+c+d;//求各个位数的总和
        System.out.println("The sum of the digit is "+ s );
    }
}

(2.10)(科学:计算能量)

package javahomework;

import java.util.Scanner;

public class java0210 {
    public static void main (String[] args) {
        //Create a Scanner object 
        Scanner input =new Scanner(System.in);
        System.out.print("Enter the amount of water in kilograms:");
        double number1 = input.nextDouble();
        System.out.print("Enter the initial temperature:");
        double number2 = input.nextDouble();
        System.out.print("Enter the final temperature:");
        double number3 = input.nextDouble();

        double Q = number1*(number3-number2)*4184;
        System.out.println("The energy needed is " + Q );
    }

}

(3.9)(商业:检查ISBN-10)

package javahomework;

import java.util.Scanner;
public class java0309 {
   public static void main(String[] args) {
       Scanner input = new Scanner(System.in);
       System.out.print("Enter the first 9 digits of an ISBN as integer: ");
       String num = input.nextLine();
       String[] nums = new String [num.length()];
       for (int i = 0; i < num.length();i++) {
           nums[i] = num.charAt(i)+"";
       }
       int A = 0;
       for (int i = 0; i < nums.length; i++) {
           A = A + Integer.parseInt(nums[i]) * (i+1) ;
       }
       A = A % 11;
       if (A == 10) {
          System.out.println("The ISBN-10 number is " + num + "X"); 
       }
       else
          System.out.println("The ISBN-10 number is " + num + A);
   }

}

(3.21)(科学:某天是星期几)

package javahomework;

import java.util.Scanner;
public class java0321{
    public static void main(String[] args) {
        Scanner input = new Scanner (System.in);
        System.out.print("Enter year:(e.g.,2012): ");
        int year = input.nextInt();
        System.out.print("Enter month: 1-12: ");
        int month = input.nextInt();
        System.out.print("Enter the day of the month: 1-31:");
        int day = input.nextInt();
        int q, m, j, k;
        q = day;
        if(month == 1) {
            m = 13;
            year = year -1;
        }
        else if(month == 2){
            m = 14;
            year = year -1;
        }
        else {
            m = month;
        }
        j = (int)Math.floor(year/100);
        k = year % 100;
        int h;
        h = (q + (26*(m + 1))/10 + k + k/4 + j/4 + 5*j) % 7;
        switch(h) {
        case(0):System.out.println("Day of the week is Saturday");break;
        case(1):System.out.println("Day of the week is Sunday");break;
        case(2):System.out.println("Day of the week is Monday");break;
        case(3):System.out.println("Day of the week is Tuesday");break;
        case(4):System.out.println("Day of the week is Wednesday");break;
        case(5):System.out.println("Day of the week is Thursday");break;
        case(6):System.out.println("Day of the week is Friday");break;
        }

    }

}

(3.23)(几何:点是否在矩形内?)

package javahomework;

import java.util.Scanner;
public class java0323 {
    public static void main(String[] args) {
        Scanner input = new Scanner (System.in);
        System.out.print("Enter a point with two coordinates: ");
        double x = input.nextDouble();
        double y = input.nextDouble();
        double width, height;
        width = 10.0;
        height = 5.0;
        double x0, y0;
        x0 = Math.abs(x);
        y0 = Math.abs(y);
        if (x0 <= width/2 && y0 <= height/2) {
            System.out.println("Point " + "(" + x + "," + y +")" + "is in the rectangle " );
        }
        else {
            System.out.println("Point " + "(" + x + "," + y +")" + "is not in the rectangle " );
        }

    }

}

(3.27)(几何:点是否在三角形内?)

package javahomework;

import java.util.Scanner;
public class java0327 {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter a point's x- and y-coordinates: ");
        double x = input.nextDouble();
        double y = input.nextDouble();
        if (x > 0 && y > 0 && 0.5*x+y-100 < 0) {
            System.out.println("Point is in the rectangle " );
        }
        else
            System.out.println("Point is not in the rectangle " );
    }

}

(4.6)(圆上的随机点)

package javahomework;

public class java0406 {
    public static void main(String[] args) {

    double r = 40.0;
    double rs1 = Math.random() * Math.PI * 2;
    double x1 = r * Math.cos(rs1);
    double y1 = r * Math.sin(rs1);


    double rs2 = Math.random() * Math.PI * 2;
    double x2 = r * Math.cos(rs2);
    double y2 = r * Math.sin(rs2);

    double rs3 = Math.random() * Math.PI * 2;
    double x3 = r * Math.cos(rs3);
    double y3 = r * Math.sin(rs3);

    double a = Math.sqrt((x2-x3)*(x2-x3) + (y2-y3)*(y2-y3));
    double b = Math.sqrt((x1-x3)*(x1-x3) + (y1-y3)*(y1-y3));
    double c = Math.sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));

    double A = Math.toDegrees(Math.acos((a*a - b*b -c*c) / (-2*b*c)));
    double B = Math.toDegrees(Math.acos((b*b - a*a -c*c) / (-2*a*c)));
    double C = Math.toDegrees(Math.acos((c*c - a*a -b*b) / (-2*a*b)));

    System.out.println("The three angles are " + Math.round(A * 100) / 100.0 + " " + Math.round(B * 100) / 100.0 + " " +Math.round(C * 100) / 100.0);

    }
}

(4.15)(电话键盘)

package javahomework;

import java.util.Scanner;
public class java0415 {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter a letter: ");
        String num = input.nextLine();

        if( num.equalsIgnoreCase("a") || num.equalsIgnoreCase("b") || num.equalsIgnoreCase("c")) {
            System.out.println("The corresponding number is 2 ");
        }
        else if(num.equalsIgnoreCase("d") || num.equalsIgnoreCase("e") || num.equalsIgnoreCase("f")) {
            System.out.println("The corresponding number is 3 ");
        }
        else if(num.equalsIgnoreCase("g") || num.equalsIgnoreCase("h") || num.equalsIgnoreCase("i")) {
            System.out.println("The corresponding number is 4 ");
        }
        else if(num.equalsIgnoreCase("j") || num.equalsIgnoreCase("k") || num.equalsIgnoreCase("l")) {
            System.out.println("The corresponding number is 5 ");
        }
        else if(num.equalsIgnoreCase("m") || num.equalsIgnoreCase("n") || num.equalsIgnoreCase("o")) {
            System.out.println("The corresponding number is 6 ");
        }
        else if(num.equalsIgnoreCase("p") || num.equalsIgnoreCase("q") || num.equalsIgnoreCase("r") || num.equalsIgnoreCase("s")) {
            System.out.println("The corresponding number is 7 ");
        }
        else if(num.equalsIgnoreCase("t") || num.equalsIgnoreCase("u") || num.equalsIgnoreCase("v")) {
            System.out.println("The corresponding number is 8 ");
        }
        else if(num.equalsIgnoreCase("w") || num.equalsIgnoreCase("x") || num.equalsIgnoreCase("y") || num.equalsIgnoreCase("z")) { 
            System.out.println("The corresponding number is 9 ");
        }
        else
            System.out.println(num + " is an invalid input ");  
        }
    }

(4.25)(生成车牌号码)

package javahomework;

public class java0425 {
    public static void main(String[] args) {

        String word1_str = "";
        String num1_str = "";
        for(int i = 0; i < 3; i++) {
            int word = (int)('A' + (int)(Math.random()*26));
            char word1 = (char) word;
            word1_str = word1_str + String.valueOf(word1).toString();
        }
        for(int i = 0; i < 4;i++) {
            int number = (int)(Math.random()*10);
             num1_str = num1_str + String.valueOf(number).toString();
        }
        System.out.println("The car's number is " + word1_str + num1_str);
    }

}

(4.26)(财务应用:货币单位)

package javahomework;
import java.util.Scanner;
public class java0426 {
    public static void main(String[] args) {
        Scanner input=new Scanner(System.in);
        System.out.print("Enter an amount(for example:11.56): ");
        String str=input.nextLine();
        if(str.indexOf('.')==-1){
            if(str.equals("0")) {
                System.out.println("no dollar no cent");
            }
            else if(str.equals("1") ) {
                System.out.println(str +" dollar"+" no cent");
            }
            else
                System.out.println(str +" dollars"+" no cent");
        }
         else if(str.indexOf('.')!=-1){
            String dollar=str.substring(0,str.indexOf('.'));
            String cent=str.substring(str.indexOf('.')+1);
            if (dollar.equals("0")) {
                if (cent.equals("0")) {
                    System.out.println("no dollar no cent");
                }
                else if(cent.equals("1")) {
                     System.out.println("no dollar "+cent+" cent");
                }
                else 
                    System.out.println("no dollar "+cent+" cents");
            }
            else if (dollar.equals("1")) {
                if (cent.equals("0")) {
                    System.out.println(dollar +" dollar no cent");
                }
                else if(cent.equals("1")) {
                     System.out.println(dollar +" dollar "+cent+" cent");
                }
                else 
                    System.out.println(dollar +" dollar "+cent+" cents");
            }
            else {
                if (cent.equals("0")) {
                    System.out.println(dollar +" dollars no cent");
                }
                else if(cent.equals("1")) {
                     System.out.println(dollar +" dollars "+cent+" cent");
                }
                else 
                    System.out.println(dollar +" dollars "+cent+" cents");
            }

        }
    }
}

你可能感兴趣的:(Java)