1.以下代码的输出结果是什么:
int X=100;
int Y=200;
System.out.println("X+Y="+X+Y);
System.out.println(X+Y+"=X+Y");
结果:
前者是因为拼接,是字符串的连接,后者是因为加法运算后输出结果
2.运行以下程序:
结果截图:
原因分析:
枚举类型的使用是借助ENUM这样一个类,这个类是JAVA枚举类型的公共基本类。枚举目的就是要让某个变量的取值只能为若干固定值中的一个。
3.一个java类中只能有一个类,不能两个类同时存在。
4,第一个程序调试:
.
结果:
用消息框弹出结果:
5.double的不精确举例
结果:
原因分析:
使用double类型的数值进行计算,其结果是不精确的。
double类型的数值占用64bit,即64个二进制数,除去最高位表示正负符号的位,在最低位上一定会与实际数据存在误差(除非实际数据恰好是2的n次方)。
解决方法,使用BigDecimal类。在构建BigDecimal对象时应使用字符串而不是double数值,否则,仍有可能引发计算精度问题。
结果:
6.源码:
结果:
7.画图形
package tiaoshi;
//Drawing shapes
import java.awt.Graphics;
import javax.swing.*;
public class SwitchTest extends JApplet {
int choice;
public void init()
{
String input;
input = JOptionPane.showInputDialog(
"Enter 1 to draw lines
" +
"Enter 2 to draw rectangles
" +
"Enter 3 to draw ovals
" );
choice = Integer.parseInt( input );
}
public void paint( Graphics g )
{
for ( int i = 0; i < 10; i++ ) {
switch( choice ) {
case 1:
g.drawLine( 10, 10, 250, 10 + i * 10 );
break;
case 2:
g.drawRect( 10 + i * 10, 10 + i * 10,
50 + i * 10, 50 + i * 10 );
break;
case 3:
g.drawOval( 10 + i * 10, 10 + i * 10,
50 + i * 10, 50 + i * 10 );
break;
default:
JOptionPane.showMessageDialog(
null, "Invalid value entered" );
} // end switch
} // end for
} // end paint()
} // end class SwitchTest
/**************************************************************************
* (C) Copyright 1999 by Deitel & Associates, Inc. and Prentice Hall. *
* All Rights Reserved. *
* *
* DISCLAIMER: The authors and publisher of this book have used their *
* best efforts in preparing the book. These efforts include the *
* development, research, and testing of the theories and programs *
* to determine their effectiveness. The authors and publisher make *
* no warranty of any kind, expressed or implied, with regard to these *
* programs or to the documentation contained in these books. The authors *
* and publisher shall not be liable in any event for incidental or *
* consequential damages in connection with, or arising out of, the *
* furnishing, performance, or use of these programs. *
*************************************************************************/
结果:用消息框提示进行选择