9.15代码试验
Addition.java
1 package shiyan; 2 3 4 // An addition program 5 6 import javax.swing.JOptionPane; // import class JOptionPane 7 8 public class Addition { 9 public static void main( String args[] ) 10 { 11 String firstNumber, // first string entered by user 12 secondNumber; // second string entered by user 13 int number1, // first number to add 14 number2, // second number to add 15 sum; // sum of number1 and number2 16 17 // read in first number from user as a string 18 firstNumber = 19 JOptionPane.showInputDialog( "Enter first integer" ); 20 21 // read in second number from user as a string 22 secondNumber = 23 JOptionPane.showInputDialog( "Enter second integer" ); 24 25 // convert numbers from type String to type int 26 number1 = Integer.parseInt( firstNumber ); 27 number2 = Integer.parseInt( secondNumber ); 28 29 // add the numbers 30 sum = number1 + number2; 31 32 // display the results 33 JOptionPane.showMessageDialog( 34 null, "The sum is " + sum, "Results", 35 JOptionPane.PLAIN_MESSAGE ); 36 37 System.exit( 0 ); // terminate the program 38 } 39 }
EnumTest.java
1 package shiyan; 2 3 public class EnumTest { 4 5 public static void main(String[] args) { 6 Size s=Size.SMALL; 7 Size t=Size.LARGE; 8 //s和t引用同一个对象? 9 System.out.println(s==t); // 10 //是原始数据类型吗? 11 System.out.println(s.getClass().isPrimitive()); 12 //从字符串中转换 13 Size u=Size.valueOf("SMALL"); 14 System.out.println(s==u); //true 15 //列出它的所有值 16 for(Size value:Size.values()){ 17 System.out.println(value); 18 } 19 } 20 21 } 22 enum Size{SMALL,MEDIUM,LARGE};
InputTest.java
1 package shiyan; 2 3 /** 4 @version 1.10 2004-02-10 5 @author Cay Horstmann 6 */ 7 8 import java.util.*; 9 10 public class InputTest 11 { 12 public static void main(String[] args) 13 { 14 Scanner in = new Scanner(System.in); 15 16 // get first input 17 System.out.print("What is your name? "); 18 String name = in.nextLine(); 19 20 // get second input 21 System.out.print("How old are you? "); 22 int age = in.nextInt(); 23 24 25 /* int i; 26 String value="100"; 27 i=Integer.parseInt(value); 28 i=200; 29 String s=String.valueOf(i);*/ 30 31 // display output on console 32 System.out.println("Hello, " + name + ". Next year, you'll be " + (age + 1)); 33 34 35 } 36 }
RandomStr.java
1 package shiyan; 2 3 4 5 6 public class RandomStr 7 { 8 public static void main(String[] args) 9 { 10 //定义一个空字符串 11 String result = ""; 12 //进行6次循环 13 for(int i = 0 ; i < 6 ; i ++) 14 { 15 //生成一个97~122的int型的整数 16 int intVal = (int)(Math.random() * 26 + 97); 17 //将intValue强制转换为char后连接到result后面 18 result = result + (char)intVal; 19 } 20 //输出随机字符串 21 System.out.println(result); 22 } 23 }
SwitchTest.java
1 package shiyan; 2 3 // Drawing shapes 4 import java.awt.Graphics; 5 import javax.swing.*; 6 7 public class SwitchTest extends JApplet { 8 int choice; 9 10 public void init() 11 { 12 String input; 13 14 input = JOptionPane.showInputDialog( 15 "Enter 1 to draw lines\n" + 16 "Enter 2 to draw rectangles\n" + 17 "Enter 3 to draw ovals\n" ); 18 19 choice = Integer.parseInt( input ); 20 } 21 22 public void paint( Graphics g ) 23 { 24 for ( int i = 0; i < 10; i++ ) { 25 switch( choice ) { 26 case 1: 27 g.drawLine( 10, 10, 250, 10 + i * 10 ); 28 break; 29 case 2: 30 g.drawRect( 10 + i * 10, 10 + i * 10, 31 50 + i * 10, 50 + i * 10 ); 32 break; 33 case 3: 34 g.drawOval( 10 + i * 10, 10 + i * 10, 35 50 + i * 10, 50 + i * 10 ); 36 break; 37 default: 38 JOptionPane.showMessageDialog( 39 null, "Invalid value entered" ); 40 } // end switch 41 } // end for 42 } // end paint() 43 } // end class SwitchTest
Test.java
1 package shiyan; 2 3 public class Test { 4 public static void main(String[] args) { 5 int intValue=100; 6 long longValue=intValue; 7 double doubleValue=1234567890; 8 float floatValue=(float)doubleValue; 9 System.out.println(floatValue);//1.23456794E9 10 11 int X=100; 12 int Y=200; 13 System.out.println("X+Y="+X+Y); 14 System.out.println(X+Y+"=X+Y"); 15 doNotRunme(); 16 17 String string=""; 18 double d1=1000.123; 19 double d2=1000.123; 20 if(Math.abs(d2-d1)<1e-10){ 21 22 } 23 //System.out.println(string); 24 25 } 26 27 public static void doNotRunme() 28 { 29 doNotRunme(); 30 } 31 }
TestBigDecimal.java
1 package shiyan; 2 3 4 import java.math.BigDecimal; 5 6 public class TestBigDecimal 7 { 8 public static void main(String[] args) 9 { 10 BigDecimal f1 = new BigDecimal("0.05"); 11 BigDecimal f2 = BigDecimal.valueOf(0.01); 12 BigDecimal f3 = new BigDecimal(0.05); 13 System.out.println("下面使用String作为BigDecimal构造器参数的计算结果:"); 14 System.out.println("0.05 + 0.01 = " + f1.add(f2)); 15 System.out.println("0.05 - 0.01 = " + f1.subtract(f2)); 16 System.out.println("0.05 * 0.01 = " + f1.multiply(f2)); 17 System.out.println("0.05 / 0.01 = " + f1.divide(f2)); 18 System.out.println("下面使用double作为BigDecimal构造器参数的计算结果:"); 19 System.out.println("0.05 + 0.01 = " + f3.add(f2)); 20 System.out.println("0.05 - 0.01 = " + f3.subtract(f2)); 21 System.out.println("0.05 * 0.01 = " + f3.multiply(f2)); 22 System.out.println("0.05 / 0.01 = " + f3.divide(f2)); 23 } 24 }
TestDouble.java
1 package shiyan; 2 3 4 5 public class TestDouble { 6 7 public static void main(String args[]) { 8 System.out.println("0.05 + 0.01 = " + (0.05 + 0.01)); 9 System.out.println("1.0 - 0.42 = " + (1.0 - 0.42)); 10 System.out.println("4.015 * 100 = " + (4.015 * 100)); 11 System.out.println("123.3 / 100 = " + (123.3 / 100)); 12 } 13 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 【杂谈】分布式事务——高大上的无用知识?