《java 语言程序设计》第3、4章编程练习
3.1
public class test {
public static void main(String[] args) {
System.out.println("Enter a, b, c: ");
Scanner input = new Scanner(System.in);
double a = input.nextDouble();
double b = input.nextDouble();
double c = input.nextDouble();
double delta = b * b - 4 * a * c;
double t = Math.pow(delta, 0.5);
if(delta > 0) {
double x1 = (-b + t) / 2;
double x2 = (-b - t) / 2;
System.out.println("The roots are " + x1 + " and " + x2);
} else if (delta == 0) {
System.out.println("The root is " + -b / (2 * a));
} else {
System.out.println("The equation has no real roots");
}
}
}
3.2
public class test {
public static void main(String[] args) {
System.out.println("Enter an integer: ");
Scanner input = new Scanner(System.in);
int n = input.nextInt();
System.out.print("Is " + n + " an even number? ");
if(n % 2 == 0)
System.out.println("true");
else
System.out.println("false");
}
}
3.3
public class test {
public static void main(String[] args) {
System.out.print("Enter a, b, c, d, e, f: ");
Scanner input = new Scanner(System.in);
double a = input.nextDouble();
double b = input.nextDouble();
double c = input.nextDouble();
double d = input.nextDouble();
double e = input.nextDouble();
double f = input.nextDouble();
double fm = a * d - b * c;
if(fm == 0) {
System.out.println("The equation has no solution");
} else {
System.out.println("a is " + ((e * d - b * f) / fm) + " and y is " + ((a * f - e * c) / fm));
}
}
}
3.4
public class test {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int a = (int)(Math.random() * 100);
int b = (int)(Math.random() * 100);
System.out.print("Enter the sum of the two integer(0~100): " + a + " and " + b + ": ");
int c = input.nextInt();
if(c == a + b)
System.out.println("True");
else
System.out.println("False");
}
}
3.5
public class test {
public static int judge(int year, int month) {
boolean leap;
leap = (year % 4 ==0 && year % 100 != 0) || (year % 400 == 0);
if(month == 2) {
if(leap) return 29;
else return 28;
} else if(month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
return 31;
} else {
return 30;
}
}
public static void main(String[] args) {
String[] months = {" ", "January","February","March","April",
"May","June","July","August","September",
"October","November","December"};
System.out.print("Please inpit month and year: ");
Scanner input = new Scanner(System.in);
int month = input.nextInt();
int year = input.nextInt();
System.out.println(months[month] + " " + year + " has " + judge(year, month) + " days");
}
}
4.7
public class test {
public static void main(String[] args) {double n = 10000;
double s1, s2, t;
s1 = s2 = 0;
t = 1;
final double rate = 0.05;
for(int i = 1; i < 11; i++) {
t *= (1 + rate);
}
s1 = n * t;
System.out.println("s1 = " + s1);
}
}
4.16
public class test {
public static void main(String[] args) {
System.out.print("Enter a number: ");
Scanner input = new Scanner(System.in);
int n = input.nextInt();
int i = 2;
while(true) {
while(n % i == 0 && n != i) {
System.out.print(i + ", ");
n /= i;
}
i++;
if(n == i) {
System.out.println(i);
break;
}
}
}
}
4.25
public class test {
public static double countPi(int n) {
double pi = 0;
double t;
int m=1;
for(int i = 1; i < n; i++) {
t=1.0/(2*i-1);
t*=m;
pi+=t;
m*=-1;
}
pi *= 4;
return pi;
} public static void main(String[] args) {
System.out.print("Enter a number: ");
Scanner input = new Scanner(System.in);
for(int i = 10000; i <= 100000; i++) {
System.out.println("pi(" + i + ") = " + countPi(i));;
}
}
}
4.27
public class test {
public static boolean isLeapYear(int n) {
return ((n % 4 == 0 && n % 100 != 0) || n % 400 == 0);
} public static void main(String[] args) {
int n = 0;
for(int i = 2001; i < 2100; i++) {
if(isLeapYear(i)) {
n++;
if(n % 11 == 0) {
System.out.println("\n");
} else {
System.out.print(i + " ");
} }
}
}
}
4.33
public class test {
public static boolean test(int n) {
int i, sum;
int m = n / 2;
sum = 0;
for(i = 1; i <= m; i++) {
if(n % i == 0)
sum += i;
}
if(sum == n)
return true;
else
return false;
}
public static void main(String[] args) {
for(int i = 2; i < 10000; i++) {
if(test(i))
System.out.print(i + "\n");
}
}
}
4.41
public class test {
public static void main(String[] args) {
int n, count , max, t;
Scanner input = new Scanner(System.in);
System.out.println("Enter a number: ");
n = input.nextInt();
t = max = n;
count = 0;
while(t != 0) {
if(t > max) {
count = 1;
max = t;
} else {
count++;
}
System.out.println("Enter a number: ");
t = input.nextInt();
}
System.out.println("max= " + max + ", count= " + count);
}
}
《java 语言程序设计》第3、4章编程练习的更多相关文章
- 《java 语言程序设计》第1章编程练习
1.1 public class test { public static void main(String[] args) { System.out.println("Welcome to ...
- 《java 语言程序设计》第2章编程练习
2.1 public class test { public static void main(String[] args) { Scanner input = new Scanner(System. ...
- Java语言程序设计(基础篇)第一章
第一章 计算机.程序和Java概述 1.1 引言 什么是程序设计呢? 程序设计就是创建(或者开发)软件,软件也称为程序. 1.2 什么是计算机 计算机是存储和处理数据的电子设备,计算机包括硬件(har ...
- Java语言程序设计(基础篇)第二章
第二章 基本程序设计 2.2 编写简单的程序 1.变量名尽量选择描述性的名字(descriptive name). 2.实数(即带小数点的数字)在计算机中使用一种浮点的方法来表示.因此,实数也称为浮点 ...
- 《python语言程序设计》_第二章编程题
2.1 程序: Celsius=eval(input("Enter a degree in Celsius:"))#输入摄氏度的值Celsiusfahrenheit =(9/5)* ...
- 《python语言程序设计》_第一章编程题
题目1.1 :显示"welcome to python " 答案:print('welcome to python') 题目1.2:显示"welcome to pytho ...
- Java语言程序设计-助教篇
1. 给第一次上课(软件工程)的老师与助教 现代软件工程讲义 0 课程概述 给学生:看里面的第0个作业要求 2. 助教心得 美国视界(1):第一流的本科课堂该是什么样?(看里面的助教部分) 助教工作看 ...
- 0031 Java学习笔记-梁勇著《Java语言程序设计-基础篇 第十版》英语单词
第01章 计算机.程序和Java概述 CPU(Central Processing Unit) * 中央处理器 Control Unit * 控制单元 arithmetic/logic unit /ə ...
- 《JAVA语言程序设计》上课笔记
教学目标:1.使学生了解JAVA课程的性质.定位.作用:为什么要学习JAVA?让学生知道如何学好JAVA: 教学内容: 一. 问几个问题 1. 你们到这里来干什么 ...
- Java语言程序设计复习提纲
这是我在准备Java考试时整理的提纲,如果是通过搜索引擎搜索到这篇博客的师弟师妹,建议还是先参照PPT和课本,这个大纲也不是很准确,自己总结会更有收获,多去理解含义,不要死记硬背,否则遇到概念辨析题 ...
随机推荐
- .htaccess文件
前言 看了几篇文章,发现自己对于如何维护普通的服务器安全完全不会,先从简单的.htaccess来研究吧 .htaccess文件的作用,就是更改httpd.ini文件中的配置,但作用范围仅限当前文件夹 ...
- JavaScript的类型体系
一:总体的类型系 基本类型:数字类型(number),字符串类型(string),布尔类型(boolean); 复合类型:对象(对象,函数,数组等); 无类型:null(有定义),undefined( ...
- java8新特性——Stream API
Java8中有两大最为重要得改变,其一时Lambda表达式,另外就是 Stream API了.在前面几篇中简单学习了Lambda表达式得语法,以及函数式接口.本文就来简单学习一下Stream API( ...
- codeforces 220 C. Game on Tree
题目链接 codeforces 220 C. Game on Tree 题解 对于 1节点一定要选的 发现对于每个节点,被覆盖切选中其节点的概率为祖先个数分之一,也就是深度分之一 代码 #includ ...
- BZOJ2938 POI2000病毒
我们不能让重复过的字串出现在无限串上(就叫这个了...) 也就是要自动机一直能匹配但就是匹配不到,那么就是在自动机上找一个环. dfs判环即可.注意是个有向图. #include<bits/st ...
- 【坐标变换】【二维偏序】【线段树】Gym - 100820G - Racing Gems
题意:第一象限有n个点,你从x正半轴任选一个位置出发,vy恒定,vx可以任意变化,不过只能在-vy/r到vy/r之间变化,问你最多能经过多少个点. 暴力dp是n^2,不可取. 注意到,一个点,所能到达 ...
- bzoj 3073: [Pa2011]Journeys -- 线段树优化最短路
3073: [Pa2011]Journeys Time Limit: 20 Sec Memory Limit: 512 MB Description Seter建造了一个很大的星球,他准备建 ...
- 2015 UESTC 数据结构专题N题 秋实大哥搞算数 表达式求值/栈
秋实大哥搞算数 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/problem/show/1074 Des ...
- 卡尔曼滤波(Kalman Filter)在目标边框预测中的应用
1.卡尔曼滤波的导论 卡尔曼滤波器(Kalman Filter),是由匈牙利数学家Rudolf Emil Kalman发明,并以其名字命名.卡尔曼出生于1930年匈牙利首都布达佩斯.1953,1954 ...
- 关于图表第三方Charts的一些理解与总结
最近项目中用到了很多的图表,如柱状图,线状图,饼状图等等.接触到了一个新的第三方Charts,在做图方面确实非常强大,在使用了一段时间后,今天对他进行一个小的总结,也是自己的一点小理解. 关于char ...