• 课程中的所有动手动脑问题以及课后实验性问题


    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. *
    *************************************************************************/

    结果:用消息框提示进行选择

  • 相关阅读:
    mysql 视图
    CSS 上下居中和最低高度语法
    escape()、encodeURI()、encodeURIComponent()区别详解
    YII事件EVENT示例
    linux history命令优化
    mysql 之full join
    redis学习之数据类型
    <canvas>设置宽高遇到的问题
    关于块级元素撑满整个浏览器窗口
    jquery中bind()绑定多个事件
  • 原文地址:https://www.cnblogs.com/2016excellent-3584/p/7636084.html
Copyright © 2020-2023  润新知