博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
《Java语言程序设计与数据结构》编程练习答案(第五章)(一)
阅读量:4169 次
发布时间:2019-05-26

本文共 5267 字,大约阅读时间需要 17 分钟。

《Java语言程序设计与数据结构》编程练习答案(第五章)(一)

英文名:Introduction to Java Programming and Data Structures, Comprehensive Version, 11th Edition

5.1

import java.util.Scanner;public class book {
public static void main(String[] args) {
Scanner input = new Scanner(System.in); System.out.print("Enter an integer, the input ends if it is 0: "); int pos = 0; int neg = 0; double sum = 0; int amo = 0; while(true) {
int tmp = input.nextInt(); if(tmp==0) break; else {
amo++; sum+=tmp; if(tmp>0) pos++; else neg++; } } double ave = sum/amo; if(amo==0) System.out.println("No numbers are entered except 0"); else {
System.out.println("The number of positives is " + pos); System.out.println("The number of negatives is " + neg); System.out.println("The total is " + sum); System.out.println("The average is " + ave); } }}

5.2

import java.util.Scanner;public class book {
public static void main(String[] args) {
final int NUMBER_OF_QUESTIONS=10; int correctCount = 0; int count = 0; long startTime = System.currentTimeMillis(); String output=""; Scanner input = new Scanner(System.in); while(count

5.3

public class book {
public static void main(String[] args) {
System.out.println("千克 磅"); for(int i=1;i<=100;i++) System.out.printf("%-6d%8.1f\n",i*2-1,(2*i-1)*2.2); }}

5.4

public class book {
public static void main(String[] args) {
System.out.println("英里 千米"); for(int i=1;i<=10;i++) System.out.printf("%-11d%-6.3f\n",i,i*1.609); }}

5.5

public class book {
public static void main(String[] args) {
System.out.println("千克 磅 磅 千克"); for(int i=1;i<=100;i++) System.out.printf("%-5d%8.1f %-4d%8.2f\n",2*i-1,(2*i-1)*2.2,5*i+15,(5*i+15)/2.2); }}

5.6

public class book {
public static void main(String[] args) {
System.out.println("英里 千米 千米 英里"); for(int i=1;i<=10;i++) System.out.printf("%-8d%-7.3f %-8d%-6.3f\n",i,i*1.609,5*i+15,(5*i+15)/1.609); }}

5.7

public class book {
public static void main(String[] args) {
double ass = 10000; for(int i=0;i<10;i++) ass*=(1+0.05); double oldAss = ass; double sum = 0; for(int i=0;i<4;i++) {
sum+=ass; ass*=(1+0.05); } System.out.printf("Ass after 10 years is %.2f\n",oldAss); System.out.printf("The total cost of 4 asses is %.2f\n",sum); }}

5.8

import java.util.Scanner;public class book {
public static void main(String[] args) {
System.out.println("Enter the number of girls: "); Scanner input = new Scanner(System.in); int num = input.nextInt(); String name = ""; double cup = 0; for(int i=0;i
cup) {
cup=tmp; name=tname; } } System.out.println("The hottest girl is "+name); }}

5.9

import java.util.Scanner;public class book {
public static void main(String[] args) {
System.out.print("Enter the number of girls: "); Scanner input = new Scanner(System.in); int num = input.nextInt(); String name = ""; String name2 = ""; double cup = 0; double cup2 = 0; for(int i=0;i
cup) {
cup2=cup; name2 = name; cup=tmp; name=tname; } else if(tmp>cup2) {
cup2=tmp; name2=tname; } } System.out.println("The hottest girl is "+name); System.out.println("The second hottest girl is "+name2); }}

5.10

public class book {
public static void main(String[] args) {
int count=0; int base = 0; for(int i=100;i<=1000;i++) {
if(i%5==0&&i%6==0) {
count++; System.out.print(i+" "); } if(count%10==0&&count!=base) {
System.out.print("\n"); base+=10; } } }}

5.11

public class book {
public static void main(String[] args) {
int count=0; int base = 0; for(int i=100;i<=1000;i++) {
if(i%5==0^i%6==0) {
count++; System.out.print(i+" "); } if(count%10==0&&count!=base) {
System.out.print("\n"); base+=10; } } }}

5.12

public class book {
public static void main(String[] args) {
int n = 0; while(n*n<12000) n++; System.out.println("The n is "+n); }}

5.13

public class book {
public static void main(String[] args) {
int n = 0; while(n*n*n<12000) n++; System.out.println("The n is "+(n-1)); }}

转载地址:http://zuwai.baihongyu.com/

你可能感兴趣的文章
git学习笔录
查看>>
Activity类中7个与活动生命周期回调有关的方法
查看>>
jwt与token+redis,哪种方案更好用?
查看>>
Comparator接口
查看>>
在二叉树中找到一个节点的后继节点
查看>>
寻找第K大
查看>>
String.trim
查看>>
缓存行 伪共享
查看>>
400 : perceived to be a client error 错误
查看>>
Establishing SSL connection without server's identity verification is not recommended
查看>>
扫描包不存在:pojo类找不到
查看>>
c语言中计算数组长度的方法
查看>>
java 数组定义
查看>>
java中的&和&&的区别
查看>>
Java的位运算符
查看>>
BufferedReader与Scanner的区别
查看>>
java String于常量池中的介绍
查看>>
java Text 错误: 找不到或无法加载主类 Text
查看>>
XShell连接ubantu:给ubantu安装ssh
查看>>
c语言的null和0
查看>>