3.1

  1. public class test {
  2. public static void main(String[] args) {
  3. System.out.println("Enter a, b, c: ");
  4. Scanner input = new Scanner(System.in);
  5. double a = input.nextDouble();
  6. double b = input.nextDouble();
  7. double c = input.nextDouble();
  8. double delta = b * b - 4 * a * c;
  9. double t = Math.pow(delta, 0.5);
  10. if(delta > 0) {
  11. double x1 = (-b + t) / 2;
  12. double x2 = (-b - t) / 2;
  13. System.out.println("The roots are " + x1 + " and " + x2);
  14. } else if (delta == 0) {
  15. System.out.println("The root is " + -b / (2 * a));
  16. } else {
  17. System.out.println("The equation has no real roots");
  18. }
  19. }
  20. }

3.2

  1. public class test {
  2. public static void main(String[] args) {
  3. System.out.println("Enter an integer: ");
  4. Scanner input = new Scanner(System.in);
  5. int n = input.nextInt();
  6. System.out.print("Is " + n + " an even number? ");
  7. if(n % 2 == 0)
  8. System.out.println("true");
  9. else
  10. System.out.println("false");
  11. }
  12. }

3.3

  1. public class test {
  2. public static void main(String[] args) {
  3. System.out.print("Enter a, b, c, d, e, f: ");
  4. Scanner input = new Scanner(System.in);
  5. double a = input.nextDouble();
  6. double b = input.nextDouble();
  7. double c = input.nextDouble();
  8. double d = input.nextDouble();
  9. double e = input.nextDouble();
  10. double f = input.nextDouble();
  11. double fm = a * d - b * c;
  12. if(fm == 0) {
  13. System.out.println("The equation has no solution");
  14. } else {
  15. System.out.println("a is " + ((e * d - b * f) / fm) + " and y is " + ((a * f - e * c) / fm));
  16. }
  17. }
  18. }

3.4

  1. public class test {
  2. public static void main(String[] args) {
  3. Scanner input = new Scanner(System.in);
  4. int a = (int)(Math.random() * 100);
  5. int b = (int)(Math.random() * 100);
  6. System.out.print("Enter the sum of the two integer(0~100): " + a + " and " + b + ": ");
  7. int c = input.nextInt();
  8. if(c == a + b)
  9. System.out.println("True");
  10. else
  11. System.out.println("False");
  12. }
  13. }

3.5

  1. public class test {
  2. public static int judge(int year, int month) {
  3. boolean leap;
  4. leap = (year % 4 ==0 && year % 100 != 0) || (year % 400 == 0);
  5. if(month == 2) {
  6. if(leap) return 29;
  7. else return 28;
  8. } else if(month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
  9. return 31;
  10. } else {
  11. return 30;
  12. }
  13. }
  14. public static void main(String[] args) {
  15. String[] months = {" ", "January","February","March","April",
  16. "May","June","July","August","September",
  17. "October","November","December"};
  18. System.out.print("Please inpit month and year: ");
  19. Scanner input = new Scanner(System.in);
  20. int month = input.nextInt();
  21. int year = input.nextInt();
  22. System.out.println(months[month] + " " + year + " has " + judge(year, month) + " days");
  23. }
  24. }

4.7

  1. public class test {
  2. public static void main(String[] args) {double n = 10000;
  3. double s1, s2, t;
  4. s1 = s2 = 0;
  5. t = 1;
  6. final double rate = 0.05;
  7. for(int i = 1; i < 11; i++) {
  8. t *= (1 + rate);
  9. }
  10. s1 = n * t;
  11. System.out.println("s1 = " + s1);
  12. }
  13. }

4.16

  1. public class test {
  2. public static void main(String[] args) {
  3. System.out.print("Enter a number: ");
  4. Scanner input = new Scanner(System.in);
  5. int n = input.nextInt();
  6. int i = 2;
  7. while(true) {
  8. while(n % i == 0 && n != i) {
  9. System.out.print(i + ", ");
  10. n /= i;
  11. }
  12. i++;
  13. if(n == i) {
  14. System.out.println(i);
  15. break;
  16. }
  17. }
  18. }
  19. }

4.25

  1. public class test {
  2. public static double countPi(int n) {
  3. double pi = 0;
  4. double t;
  5. int m=1;
  6. for(int i = 1; i < n; i++) {
  7. t=1.0/(2*i-1);
  8. t*=m;
  9. pi+=t;
  10. m*=-1;
  11. }
  12. pi *= 4;
  13. return pi;
  14. }
  15.  
  16. public static void main(String[] args) {
  17. System.out.print("Enter a number: ");
  18. Scanner input = new Scanner(System.in);
  19. for(int i = 10000; i <= 100000; i++) {
  20. System.out.println("pi(" + i + ") = " + countPi(i));;
  21. }
  22. }
  23. }

4.27

  1. public class test {
  2. public static boolean isLeapYear(int n) {
  3. return ((n % 4 == 0 && n % 100 != 0) || n % 400 == 0);
  4. }
  5.  
  6. public static void main(String[] args) {
  7. int n = 0;
  8. for(int i = 2001; i < 2100; i++) {
  9. if(isLeapYear(i)) {
  10. n++;
  11. if(n % 11 == 0) {
  12. System.out.println("\n");
  13. } else {
  14. System.out.print(i + " ");
  15. }
  16.  
  17. }
  18. }
  19. }
  20. }

4.33

  1. public class test {
  2. public static boolean test(int n) {
  3. int i, sum;
  4. int m = n / 2;
  5. sum = 0;
  6. for(i = 1; i <= m; i++) {
  7. if(n % i == 0)
  8. sum += i;
  9. }
  10. if(sum == n)
  11. return true;
  12. else
  13. return false;
  14. }
  15. public static void main(String[] args) {
  16. for(int i = 2; i < 10000; i++) {
  17. if(test(i))
  18. System.out.print(i + "\n");
  19. }
  20. }
  21. }

4.41

  1. public class test {
  2. public static void main(String[] args) {
  3. int n, count , max, t;
  4. Scanner input = new Scanner(System.in);
  5. System.out.println("Enter a number: ");
  6. n = input.nextInt();
  7. t = max = n;
  8. count = 0;
  9. while(t != 0) {
  10. if(t > max) {
  11. count = 1;
  12. max = t;
  13. } else {
  14. count++;
  15. }
  16. System.out.println("Enter a number: ");
  17. t = input.nextInt();
  18. }
  19. System.out.println("max= " + max + ", count= " + count);
  20. }
  21. }

《java 语言程序设计》第3、4章编程练习的更多相关文章

  1. 《java 语言程序设计》第1章编程练习

    1.1 public class test { public static void main(String[] args) { System.out.println("Welcome to ...

  2. 《java 语言程序设计》第2章编程练习

    2.1 public class test { public static void main(String[] args) { Scanner input = new Scanner(System. ...

  3. Java语言程序设计(基础篇)第一章

    第一章 计算机.程序和Java概述 1.1 引言 什么是程序设计呢? 程序设计就是创建(或者开发)软件,软件也称为程序. 1.2 什么是计算机 计算机是存储和处理数据的电子设备,计算机包括硬件(har ...

  4. Java语言程序设计(基础篇)第二章

    第二章 基本程序设计 2.2 编写简单的程序 1.变量名尽量选择描述性的名字(descriptive name). 2.实数(即带小数点的数字)在计算机中使用一种浮点的方法来表示.因此,实数也称为浮点 ...

  5. 《python语言程序设计》_第二章编程题

    2.1 程序: Celsius=eval(input("Enter a degree in Celsius:"))#输入摄氏度的值Celsiusfahrenheit =(9/5)* ...

  6. 《python语言程序设计》_第一章编程题

    题目1.1 :显示"welcome to python " 答案:print('welcome to python') 题目1.2:显示"welcome to pytho ...

  7. Java语言程序设计-助教篇

    1. 给第一次上课(软件工程)的老师与助教 现代软件工程讲义 0 课程概述 给学生:看里面的第0个作业要求 2. 助教心得 美国视界(1):第一流的本科课堂该是什么样?(看里面的助教部分) 助教工作看 ...

  8. 0031 Java学习笔记-梁勇著《Java语言程序设计-基础篇 第十版》英语单词

    第01章 计算机.程序和Java概述 CPU(Central Processing Unit) * 中央处理器 Control Unit * 控制单元 arithmetic/logic unit /ə ...

  9. 《JAVA语言程序设计》上课笔记

    教学目标:1.使学生了解JAVA课程的性质.定位.作用:为什么要学习JAVA?让学生知道如何学好JAVA: 教学内容: 一.        问几个问题 1.             你们到这里来干什么 ...

  10. Java语言程序设计复习提纲

     这是我在准备Java考试时整理的提纲,如果是通过搜索引擎搜索到这篇博客的师弟师妹,建议还是先参照PPT和课本,这个大纲也不是很准确,自己总结会更有收获,多去理解含义,不要死记硬背,否则遇到概念辨析题 ...

随机推荐

  1. Java 读取目录下的所有文件

    package util; import java.io.File; import java.util.ArrayList; import java.util.List; import org.apa ...

  2. POJ 3735 Training little cats<矩阵快速幂/稀疏矩阵的优化>

    Training little cats Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 13488   Accepted:  ...

  3. codevs 2173 忠诚

    2173 忠诚  时间限制: 1 s  空间限制: 32000 KB  题目等级 : 钻石 Diamond   题目描述 Description 老管家是一个聪明能干的人.他为财主工作了整整10年,财 ...

  4. [APIO2015]巴厘岛的雕塑 --- 贪心 + 枚举

    [APIO2015]巴厘岛的雕塑  题目描述 印尼巴厘岛的公路上有许多的雕塑,我们来关注它的一条主干道. 在这条主干道上一共有\(N\)座雕塑,为方便起见,我们把这些雕塑从 1 到\(N\)连续地进行 ...

  5. [bzoj3625][Codeforces 250 E]The Child and Binary Tree(生成函数+多项式运算+FFT)

    3625: [Codeforces Round #250]小朋友和二叉树 Time Limit: 40 Sec  Memory Limit: 256 MBSubmit: 650  Solved: 28 ...

  6. [BZOJ4887][TJOI2017]可乐(DP+矩阵快速幂)

    题目描述 加里敦星球的人们特别喜欢喝可乐.因而,他们的敌对星球研发出了一个可乐机器人,并且放在了加里敦星球的1号城市上.这个可乐机器人有三种行为: 停在原地,去下一个相邻的城市,自爆.它每一秒都会随机 ...

  7. CodeForces - 1017D The Wu

    题面在这里! 比较显而易见的暴力,O(2^(2n) + 2^n * 100) 就可以直接做了 #include<bits/stdc++.h> #define ll long long us ...

  8. MySQL5.7添加授权账号及修改默认端口

    1.修改默认端口 打开配置文件 vim /etc/my.cnf 分别添加端口在client.mysql节点 [client] port=15099 [mysqld] port=15099 需要注意se ...

  9. HDU 4667 Building Fence(2013多校7 1002题 计算几何,凸包,圆和三角形)

    Building Fence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)To ...

  10. Linux命令-添加新硬盘,分区及挂载[转]

    http://www.cnblogs.com/qiyebao/p/4484370.html 转自:http://blog.chinaunix.net/uid-25829053-id-3067619.h ...