博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java语言程序设计基础篇第10版第5章习题答案
阅读量:4691 次
发布时间:2019-06-09

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

5.1  1 public class Demo { 2     public static void main(String[] args) { 3         // 创建一个输入对象 4         java.util.Scanner input = new java.util.Scanner(System.in); 5         System.out.print("Enter an integer,the input ends if it is 0: "); 6         //输入一串整数以空格隔开,如果输入0,程序结束 7         int num = input.nextInt(); 8         //定义正数与负数的个数变量,定义输入值得总和(不包括0) 9         int positiveNum = 0, negativeNum = 0;10         double sum = 0;11         //判断输入的第一个整数是否为0,如果不是,继续判断,如果是,直接else12         if (num != 0) {13             //读入的整数计算正数个数、负数个数、总和,直到读入为0跳出循环14             while (num != 0) {15                 if (num > 0)16                     positiveNum++;17                 else18                     negativeNum++;19                 sum += num;20                 num = input.nextInt();21             }22             //输出相应的正数、负数、总和和平均数的值23             System.out.println("The number of positives is " + positiveNum);24             System.out.println("The number of negatives is " + negativeNum);25             System.out.println("The total is " + sum);26             System.out.println("The average is " + sum27                     / (positiveNum + negativeNum));28         }else29             System.out.println("No numbers are entered except 0");30     }31 }

 

5.2  1 public class Demo { 2     public static void main(String[] args) { 3         // 定义问题的数量为10 4         final int NUMBER_OF_QUESTIONS = 10; 5         // 定义变量存放正确的个数,定义变量存放循环次数 6         int correctCount = 0; 7         int count = 1; 8         // 定义开始时间 9         long startTime = System.currentTimeMillis();10         // 创建一个输入对象11         java.util.Scanner input = new java.util.Scanner(System.in);12         // 随机产生两个整数,循环10次13         while (count <= 10) {14             // 定义两个整数变量,存放随机产生的1~15内的整数15             int num1 = (int) (Math.random() * 15) + 1;16             int num2 = (int) (Math.random() * 15) + 1;17             // 输入答案18             System.out.print("What is " + num1 + " + " + num2 + "? ");19             int answer = input.nextInt();20             // 如果回答正确,正确的个数加一,回答不正确,输出正确的结果21             if (num1 + num2 == answer) {22                 System.out.println("You are correct!");23                 correctCount++;24             } else25                 System.out.println("Your answer is wrong.\n" + num1 + " + "26                         + num2 + " should be " + (num1 + num2));27             // 循环次数加一28             count++;29         }30         // 定义结束时间31         long endTime = System.currentTimeMillis();32         // 计算测验时间33         long testTime = endTime - startTime;34         // 输出正确答案的个数与测验时间(单位秒)35         System.out.println("Correct count is " + correctCount36                 + "\nTest time is " + testTime / 1000 + " seconds");37     }38 }

 

转载于:https://www.cnblogs.com/lirun/p/5988959.html

你可能感兴趣的文章
C. Dasha and Password 预处理 + dp
查看>>
hadoop Yarn运行机制
查看>>
python3操作MySQL实现数据驱动完整实例
查看>>
四子连棋
查看>>
.9 图片的创建
查看>>
xgboost gbdt特征点分烈点
查看>>
python实现单例模式及应用场景
查看>>
简单Servlet页面交互代码解读
查看>>
架构学习提炼笔记(1):架构设计的基本概念以及设计的三大原则
查看>>
图的最短路径-----------SPFA算法详解(TjuOj2831_Wormholes)
查看>>
crm02 stark组件(自定义Django-admin)之运行流程
查看>>
IOS-WebViewJavascriptBridge使用说明
查看>>
201506051031_《JavaScript权威指南》(p104-143)
查看>>
QT && GDAL
查看>>
【BZOJ-1026】windy数 数位DP
查看>>
Java NIO系列教程(三) Buffer
查看>>
LeetCode的一道题的个人见解
查看>>
IOS的滑动菜单(Sliding Menu)的具体写法(附代码)
查看>>
Gitbook 使用入门
查看>>
opencv测试分类器性能 opencv_performance.exe 使用方法 及参数含义_majian_新浪博客
查看>>