题目
1.2.1
public class ABT
{
public static void main(String[] args)
{
int a = Integer.parseInt(args[0]);
int b = Integer.parseInt(args[1]);
int t = a; b = t; a = b ;
System.out.println("a="+a+"\nb="+b+"\nt="+t);
}
//结果是
// a=a
// b=a
// t=a
}
1.2.2
通过sin和cos 来理解计算机计算的局限性 无限不循环小数(浮点数不友好)
public class SinCos
{
public static void main (String[] args)
{
double t=Double.parseDouble(args[0]);
double a=Math.sin(t);
double b=Math.cos(t);
System.out.println((a*a+b*b));
}
}
1.2.3
证明布尔表达式成立用真值表法就可以了
a | b | 表达式的值 |
---|---|---|
true | true | true |
true | false | true |
false | true | true |
false | false | true |
public class Boolean1
{
public static void main(String[] args)
{
Boolean a = Boolean.parseBoolean(args[0]);
Boolean b = Boolean.parseBoolean(args[1]);
if ((!(a&&b)&& (a||b))||((a&&b)||!(a||b)))
{
System.out.println("true");
}
}
}
1.2.4
设置a<b为t a>b=!t
即可运算得出值一定为0
1.2.7
/*
练习1.2.7
打印字符串和常量的拼接量
字符串的"+"操作
当"+"操作出现字符串时,这个"+"是字符串连接符,而不是算术运算
在"+"操作中,如果出现了字符串,就是连接运算符,否则就是算术运算
当连续进行"+"操作时,从左到右逐个执行
*/
public class Dl_1_2_7{
public static void main(String[] args){
System.out.println(2+"bc");//2bc
System.out.println("------");
System.out.println(2+3+"bc");//5bc
System.out.println("------");
System.out.println((2+3)+"bc");//5bc
System.out.println("------");
System.out.println("bc"+(2+3));//bc5
System.out.println("------");
System.out.println((2+3)+"bc");//5bc
System.out.println("------");
System.out.println("bc"+(2+3));
System.out.println("------");
System.out.println("bc"+2+3);
}
}
1.2.8
/*
练习1.2.8
如何利用1.2.3求一个数的平方根
public class Quadratic
//1.2.3
{
public static void main(String[] args)
{
double b = Double.parseDouble(args[0]);
double c = Double.parseDouble(args[1]);
double discriminant = b*b-4.0*c;
//求出判别式
double d= Math.sqrt(discriminant);
System.out.println((-b+d)/2.0);
//将判别式代入其中,简化编程运算
System.out.println((-b-d)/2.0);
}
}
需求:求平方根
分析:
1.程序1.2.3是求一元二次方程的解
2.数字的二次方式子就是一元二次方程b=0的情况。
3.所以将b设置为0,取数的负数为c就可以求出。
*/
public class Dl_1_2_8{
public static void main(String[] args){
System.out.println("求平方根");
System.out.println("请输入一个数字");
double c = Double.parseDouble(args[0]);
double discriminant = 4.0*c;
//求出判别式
double d= Math.sqrt(discriminant);
System.out.println((+d)/2.0);
//将判别式代入其中,简化编程运算
System.out.println((-d)/2.0);
}
}
1.2.9
/*
练习1.2.9 打印结果
解释:
提升规则:
1.byte类型,short类型和char类型将被提升到int类型
2.整个表达式的类型自动提升到表达式中最高等级操作数同样的类型
等级顺序:byte、short、char———>int——>long——>float——>double
*/
public class Dl_1_2_9{
public static void main(String[] args){
System.out.println('b');//b
System.out.println("------");
System.out.println('b'+'c');//197
System.out.println("------");
System.out.println((char)('a'+4));//e
}
}
1.2.10
/*
练习1.2.10
假设变量a的声明如下:int a =2147483647(这个值就是Integer.MAX_VALUE).一下各项打印结果是什么?
*/
public class Dl_1_2_10 {
public static void main(String[] args){
int a = 2147483647;
System.out.println(a);//2147483647
System.out.println("------");
System.out.println(a+1);//-2147483648
/*
2147483647=0111 1111 1111 1111 1111 1111 1111 1111
2147483647+1=0111 1111 1111 1111 1111 1111 1111 1111+1
=1000 0000 0000 0000 0000 0000 0000 0000
=-2147483648
*/
System.out.println("------");
System.out.println(2-a);//-2147483645
/*
2147483647=0111 1111 1111 1111 1111 1111 1111 1111
2-2147483647=10-0111 1111 1111 1111 1111 1111 1111 1111
=10+1000 0000 0000 0000 0000 0000 0000 0001
=1000 0000 0000 0000 0000 0000 0000 0011
= -0111 1111 1111 1111 1111 1111 1111 1101
= -2147483645
*/
System.out.println("------");
System.out.println(-2-a);//2147483647
/*
2147483647=0111 1111 1111 1111 1111 1111 1111 1111
-2-2147483647=1111 1111 1111 1111 1111 1111 1111 1110-0111 1111 1111 1111 1111 1111 1111 1111
=1111 1111 1111 1111 1111 1111 1111 1110+1000 0000 0000 0000 0000 0000 0000 0001
=0111 1111 1111 1111 1111 1111 1111 1111
=2147483647
*/
System.out.println("------");
System.out.println(2*a);//-2
/*
2147483647=0111 1111 1111 1111 1111 1111 1111 1111
2147483647*2=0111 1111 1111 1111 1111 1111 1111 1111*2
=1111 1111 1111 1111 1111 1111 1111 1110
=-2
*/
System.out.println("------");
System.out.println(4*a);//-4
}
}
1.2.11
/*
练习 1.2.11
假设变量a的声明如下:double 啊=3.14159.以下各项打印结果是什么?
解释:前置类型转换,int除法为商的值
*/
public class Dl_1_2_11{
public static void main (String[] args){
double a = 3.14159;
System.out.println(a);//3.14159
System.out.println("------");
System.out.println(a+1);//4.14159
System.out.println("------");
System.out.println(8/(int)a);//2
System.out.println("------");
System.out.println(8/a);//2.5464812403910124
System.out.println("------");
System.out.println((int)(8/a));//2
}
}
1.2.12
/*
练习1.2.12
描述在程序1.2.3中使用sqrt而不是Math.sqrt 会发生什么情况
Dl_1_2_12.java:13: 错误: 找不到符号
double d = sqrt(discriminant);
^
符号: 方法 sqrt(double)
位置: 类 Dl_1_2_12
1 个错误
*/
public class Dl_1_2_12
{
public static void main(String[] args)
{
double b = Double.parseDouble(args[0]);
double c = Double.parseDouble(args[1]);
double discriminant = b*b - 4.0*c;
double d = sqrt(discriminant);
System.out.println((-b+d) / 2.0);
System.out.println((-b-d) / 2.0);
}
}
1.2.13
/*
练习1.2.13
求表达式(Math.sqrt(2)*Math.sqrt(2)=2的值.)
*/
public class Dl_1_2_13
{
public static void main(String[] args)
{
System.out.println((Math.sqrt(2)*Math.sqrt(2)==2));//false
}
}
/*
Dl_1_2_13.java:10: 错误: 意外的类型
System.out.println((Math.sqrt(2)*Math.sqrt(2)=2));
^
需要: 变量
找到: 值
1 个错误
错误原因:不是赋值是比较
*/
1.2.14
/*
练习:1.2.14
编写一个程序,该程序将两个正整数作为命令行参数如果其中一个能将另一个整除,
则打印结果true.
需求:
1.该程序将两个正整数作为命令行参数
2.如果其中一个能将另一个整除,则打印结果true.
分析:
1.将两个正整数作为命令行参数
int a = Int.parseInt(args[0]);
int b = Int.parseInt(args[1]);
2.判断其中一个是否能将另一个整数整除,则打印结果
(a%b==0||b%a==0)
*/
public class Dl_1_2_14{
public static void main (String[] args){
int a = Integer.parseInt(args[0]);//Int不是int 而是Integer
int b = Integer.parseInt(args[1]);
System.out.println(a%b==0||b%a==0);
}
}
1.2.15
/*
练习:1.2.15
需求:
编写一个程序,它将三个正整数作为命令行参数,如果其中任何一个大于或等于另外两个的和。
则输出false,否则输出true。(注意:这个计算用于测试三个数字是否可以是某个三角形的边长)
分析:
1.输入三个正整数作为命令行参数。
int a = Integer.parseInt(args[0]);
int b = Integer.parseInt(args[1]);
int c = Integer.parseInt(args[2]);
2.判断其中任意一个大于或等于另外两个的和并输出false和true
System.out.println(a+b>=c&&a+c>=b&&b+c>=a);
*/
public class Dl_1_2_15{
public static void main(String[] args){
int a = Integer.parseInt(args[0]);
int b = Integer.parseInt(args[1]);
int c = Integer.parseInt(args[2]);
System.out.println(!(c>=a+b||b>=a+c||a>=b+c));
}
}
1.2.16
/*
练习:1.2.16
假设和物理系学生想在程序中计算公式F=Gm1m2/r^2,他写出如下代码:
double force =C*mass1*mass2/r*r
//因为是顺序执行的所以/r后*r 要加括号;
*/
import java.util.Scanner;
public class Dl_1_2_16{
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
//1. new一个Scanner类的对象,好习惯:顺手就把scanner.close();写了
System.out.println("请输入m1:");
double mass1 = scanner.nextDouble();
System.out.println("m1="+mass1);
System.out.println("请输入m2:");
double mass2 = scanner.nextDouble();
System.out.println("m2="+mass2);
System.out.println("请输入r:");
double r = scanner.nextDouble();
System.out.println("r="+r);
System.out.println("请输入G:");
double g = scanner.nextDouble();
System.out.println("G="+g);
double force = g * mass1 * mass2 / (r*r);
System.out.println("force ="+force);
scanner.close();
}
}
1.2.17
/*
练习:1.2.17——1
执行每个语句后给出变量a的值
输出
2
------
4
------
8
------
*/
public class Dl_1_2_17_1{
public static void main(String[] args){
int a = 1;
a = a + a;
System.out.println(a);
System.out.println("------");
a = a + a;
System.out.println(a);
System.out.println("------");
a = a + a;
System.out.println(a);
System.out.println("------");
}
}
/*
练习:1.2.17——2
执行每个语句后给出变量a的值
false
------
true
------
false
------
*/
public class Dl_1_2_17_2{
public static void main(String[] args){
boolean a = true;
a = !a;
System.out.println(a);
System.out.println("------");
a = !a;
System.out.println(a);
System.out.println("------");
a = !a;
System.out.println(a);
System.out.println("------");
}
}
/*
练习:1.2.17——3
执行每个语句后给出变量a的值
4
------
16
------
256
------
*/
public class Dl_1_2_17_3{
public static void main(String[] args){
int a = 2;
a = a*a;
System.out.println(a);
System.out.println("------");
a = a*a;
System.out.println(a);
System.out.println("------");
a = a*a;
System.out.println(a);
System.out.println("------");
}
}
1.2.18
/*
每次一句:注意力的集中,意象的孤立绝缘,便是美感的态度的最大特点
练习:1.2.18
需求:编写一个程序,该程序需要两个浮点命令行参数x和y,
并打印从;(x,y)到原点(0,0)的欧几里得距离
分析:
1.输入两个浮点命令行参数
double a = Double.parseDouble(args[0]);
double b = Double.parseDouble(args[1]);
2.打印欧几里得距离
double distance =Math.sqrt(a*a+b*b);
System.out.println(Math.sqrt(a*a+b*b));
*/
public class Dl_1_2_18{
public static void main(String[] args){
double a = Double.parseDouble(args[0]);
double b = Double.parseDouble(args[1]);
//double distance =Math.sqrt(a*a+b*b);
System.out.println(Math.sqrt(a*a+b*b));
}
}
1.2.19
/*
练习:1.2.19
编写一个程序,该程序需要两个整数型命令行参数a和b,
并打印一个a和b之间(含a和b)的随机整数
需求:
该程序需要两个整数型命令行参数a和b,
并打印一个a和b之间(含a和b)的随机整数.
分析:
1. 输入两个整型命令行参数
int a = parseInt(args[0]);
int b = parseInt(args[1]);
2. 并打印一个a和b之间(含a和b)的随机整数
(a-b < 0) ? -(a-b) : a-b;
Randon r = new Random((a-b < 0) ? -(a-b) : a-b)+((a<b) ? a : b);
*/
import java.util.Random;
public class Dl_1_2_19{
public static void main(String[] args){
int a = Integer.parseInt(args[0]);
int b = Integer.parseInt(args[1]);
Random r = new Random();
for ( int i=1;i<101; i++ ){
int number = r.nextInt(((a-b < 0) ? -(a-b) : a-b)+1)+((a<b) ? a : b);
System.out.println(number);
}
}
}
1.2.20
/*
练习:1.2.20
编写一个程序,打印1到6之间的两个随机整数的和,
(计算结果就是掷骰子时可能得到的值)
需求以上,
分析:设置两个随机数,使其相加输出结果
*/
import java.util.Random;
public class Dl_1_2_20{
public static void main(String[] args){
Random r = new Random();
for (int i=1;i<100;i++){
int a = r.nextInt(6)+1;
int b = r.nextInt(6)+1;
System.out.print(a+b+" ");
}
}
}