《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)); } } }
作者:wuyudong
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.
如果觉得本文对您有帮助,可以对作者进行小额【赞助】