• java课件运行实践


    1. 两数相加   

    源文件:Addition.java

    源代码:

    // An addition program

    import javax.swing.JOptionPane;  // import class JOptionPane

    public class Addition {

       public static void main( String args[] )

       {

          String firstNumber,   // first string entered by user

                 secondNumber;  // second string entered by user

          int number1,          // first number to add

              number2,          // second number to add

              sum;              // sum of number1 and number2

          // read in first number from user as a string

          firstNumber =

             JOptionPane.showInputDialog( "Enter first integer" );

          // read in second number from user as a string

          secondNumber =

             JOptionPane.showInputDialog( "Enter second integer" );

          // convert numbers from type String to type int

          number1 = Integer.parseInt( firstNumber );

          number2 = Integer.parseInt( secondNumber );

          // add the numbers

          sum = number1 + number2;

          // display the results

          JOptionPane.showMessageDialog(

             null, "The sum is " + sum, "Results",

             JOptionPane.PLAIN_MESSAGE );

          System.exit( 0 );   // terminate the program

       }

    }

    运行截图:

    1. 枚举

    源文件:EnumText.java

    源代码:

    public class EnumTest {

    public static void main(String[] args) {

    Size s=Size.SMALL;

    Size t=Size.LARGE;

    //st引用同一个对象?

    System.out.println(s==t);  //

    //是原始数据类型吗?

    System.out.println(s.getClass().isPrimitive());

    //从字符串中转换

    Size u=Size.valueOf("SMALL");

    System.out.println(s==u);  //true

    //列出它的所有值

    for(Size value:Size.values()){

    System.out.println(value);

    }

    }

    }

     enum Size{SMALL,MEDIUM,LARGE};

    运行截图

    1. 读取输入

    源文件InputTest.java

    源代码

    import java.util.*;

    public class InputTest

    {  

       public static void main(String[] args)

       {  

          Scanner in = new Scanner(System.in);

          // get first input

          System.out.print("What is your name? ");

          String name = in.nextLine();

          // get second input

          System.out.print("How old are you? ");

          int age = in.nextInt();

          

          

        /* int i;

         String value="100";

         i=Integer.parseInt(value);

         i=200;

         String s=String.valueOf(i);*/

         

          // display output on console

          System.out.println("Hello, " + name + ". Next year, you'll be " + (age + 1));

      

          

       }

    }

    运行截图

    4double类型.

    源文件 TestDouble.java

    源代码

    public class TestDouble {

        public static void main(String args[]) {

            System.out.println("0.05 + 0.01 = " + (0.05 + 0.01));

            System.out.println("1.0 - 0.42 = " + (1.0 - 0.42));

            System.out.println("4.015 * 100 = " + (4.015 * 100));

            System.out.println("123.3 / 100 = " + (123.3 / 100));

        }

    }

    运行截图

  • 相关阅读:
    位运算02 零基础入门学习C语言65(完)
    PE格式详细讲解4 系统篇04|解密系列
    PE格式详细讲解4 系统篇04|解密系列
    int指令02 零基础入门学习汇编语言65
    Tabhost漂亮的自定义实现(背景随着选项卡滑动改变)
    Android API Guide 之 User Interface笔记
    java程序员菜鸟进阶(九)windows下搭建SVN服务器及配置myeclipse SVN客户端
    android 左右页面滑动(滑屏)增加layout文件 而不是drawable(还有activity)
    android 最简单的九宫格实现
    ViewPager
  • 原文地址:https://www.cnblogs.com/xuange1/p/9747632.html
Copyright © 2020-2023  润新知