【Java自学】5、Java语言程序设计(基础篇)第二章习题答案

本文提供了多个Java编程的基础习题,包括温度转换、几何计算、财务应用、物理问题等,涵盖了数据输入、数学运算、条件判断和循环结构等核心概念。每个习题都附带了代码实现,适合初学者巩固基础。
摘要由CSDN通过智能技术生成

1.(将摄氏温度转换为华氏温度)编写程序,从控制台读入double型的摄氏温度,然后将其转换为华氏温度,并且显示结果:
代码如下:

public class Exercise1 {
   
	public static void main(String [] args) {
   
		@SuppressWarnings("resource")
		Scanner input = new Scanner(System.in);
		System.out.print("Enter a degree in Celsius:");
		double celsius = input.nextDouble();
		double fahrenheit = (9.0 / 5) * celsius + 32;
		System.out.print(celsius + " Celsius is "  + fahrenheit + " Fahrenheit");
	}
}

2.(计算圆柱体的体积)编写程序,读入圆柱体的半径和高,计算出圆柱的体积:
代码如下:

import java.util.*;

public class Exercise2 {
   
	public static void main(String [] args) {
   
		final double PI = 3.14159;
		
		@SuppressWarnings("resource")
		Scanner input = new Scanner(System.in);
		System.out.print("Enter the radius and length of a cylinder:");
		double radius = input.nextDouble();
		double length = input.nextDouble();
		
		double area = radius * radius * PI;//圆柱底面积
		double volume = area * length;
		
		System.out.println("The area is " + String.format("%.4f", area));//这里输出的是圆柱底面积
		System.out.print("The volume is " + String.format("%.1f", volume));
	}
}

3.(将英尺转换为米)编写程序,读入英尺数,将其转换为米数并显示结果。一英尺等于0.305米。
代码如下:

import java.util.*;

public class Exercise3 {
   
	public static void main(String [] args) {
   
		@SuppressWarnings("resource")
		Scanner input = new Scanner(System.in);
		System.out.print("Enter a value for feet: ");
		double feet = input.nextDouble();
		
		double meter = feet * 0.305;
		
		System.out.print(feet + " feet is " + meter + " meters");
	}
}

4.(将磅转换为千克)编写程序,将磅数转化为千克数。一磅等于0.454千克。
代码如下:

import java.util.Scanner;

public class Exercise4 {
   
	public static void main(String [] args) {
   
		@SuppressWarnings("resource")
		Scanner input = new Scanner(System.in);
		System.out.print("Enter a number in pounds: ");
		double pound = input.nextDouble();
		
		double kg = pound * 0.454;
		
		System.out.print(pound + " pounds is " + kg + " kilograms");
	}
}

5.(财务应用程序:计算小费)编写一个程序,读入一笔费用与酬金率,计算酬金和总钱数。例如:如果用户输入10作为费用,15%作为酬金率,计算结果显示酬金为$1.5,总费用为$11.5。
代码如下:

public class Exercise5 {
   
	public static void main(String[] args) {
   
		@SuppressWarnings("resource")
		Scanner input = new Scanner(System.in);
		System.out.print("Enter the subtotal and a gratuity rate:");
		double subtotal = input.nextDouble();
		double gratuityRate = input.nextDouble();
		
		double gratuity = subtotal * gratuityRate * 0.01;
		double total = subtotal + gratuity;
		System.out.print("The gratuity is $" + gratuity + " and total is $" + total);
	}
}

6.(求一个整数各位数的和)编写程序,读取一个在0和1000之间的整数,并将该整数的各位数字相加。
代码如下:

import java.util.Scanner;

public class Exercise6 {
   
	public static void main(String[] args) {
   
		@SuppressWarnings("resource")
		Scanner input = new Scanner(System.in);
		System.out.print("Enter the number between 0 and 1000:");
		int number = input.nextInt();
		
		int onesPlace = number % 10;
		int tensPlace = number % 100 / 10;
		int hundredsPlace = number % 1000 / 100;
		int thousandsPlace = number % 10000 / 1000;
		int sumOfTheDigits = onesPlace + tensPlace + hundredsPlace + thousandsPlace;
		System.out.print("The sum of the digits is " +sumOfTheDigits);
	}
  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值