《java 语言程序设计》第2章编程练习

2.1

public class test {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        double f, c;
        c = input.nextDouble();
        f = (9.0/5)*c+32;
        System.out.println(f);
    }
}

2.2

public class test {
    public static void main(String[] args) {
        double r, h;
        final double PI = 3.1415925;
        System.out.println("Enter the radius and length of a cylinder: ");
        Scanner input = new Scanner(System.in);
        r = input.nextDouble();
        h = input.nextDouble();
        System.out.println("The area is " + PI*r*r);
        System.out.println("The volume is " + PI*r*r*h);
    }
}

2.3

public class test {
    public static void main(String[] args) {
        double f, m;
        Scanner input = new Scanner(System.in);
        System.out.println("Enter a value for feet: ");
        f = input.nextDouble();
        System.out.println(f + " feet is " + 0.305 *f + " meters");
    }
}

2.4

public class test {
    public static void main(String[] args) {
        double p, k;
        Scanner input = new Scanner(System.in);
        System.out.println("Enter a number in pounds: ");
        p = input.nextDouble();
        System.out.println(p + " pounds is " + 0.454 * p + " kilograms");
    }
}

2.6

public class test {
    public static void main(String[] args) {
        int n, sum, t;
        Scanner input = new Scanner(System.in);
        System.out.println("Enter a number between 0 and 1000: ");
        n = input.nextInt();
        sum = 0;
        t = n % 10;
        while(t != 0) {
            sum += t;
            n /= 10;
            t = n % 10;
        }
        System.out.println("The sum of the digits is " + sum);
    }
}

2.7

public class test {
    public static void main(String[] args) {
        int m = 0;
        int years, days, t;
        System.out.println("Enter the number of minutes: ");
        Scanner input = new Scanner(System.in);
        m = input.nextInt();
        t = (m / 60) / 24;
        years = t / 365;
        days = t % 365;
        System.out.println(m + " minutes is approximately " + years + " years and " + days + "days.");
    }
}

2.8

public class test {
    public static void main(String[] args) {
        int n;
        char c;
        Scanner input = new Scanner(System.in);
        System.out.print("Enter an ASCII code: ");
        n = input.nextInt();
        c = (char)n;
        System.out.println("The character for ASCII code " + n + " is " + c);

    }
}

2.11

public class test {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("Enter employee's name: ");
        String name = input.next();
        System.out.println("Enter number of hours worked in a week: ");
        float hours = input.nextFloat();
        System.out.println("Enter hourly pay rate: ");
        float payRate = input.nextFloat();
        System.out.println("Enter federal tax withholding rate: ");
        float ftwr = input.nextFloat();
        System.out.println("Enter state tax withholding rate: ");
        float stwr = input.nextFloat();
        System.out.println("Employee Name " + name);
        System.out.println("Hours Worked " + hours);
        System.out.println("Pay Rate: $" + payRate);
        System.out.println("Gross Pay: $" + hours * payRate);
        System.out.println("Deductions:");
        System.out.println("  Federal Withholding (" + ftwr * 100 +"%): $" + payRate * ftwr);
        System.out.println("  State Withholding (" + stwr * 100 +"%): $" + payRate * stwr);
        System.out.println("  Total Deduction: $" +  payRate * (ftwr + stwr);
    }
}

2.12

public class test {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("Enter balance and interest rate (e.g., 3 for 3%): ");
        double balance = input.nextDouble();
        double rate = input.nextDouble();
        System.out.printf("The interest is %.4f", balance * (rate / 1200));
    }
}

2.13

public class test {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        //System.out.println("Enter balance and interest rate (e.g., 3 for 3%): ");
        System.out.print("Enter investment amount: ");
        double investmount = input.nextDouble();
        System.out.print("Enter monthly interest rate: ");
        double rate = input.nextDouble();
        System.out.print("Enter number of years: ");
        int year = input.nextInt();
        double s = investmount * Math.pow((1 + rate / 100), (year * 12));
        System.out.println("Accumulated value is " + s);
    }
}

2.14

public class test {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter weigth in pounds: ");
        float weigth = input.nextFloat();
        System.out.print("Enter heigth in inches: ");
        float height = input.nextFloat();
        System.out.println("BMI is " + 0.45359237 * weigth / Math.pow(height * 0.0254, 2));
    }
}

2.15

public class test {
    public static void main(String[] args) {
        double t, s;
        s = t = 0;
        Scanner input = new Scanner(System.in);
        for(int i = 0; i < 6; i++) {
            s = (100 + t) * (1 + 0.00417);
            t = s;
        }
        System.out.println("After six months, result is: " + s);
    }
}

2.16

public class test {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter the amount of water in kilogram: " );
        double m = input.nextDouble();
        System.out.print("Enter the initial temperature: " );
        double it = input.nextDouble();
        System.out.print("Enter the final temperature: " );
        double ft = input.nextDouble();
        System.out.println("The energy needed is " + m * (ft - it) * 4184);
    }
}

2.17

public class test {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter the temperature in Fahrenheit: " );
        double f = input.nextDouble();
        System.out.print("Enter the wind miles per hour: ");
        double speed = input.nextDouble();
        System.out.println("The wind chill index is " + (35.74 + 0.6215 * f - 35.75 * Math.pow(speed, 0.16) + 0.427 * f * Math.pow(speed, 0.16)));
    }
}

2.18

public class test {
    public static void print() {
        System.out.print("     ");
    }
    public static void main(String[] args) {
        System.out.println("a     b     pow(a, b)");
        for(int i = 1; i < 6; i++) {
            System.out.print(i);
            print();
            System.out.print(i + 1);
            print();
            System.out.println((int)Math.pow(i, i +1));
        }
    }
}
时间: 2024-09-21 07:12:12

《java 语言程序设计》第2章编程练习的相关文章

java-Java语言程序设计第4章编程练习题1求问

问题描述 Java语言程序设计第4章编程练习题1求问 import java.util.Scanner; public class Practice { public static void main(String[] args) { Scanner input = new Scanner(System.in); int data = 0; int positive = 0; int negative = 0; int sum = 0; int count = 0; System.out.pri

100分求java语言程序设计进阶篇pdf

问题描述 求java语言程序设计进阶篇pdf 解决方案 解决方案二:同求啊!!!解决方案三:这个网上是没有的,我也在网上找过,我建议你去网上找java核心技术<上下卷>pdf这本书写的也是不错的,,这个网上有电子书的,,这两本书配合着java编程思想,相当的不错的解决方案四:真正的进阶是需要项目练习的,纸上得来终觉浅解决方案五:引用2楼xinzailiulei的回复: 这个网上是没有的,我也在网上找过,我建议你去网上找java核心技术<上下卷>pdf这本书写的也是不错的,,这个网上

《java 语言程序设计》第1章编程练习

1.1 public class test { public static void main(String[] args) { System.out.println("Welcome to java"); System.out.println("Welcome to Computer Science"); System.out.println("Programming is fun"); } } 1.2 public class test {

《java 语言程序设计》第3、4章编程练习

3.1 public class test { public static void main(String[] args) { System.out.println("Enter a, b, c: "); Scanner input = new Scanner(System.in); double a = input.nextDouble(); double b = input.nextDouble(); double c = input.nextDouble(); double d

求java语言程序设计进阶篇课后复习题和编程题答案

问题描述 各位好心人,帮忙发到我邮箱,谢谢了 解决方案

求 《java语言程序设计_进阶篇》课后完整答案

问题描述 如题,可以的话发到54438561@qq.com.或者给个淘宝网站什么的,我去买也行啊.谢谢啊. 解决方案 解决方案二:不知道,从未看过这类东西

java-Java语言程序设计第八版基础篇的编程练习

问题描述 Java语言程序设计第八版基础篇的编程练习 为什么按照下面的运行实例的数进行输入所得的结果却不同import java.util.Scanner; public class FutureInvestmentValue { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print(""Enter investment amount:"

《Java 7程序设计入门经典》一导读

前 言 本书通过Java语言来讲解编程的基础知识.该书假设读者之前没有任何编程经验.本书首先介绍最基本的知识,例如,如何编译和运行Java程序:接着,讨论关键字.运算符和一些形成Java语言的构造.本书还会涉及Java的应用编程接口(Application Programming Interface,API)库.其中,包括Swing框架,它可以用来创建具有图形用户界面(Graphical User Interface,GUI)的程序.还包括集合框架,它可以用来存储对象集合.简而言之,本书旨在对J

java语言程序设计基础篇4.1

问题描述 java语言程序设计基础篇4.1 java语言程序设计基础篇4.1java语言程序设计基础篇4.1java语言程序设计基础篇4.1 解决方案 除非你有先天性智力缺陷,否则这种问题都不会的基本都是自己根本不学的.既然你都不想学,你还提什么问题,早点回家算了,可怜你父母的学费. 解决方案二: <Java语言程序设计(基础篇)>读书笔记(一)<Java语言程序设计(基础篇)>读书笔记(四)java语言程序设计-基础篇-4.21习题