文章目录
- 前言
- 一、第一章 计算机 程序 和Java概述
- 二、第一章课后习题解析
-
- 1.1.(显示三条信息)编写程序,显示Welcome to Java,Welcome to Computer Science 和Programming is fun。
- 1.2.(显示五条信息)编写程序,显示Welcome to Java 五次。
- 1.3 (显示图案)编写一个程序,显示一个Java图案
- 1.4 (打印表格)编写程序,显示以下表格:
- 1.5(计算表达式)编写程序,显示以下公式的结果。
- 1.6(数列求和)编写程序,显示1+2+3+4+5+6+7+8+9的结果。
- 1.7(近似求p)可以使用以下公式计算p:
- 1.8(圆的面积和周长)编写程序,使用以下公式计算并显示半径为5.5的圆的面积和周长。
- 1.9(矩阵的面积和周长)编写程序,使用以下公式计算并显示宽度为4.5,高度为7.9的矩形的面积和周长。
- 1.10(以英里计的平均速度)假设一个跑步者45分钟30秒跑了14公里。编写一个程序显示以每小时多少英里为单位的平均速度值。(注意,1英里等于1.6公里)
- 1.11(人口估算)美国人口调查局基于以下假设进行人口估算:
- 1.12(以公里计的平均速度)假设一个跑步者1小时40分钟35秒内跑了24英里。编写一个程序小显示以每小时多少公里为单位的平均速度值。
- 1.13(代数,求解2✖️2线性方程)可以使用Cramer规则解下面的2✖️2线性方程组:
- 总结
前言
本篇文章旨在帮助在学习Java的小白解决一些在教材里遇到的问题,我们以课后习题为下手点,以题带面。
一、第一章 计算机 程序 和Java概述
第一章我们简单了解了计算机基础知识,程序和操作系统,以及Java和万维网的关系,理解了API,JDK,IDE的含义,也配置了自己的编程环境,接下来,我们就是要动手写一些简单的程序,前期我们可以通过抄写一些简单程序来大致了解到我们的Java程序的基本构成,为后期更深入的学习打下基础。在抄写这些程序的时候我们会出现一些常见的错误,这里我们就要学会分析错误是怎么产生的。
二、第一章课后习题解析
1.1.(显示三条信息)编写程序,显示Welcome to Java,Welcome to Computer Science 和Programming is fun。
这道题比较简单
代码如下:
/*
Display three messages
*/
public class Exercise1_1 {
public static void main(String[] args) {
System.out.println("Welcome to Java ");
System.out.println("Welcome to Computer Science ");
System.out.println("Programming is fun ");
}
}
1.2.(显示五条信息)编写程序,显示Welcome to Java 五次。
这道题可以像机器一样写成下面这样
public class Exercise1_1 {
public static void main(String[] args) {
System.out.println("Welcome to Java ");
System.out.println("Welcome to Java ");
System.out.println("Welcome to Java ");
System.out.println("Welcome to Java ");
System.out.println("Welcome to Java ");
}
}
也可以写的非常简单
public class Exercise1_2 {
public static void main(String[] args) {
for(int i=0;i<5;i++) {
System.out.println("Welcom to Java");
}
}
}
两个代码输出的结果都一样,但是你更喜欢简单,还是重复简单的工作呢??
答案肯定是简单,所以接下来我先给你们讲点后边学的东西,他叫循环
/*
This class is for output loop massage
This exercise can use loop,here is some loop methods:
1.for loop(know the number of loop):
Usage: judge first, then execute; If the number of loops is certain, the for loop is usually used
1.1.int sum = 0;//The intial sum is zero
for(int i = 0; i<100; i++) {
sum += i +1;//sum = sum + i + 1
}
2.while loop
Usage: judge first, then execute; When the conditional expression is true, the body of the loop is executed, and then the judgment is made. If the condition does not stand, it may not be executed. Generally used for cycles with uncertain number of cycles
2.1.int i = 0;
int sum = 0;
while(i<100) {
if(i%3==0) {
sum += i;
}
i++;
}
3.do..weile loop
Usage: execute first, judge later; It is generally used for loops with uncertain number of cycles. Unlike the while loop, it is executed first and judged later, and will be executed at least once.
3.1.int i = 0;
int sum = 0;
do {
if(i%3==0 && i%5==0) {
sum += i;
}
i++;
} while (i<=100);
4.switch loop
How to use: Usually used to branch, or to make a choice
4.1.System.out.println("Please choose the day of the week ");
System.out.println("Monday Tuesday Wednesday Thursday Friday");
Scanner inputScanner = Scanner.nextInt(System.in);
int count = inputScanner.nextScanner();
switch (count) {
case 1:
System.out.println("Today is Monday");
break;
case 2:
System.out.println("Today is Tuesday");
break;
case 3:
System.out.println("Today is Wednesday");
break;
case 4:
System.out.println("Today is Thursday");
break
case 5:
System.out.println("Today is Friday");
break;
default:
System.out.println("In fact today is Sunday");
break;
}
*/
public class Exercise1_2 {
public static void main(String[] args) {
for(int i=0;i<5;i++) {
System.out.println("Welcom to Java");
}
}
}
以上代码里有我对该代码的一些知识补充,以后的文章里也都是这样的
1.3 (显示图案)编写一个程序,显示一个Java图案
这道题就老老实实的写,但是这里你得悟到我们在
System.out.println( );
括号里你输入了几个空格最后我们就会输出几个空格
这道题代码如下:
/*
Display pattern
*/
public class Exercise1_3 {
public static void main(String[] args) {
System.out.println(" J A V V A ");
System.out.println(" J A A V V A A ");
System.out.println(" J J AAAAA V V AAAAA ");
System.out.println(" J J A A V A A ");
}
}
1.4 (打印表格)编写程序,显示以下表格:
这道题比较简单,我们直接像上道题那样,按照他这个格式打出来就行
/*
Print form
*/
public class Exercise1_4 {
public static void main(String[] args) {
System.out.println("a a^2 a^3");
System