课堂代码

1、Addition.java

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
// 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
   }
}

  

将两个整数相加并显示结果。

程序会显示一个提示框,要求用户输入第一个整数。输入值会以字符串的形式被保存。接下来会显示另一个提示框,要求输入第二个整数。输入值会以字符串的形式被保存

然后,将这两个字符串类型的数值转换为整型,并保存

number1和number2结果保存在sum变量中。

用户将会看到一个简单的提示框,显示两个整数相加的结果。

 

2、EnumTest.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public class EnumTest {
 
    public static void main(String[] args) {
        Size s=Size.SMALL;
        Size t=Size.LARGE;
        //s和t引用同一个对象?
        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};

  因为 s 与 t 引用对象不同,会输出 false

  枚举类型为引用类型,所以不是原始数据类型,会输出false

  s引用的为SMALL u也为SMALL  所以这项判断是正确的,会输出true

  最后是输出所有枚举类型的值SMALL MEDIUM LARGE

 

3、InputTest.java

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
<em id="__mceDel">import java.util.*;
 
public class InputTest
   public static void main(String[] args)
   
      Scanner in = new Scanner(System.in);
 
      System.out.print("What is your name? ");
      String name = in.nextLine();
 
      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);
   
      System.out.println("Hello, " + name + ". Next year, you'll be " + (age + 1));
   
       
   }
}
</em>

  一个输入输出的程序 输入姓名 年龄 输出明年的年龄

  被注释的代码作用 它将字符串"value"转换为整数并赋值给变量’i’。然后,它将’i’的值更改为200,并将200转换为字符串并赋值给变量’s’。和这个程序好像没有关系

  不过学会了字符串与整型的相互转化

 

4、RandomStr.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class RandomStr
{
    public static void main(String[] args)
    {
        //定义一个空字符串
        String result = "";
        //进行6次循环
        for(int i = 0 ; i < 6 ; i ++)
        {
            //生成一个97~122的int型的整数
            int intVal = (int)(Math.random() * 26 + 97);
            //将intValue强制转换为char后连接到result后面
            result = result + (char)intVal;
        }
        //输出随机字符串
        System.out.println(result);
    }
}

 生成随机数

 随机数转换为字符

 字符存入字符串

    随机生成验证码程序中可以用,考虑改为4位

 

5、SwitchTest.java

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
<em id="__mceDel">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\n" +
                 "Enter 2 to draw rectangles\n" +
                 "Enter 3 to draw ovals\n" );
 
      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
</em>

  放到编译器中,发现没有主函数

  自己添加了一个主函数

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
51
import java.awt.Graphics;
import javax.swing.*;
 
public class Main extends JApplet {
    int choice;
 
    public void init()
    {
        String input;
 
        input = JOptionPane.showInputDialog(
                "Enter 1 to draw lines\n" +
                        "Enter 2 to draw rectangles\n" +
                        "Enter 3 to draw ovals\n" );
 
        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()
 
    public static void main(String[] args) {
        Main applet = new Main();
        applet.init();
        applet.start();
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(applet);
        frame.setSize(300, 300);
        frame.setVisible(true);
    }
}

  生成直线 矩形 和圆

 

 6、Test.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class Test {
public static void main(String[] args) {
    int intValue=100;
    long longValue=intValue;
    double doubleValue=1234567890;
    float floatValue=(float)doubleValue;
    System.out.println(floatValue);//1.23456794E9
     
    int X=100;
    int Y=200;
    System.out.println("X+Y="+X+Y);
    System.out.println(X+Y+"=X+Y");
    doNotRunme();
     
}
 
}

  

先删除了一部分代码

第一部分 int类型转换为long long类型 double类型转换为float类型 前者为低精度转换为高精度没有问题 而后者是高精度转换为低精度 造成精度丢失 产生错误

第二部分 主要是两个输出 第一个输出结果为100200 表示+X+Y不能将其想加

删除的代码是

复制代码
doNotRunme();

        String string="";
        double d1=1000.123;
        double d2=1000.123;
        if(Math.abs(d2-d1)<1e-10)
        {

        }
public static void doNotRunme()
    {
        doNotRunme();
    }
复制代码

 

7、TestBigDecimal.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import java.math.BigDecimal;
 
public class TestBigDecimal
{
    public static void main(String[] args)
    {
        BigDecimal f1 = new BigDecimal("0.05");
        BigDecimal f2 = BigDecimal.valueOf(0.01);
        BigDecimal f3 = new BigDecimal(0.05);
        System.out.println("下面使用String作为BigDecimal构造器参数的计算结果:");
        System.out.println("0.05 + 0.01 = " + f1.add(f2));
        System.out.println("0.05 - 0.01 = " + f1.subtract(f2));
        System.out.println("0.05 * 0.01 = " + f1.multiply(f2));
        System.out.println("0.05 / 0.01 = " + f1.divide(f2));
        System.out.println("下面使用double作为BigDecimal构造器参数的计算结果:");
        System.out.println("0.05 + 0.01 = " + f3.add(f2));
        System.out.println("0.05 - 0.01 = " + f3.subtract(f2));
        System.out.println("0.05 * 0.01 = " + f3.multiply(f2));
        System.out.println("0.05 / 0.01 = " + f3.divide(f2));
    }
}

 

下面使用String作为BigDecimal构造器参数的计算结果:
0.05 + 0.01 = 0.06
0.05 - 0.01 = 0.04
0.05 * 0.01 = 0.0005
0.05 / 0.01 = 5.00

下面使用double作为BigDecimal构造器参数的计算结果:
0.05 + 0.01 = 0.060000000000000005
0.05 - 0.01 = 0.04000000000000001
0.05 * 0.01 = 0.0005000000000000001
0.05 / 0.01 = 5.000000000000001


8、TestDouble.javaTestDouble.java
 
1
2
3
4
5
6
7
8
9
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));
    }
}

  

0.05 + 0.01 = 0.06
1.0 - 0.42 = 0.58
4.015 * 100 = 401.5
123.3 / 100 = 1.233

 

 

 
posted @   财神给你送元宝  阅读(22)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
点击右上角即可分享
微信分享提示