1.1(显示三条消息)编写程序,显示 Welcome to Java We1 come to Computer Science 和Programming is fun。
package Java;
public class Day1_2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Welcome to Java");
System.out.println("Welcome to Computer");
System.out.println("Programing is fun");
}
}
运行结果
Welcome to Java
Welcome to Computer
Programing is fun
1.2(显示五条消息)编写程序,显示 Welcome to Java 五次
package Java;
public class Day1_2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
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");
}
}
或者利用循环
package Java;
public class Day1_2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
for(int i=0;i<5;i++)
System.out.println("Welcome to Java");
}
}
输出结果
Welcome to Java
Welcome to Java
Welcome to Java
Welcome to Java
Welcome to Java
1.3(显示图案)编写程序,显示下面的图案:
package Java;
public class Day1_2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
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");
}
}
输出
J A V V A
J A A V V A A
J J AAAAA V V AAAAA
J J A A V A A
1.4(打印表格)编写程序,显示以下表格
package chapter01;
public class Test04 {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("a\ta^2\ta^3");
for(int i = 1;i < 5;i++){
System.out.print((int)Math.pow(i,1));//print不会换行
System.out.print("\t");//一个制表符即4个空格
System.out.print((int)Math.pow(i,2));
System.out.print("\t");//一个制表符即4个空格
System.out.println((int)Math.pow(i,3));//println自动换行
}
}
}
输出
a a^2 a^3
1 1 1
2 4 8
3 9 27
4 16 64
1.5(计算表达式)编写程序,显示以下式子的结果:
package chapter01;
public class Test05 {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.print("(9.5*4.5-2.5*3)/(45.5-3.5) = ");
System.out.println((9.5*4.5-2.5*3)/(45.5-3.5));
System.out.println((double)((9.5*4.5)-(2.5*3))/((45.5-3.5)));
System.out.println((int)(9.5*4.5-2.5*3)/(45.5-3.5));
System.out.println((int)((9.5*4.5)-(2.5*3))/((45.5-3.5)));
}
}
/**输出
(9.5*4.5-2.5*3)/(45.5-3.5) = 0.8392857142857143
0.8392857142857143
0.8333333333333334
0.8333333333333334 */
1.6(数列求和)编写程序,显示)+2+3+4+5+6+7+8+9的结果。
package chapter01;
public class Test06 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int sum = 0;
for(int i = 1;i < 10;i++)
sum+=i;
System.out.println(sum);
}
}
//45
1.7(求1T的近似值)可以使用以下公式计算
package chapter01;
public class Test07 {
public static void main(String[] args) {
double sum = 1.0;
double s = 1.0;
for(double i= 1.0;i < 6.0;i++) {
sum += Math.pow(-1,i) * (1 / (i * 2 +1));
}
for(double i= 1.0;i < 7.0;i++) {
s += Math.pow(-1,i) * (1 / (i * 2 +1));
}
System.out.println(sum * 4);
System.out.println(s * 4);
}
}
/*2.9760461760461765
3.2837384837384844*/
或者
package chapter01;
public class Test07 {
public static void main(String[] args) {
double sum = 0;
int count = 0;
for(int i = 1;i < 15;i += 2) {
if(count % 2 ==0) {
sum += 1.0 / i;
count++;
}
else if(count % 2 !=0){
sum -= 1.0 / i;
count++;
}
if(i == 11) {
System.out.println(sum * 4);
}
}
System.out.println(sum * 4);
}
}
1.8(圆的面积和周长)编写程序,使用以下公式计算并显示半径为 5.
package chapter01;
import java.util.Scanner;
public class Test08 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.println("请输入圆的半径:");
double radius = input.nextDouble();
System.out.println("半径为" + radius + "的圆的面积是" +
radius * radius * 3.14);
System.out.println("半径为" + radius + "的圆的周长是" +
2 * radius * Math.PI);//Math.PI是3.14
}
}
/*请输入圆的半径:
5
半径为5.0的圆的面积是78.5
半径为5.0的圆的周长是31.41592653589793
*/
1.9(矩形的面积和周长)编写程序,使用以下公式计算并显示宽为 4.5、高为 7.9 的矩形的面积和周长
package chapter01;
import java.util.Scanner;
public class Test09 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.println("请输入矩形长");
double longth = input.nextDouble();
System.out.println("请输入矩形宽");
double width = input.nextDouble();
System.out.println("长" + longth + "宽" + width + "的矩形的面积是"
+ longth * width);
System.out.println("长" + longth + "宽" + width + "的矩形的周长是"
+ (longth + width) * 2);
}
}
/*请输入矩形长
2
请输入矩形宽
5
长2.0宽5.0的矩形的面积是10.0
长2.0宽5.0的矩形的周长是14.0
*/
1.10(以英里计 的平均速度)假设一个跑步者 45 分 30秒跑了 14 千米。编写少英里为单位的平均速度值。(注意, 1 英里约等于 1.6 千米。)
package chapter01;
import java.util.Scanner;
public class Test10 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.print("请输入用时分钟部分:");
int minutes = input.nextInt();
System.out.print("请输入用时秒钟部分:");
int seconds = input.nextInt();
System.out.print("请输入跑步距离:");
double distance = input.nextDouble();
System.out.println("他的平均速度:" +
(distance / 1.6)/ ((minutes * 60) + seconds)/3600);
}
}
/*
请输入用时分钟部分:45
请输入用时秒钟部分:30
请输入跑步距离:14
他的平均速度:8.903133903133903E-7
*/
1.11(人口估算)美国人口调查局基千以下假设进行人口估算:
package chapter01;
public class Test11 {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("五年后的人口数:" + (365 * 24 * 3600 / 7
- 365 * 24 * 3600 / 13 + 365 * 24 * 3600 +
312032486));
}
}
package chapter01;
public class Test11 {
public static void main(String[] args) {
// TODO Auto-generated method stub
long person = 312032486;
long born = (long)(365 * 24 * 3600 / 7.0);
long death = (long)(365 * 24 * 3600 /13.0);
long immigrant = (long)(365 * 24 * 3600 /45.0);
for(int i = 0;i < 5;i++) {
person = person + born - death + immigrant;
}
System.out.println("五年后的人口:" + person);
}
}
1.12 (以千术计的平均速度)假设一个跑步者1小时40分 35秒跑了24英里。
package chapter01;
import java.util.Scanner;
public class Test12 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.print("请输入用时分钟部分:");
int minutes = input.nextInt();
System.out.print("请输入用时秒钟部分:");
int seconds = input.nextInt();
System.out.print("请输入跑步距离:");
double distance = input.nextDouble();
System.out.println("他的平均速度:" +
(distance * 1.6)/ ((minutes * 60) + seconds)/3600);
}
}
1.13 (代数: 求解2x2线性方程组)可以使用Cramer规则解下面的2x2线性方程组
package chapter01;
import java.util.Scanner;
public class Test13 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.print("请输入a的值:");
double a = input.nextDouble() ;
System.out.print("请输入b的值:");
double b = input.nextDouble();
System.out.print("请输入c的值:");
double c = input.nextDouble() ;
System.out.print("请输入d的值:");
double d = input.nextDouble() ;
System.out.print("请输入e的值:");
double e = input.nextDouble();
System.out.print("请输入f的值:");
double f = input.nextDouble();
if((a * d - b * c) == 0) {
System.out.println("该方程无解");
}
else {
System.out.println((e * d - b * f)/(a * d - b * c));
System.out.println( (a * f - e * c)/(a * d - b * c));
//不能采用System.out.println(double,double)的形式
}
}
}
/*
请输入用时分钟部分:45
请输入用时秒钟部分:30
请输入跑步距离:14
他的平均速度:8.903133903133903E-7
*/