java语言程序设计(基础篇)第二章程序练习题答案

2.1(将摄氏温度转化为华氏温度)

import java.util.*; public class test { }

public static void main(String[] args) { }

// TODO Auto-generated method stub Scanner input = new Scanner(System.in);

System.out.print(\); double Celsius = input.nextDouble();

double Fahrenheit;

Fahrenheit = (9.0/5) * Celsius + 32;

System.out.println(Celsius + \ + Fahrenheit + \

Fahrenheit\);

2.2(计算圆柱体的体积)

import java.util.*;

public class test { }

1

public static void main(String[] args) { }

// TODO Auto-generated method stub Scanner input = new Scanner(System.in);

System.out.print(\); double radius = input.nextDouble(); double length =input.nextDouble();

double area = radius * radius * Math.PI; double volume = area * length;

System.out.println(\ + area); System.out.println(\ + volume);

2.3(将英尺转换为米)

import java.util.Scanner;

public class test { }

public static void main(String[] args) { }

// TODO Auto-generated method stub Scanner input = new Scanner(System.in); System.out.print(\); double feet = input.nextDouble();

double meters = feet * 0.305;

System.out.println(feet+ \ + meters + \);

2.4(将磅转换为千克)

import java.util.Scanner;

public class test { }

public static void main(String[] args) { }

// TODO Auto-generated method stub Scanner input = new Scanner(System.in);

System.out.print(\); double pounds = input.nextDouble();

double kilograms = pounds * 0.454;

System.out.println(pounds + \ + kilograms + \);

2.5(财务应用程序:计算消费)

import java.util.Scanner;

2

public class test { }

public static void main(String[] args) { }

// TODO Auto-generated method stub Scanner input = new Scanner(System.in);

System.out.print(\); double subtotal = input.nextDouble(); double Gratuity = input.nextDouble();

double gratuity = subtotal * Gratuity * 0.01; double total = gratuity + subtotal;

System.out.println(\ + gratuity + \

+ total);

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

import java.util.Scanner;

public class test { }

public static void main(String[] args) { }

// TODO Auto-generated method stub Scanner input = new Scanner(System.in);

System.out.print(\); int number = input.nextInt();

int sum = number % 10 + (number / 10) % 10 + (number / 100) % 10; System.out.println(\ + sum);

2.7(求出年数)

import java.util.Scanner;

public class test {

public static void main(String[] args) {

3

联系客服:779662525#qq.com(#替换为@) 苏ICP备20003344号-4