double > float > long > int > short > byte
A. 小类型转大类型是自动转换 (向上转型);
B. 大类型转小类型会发生精度丢失, 也有可能发生数据溢出, 所以编译器要求我们必须强制转换, 否则会有编译错误.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| int i = 1 , j ; //正确:对于 j 这里只是定义,没有初始化. float f1 = 0.1 ; //错误:在java中,有小数的数值默认为double类型,所以结果为double类型,看B解释 float f2 = 123; //正确: 结果为int类型,自动转换成float类型,A解释 double d1 = 2e20,d2 = 123; //正确:A解释 byte b1 = 1,b2 = 2 ,b3 = 129; //错误: b1,b2没有错,A解释;b3超过范围. j = j + 10; //错误:只有定义 没有初始化 i= i / 10; //正确: i = i * 0.1; //错误:i先自动转型为double,所以结果为double类型,看B解释. char c1 = 'a',c2 = 125; //正确: byte b = b1 - b2; //错误:结果为in类型 char c = c1 + c2 -1; //错误:结果为int类型,看B解释 float f3 = f1 + f2 ; //正确:在f1和f2为float为前提条件下 float f4 = f1 + f2*0.1; //错误:先是f2转换成double,然后f1转换为double,所以结果为double类型,看B解释 double d = d1*i + j; //错误:j未初始化. float f = (float)(d1*5 + d2); //正确:结果为double,但是强制转换成了float,看B解释.
|
if 判断语句
- if - else
1 2 3 4 5
| if(判断条件) { 执行语句; }else { 执行语句; }
|
- if - else if
1 2 3 4 5 6 7 8 9
| if(判断条件){ 执行语句; }else if(判断条件) { 执行语句; } ... else if(判断条件) { 执行语句; }
|
1 2 3 4 5 6 7 8 9 10 11
| if(判断条件){ 执行语句; }else if(判断条件) { 执行语句; } ... else if(判断条件) { 执行语句; }else { 执行语句; }
|
练习:
要求:
- 输入成绩, 给出等级;
- A:100-90;
- B:89-80
- C:79-70
- D:69-60
- 不及格: 小于 59
- 输入 quit 退出程序;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| import javax.swing.JOptionPane; import java.util.regex.Matcher; class Demo { public static void main(String[] args) { while(true){ String s = JOptionPane.showInputDialog(null,"请输入成绩"); if(s.matches("^[0-9]{1,3}$")){ int num = Integer.parseInt(s); if(num >= 90 ) JOptionPane.showMessageDialog(null,"等级:A"); else if(num >= 80) JOptionPane.showMessageDialog(null,"等级:B"); else if(num >= 70) JOptionPane.showMessageDialog(null,"等级:C"); else if(num >= 60) JOptionPane.showMessageDialog(null,"等级:D"); else if(num <= 59 ) JOptionPane.showMessageDialog(null,"不及格"); } else if(s.equals("quit")){ return ; } else JOptionPane.showMessageDialog(null,"输入错误,请重新输入"); } } }
|
作业一
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
|
import javax.swing.JOptionPane; class Operation { public static void Result (double number1, double number2 ,String operate) { switch (operate){ case "1":{ JOptionPane.showMessageDialog(null,number1+" + "+number2+" = "+(number1+number2)); break; } case "2":{ JOptionPane.showMessageDialog(null,number1+" - "+number2+" = "+(number1-number2)); break; } case "3":{ JOptionPane.showMessageDialog(null,number1+" * "+number2+" = "+(number1*number2)); break; } case "4":{ if(number2 != 0){ JOptionPane.showMessageDialog(null,number1+" / "+number2+" = "+(number1/number2)); }else JOptionPane.showMessageDialog(null,"被除数不能为0"); break; }
} } } class OperationTest { public static void main(String[] args) { while(true){ try{ String s1 = JOptionPane.showInputDialog(null,"请输入第一个数"); String s2 = JOptionPane.showInputDialog(null,"请输入第二个数"); String s = JOptionPane.showInputDialog(null,"请选择算法:1.加法,2.减法,3.乘法,4.除法;\"quit\"退出"); if (s.equals("quit")) return ; double firstNumber = Double.parseDouble(s1); double secondNumber = Double.parseDouble(s2); Operation.Result(firstNumber,secondNumber,s); }catch(Exception e){ JOptionPane.showMessageDialog(null,"输入有误,请重新输入"); } } } }
|
作业二
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| import javax.swing.JOptionPane; class PalindromicNumber { public static void main(String[] args) { String s ; int num ; JOptionPane.showMessageDialog(null,"输入 quit 退出,回车继续"); while(true){ s = JOptionPane.showInputDialog(null,"请输入一个5位整数"); try { num = Integer.parseInt(s); if(num < 0 ){ JOptionPane.showMessageDialog(null,"请输入正整数"); continue ; } char[] charArray = s.toCharArray(); System.out.println(charArray.length); if(5 == charArray.length){ boolean on_off = false ; for(int j = 0 ; j < charArray.length/2 ;j++) { if(charArray[j] == charArray[charArray.length -1 - j]) on_off = true ; } if (on_off) JOptionPane.showMessageDialog(null,"是回文数"); else JOptionPane.showMessageDialog(null,"不是回文数"); }else JOptionPane.showMessageDialog(null,"请输入一个5位的整数"); }catch (Exception e){ if(s.equals("quit")) return ; JOptionPane.showMessageDialog(null,"请正确输入一个5位整数"); } } } }
|
回文数练习
不用数组的方法打印 1-100000 之间所有的回文数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| class PalindromicNumber2 { public static void main(String[] args) { int k = 0,num; for(int i = 0 ; i <= 1000000 ; i++){ num = i ; while (num != 0){ k = k*10 + num%10; num = num/10; } if(k == i){ System.out.println(i); } k = 0; } } }
|