Experiment 1 Java language foundation

1. The purpose of the experiment

1). 熟悉java开发工具及配置
2). 熟悉java程序类型,掌握java应用程序结构及书写形式
3). 掌握java程序编写、编译、执行过程
4). 掌握java程序基本语法
5). 熟悉用System进行简单的输入输出
6). 熟悉命令行参数,熟悉基于包装类的数据转换 

2. Experimental content

1.下面的程序有错误请修改并编译运行。
1)public class KY2_2 { 
 static int i=10; 
   public static void main(String args[]) { 
        { 
          int k=10; 
          System.out.println("i="+i); 
          System.out.println("k="+k); 
         } 
      System.out.println("i="+i); 
      System.out.println("k="+k); 
              } 
   } 

2)class Test {
public static void main(String args[]) {
	int 2a = 257;
	byte b2 = 2a;
	System.out.println("a=" + a);
	System.out.println("b=" + b);
}
}
3)求一组数字的平均值 */  
import javax.swing.JOptionPane;
	class Example2_18 { 
	public static void main(string args[]) { 
	double nums[5] = {10.1, 11.2, 12.3, 13.4, 14.5}; 
	double result = 0; 
	for(int i=0; i<5; i++) 
      {result = result + nums[i]; }
 JOptionPane.showMessageDialog(null,"平均值为: " + result / 5); //弹出对话框输出
 System.exit(0); //退出程序
 } 
} 
4)import java.io.*; 
  class KY2_9 { 
      public static void main(String args[]) throws IOException { 
        char ch; 
   System.out.println("按 1/2/3 数字键可得大奖!"); 
   System.out.println("按空格键后回车可退出循环操作."); 
   while ((ch=System.in.read())!=' ')
  { 
     System.in.skip(2);     // 跳过回车键,否则下次读入回车
     switch (ch) { 
       case 1: 
         System.out.println("恭喜你得大奖,一辆汽车!"); 
         break; 
       case 2: 
         System.out.println("不错呀,你得到一台笔记本电脑!"); 
         break; 
       case 3: 
         System.out.println("没有白来,你得到一台冰箱!"); 
         break; 
       default: 
         System.out.println("真不幸,你没有奖品!下次再来吧。"); 
     } 
   } 
 } 



2. 补充语句;
1)编写声明不同数据类型变量的程序文件 KY2_1.java,源代码如下,请补充完整: 
public class KY2_1 { 
public static void main(String args[]) { 
  
  System.out.println("字节型变量 b = "+b); 
  System.out.println("短整型变量 s = "+s); 
  System.out.println(" 整型变量 i = "+i); 
  System.out.println("长整型变量 l = "+l); 
  System.out.println("字符型变量 c = "+c); 
  System.out.println("浮点型变量 f = "+f); 
  System.out.println("双精度变量 d = "+d); 
  System.out.println("布尔型变量 B = "+B); 
  System.out.println("字符串类对象 S = "+S); 
} 
} 
2)在下面源代码中补充语句,使得输出如下的结果,编译并运行:
8 16 24 32 <>
7 14 21 28 <>

class Test {
public static void main(String args[]) {
    int j, k;

    for (j = 8; j >1; j--) {
        for (k=1; k<=9; k++) {
            if (k ==5)
	 
	 if (j==6)

	System.out.print(j*k+“ ”);
        }
        System.out.println(“<>”);
       }
}
}

Three. Programming questions

2. 通过命令行参数输入整数n和m,求两数的最小公倍数。
3. 编写程序,把字符串“1345”,“23.6”,“45.888”,“222”,“true"分别转换为整型、实型、双精度、逻辑型的基本类型(提示:用各基本类型对应包装类的parseXXX()方法)

3. Experimental analysis, code and operating results

1.
1)public class KY2_2 { 
static int i=10;
static int k=10;
public static void main(String args[]) { 
     { 
       
       System.out.println("i="+i); 
       System.out.println("k="+k); 
      } 
   System.out.println("i="+i); 
   System.out.println("k="+k); 
} 
} 

operation result:
Insert picture description here

Analysis : The scope of k needs to be placed outside in the main function, and then static members can only be used directly in static member functions, plus the static keyword

2)class Test {
public static void main(String args[]) {
	int a2 = 257;
	byte b2 = (byte)a2;
	System.out.println("a=" + a2);
	System.out.println("b=" + b2);
}
}

operation result:

Insert picture description here

Analysis: Identifier naming rules, cannot start with a number

3)求一组数字的平均值   
import javax.swing.JOptionPane;
class Example2_18 { 
public static void main(String args[]) { 
    double nums[] = {10.1, 11.2, 12.3, 13.4, 14.5}; 
    double result = 0; 
    for(int i=0; i<5; i++)
      {result = result + nums[i]; }
    JOptionPane.showMessageDialog(null,"平均值为: " + result / 5); //弹出对话框输出
	System.exit(0); //退出程序
  } 
}

operation result:

Insert picture description here

Analysis : Declare that the specific size of the array cannot be written, and it needs to be specified at new or at initialization (indirectly specified)
4)
import java.io.*;

class KY2_9 { 
public static void main(String args[]) throws IOException { 
char ch; 
System.out.println("按 1/2/3 数字键可得大奖!"); 
System.out.println("按空格键后回车可退出循环操作."); 
while ((ch=(char)System.in.read())!=' ')
{ 
 System.in.skip(2);     // 跳过回车键,否则下次读入回车
 switch (ch-'0') { 
   case 1: 
     System.out.println("恭喜你得大奖,一辆汽车!"); 
     break; 
   case 2: 
     System.out.println("不错呀,你得到一台笔记本电脑!"); 
     break; 
   case 3: 
     System.out.println("没有白来,你得到一台冰箱!"); 
     break; 
   default: 
     System.out.println("真不幸,你没有奖品!下次再来吧。"); 
 } 
} 
}
}

operation result:

Insert picture description here

Analysis: The read() method reads the ascii code value corresponding to the character, which needs to be converted to the char type, and the arithmetic operations between the char types will automatically be converted to the ascii code value of the int type, '1'-'0' <= => 49-48 = 1

2. 
1)

 public class KY2_1 { 
    public static void main(String args[]) { 
	      byte b = 123;
	      short s = 123;
	      int i = 123;
	      long l = 123;
	      char c = 'x';
	      float f = 123.0F;
	      double d = 123.000;
	      boolean B = true;
	      String S = "123";
	      System.out.println("字节型变量 b = "+ b); 
	      System.out.println("短整型变量 s = "+ s); 
	      System.out.println(" 整型变量 i = "+ i); 
	      System.out.println("长整型变量 l = "+ l); 
	      System.out.println("字符型变量 c = "+ c); 
	      System.out.println("浮点型变量 f = "+ f); 
	      System.out.println("双精度变量 d = "+ d); 
	      System.out.println("布尔型变量 B = "+ B); 
	      System.out.println("字符串类对象 S = "+ S); 
	}

operation result:

Insert picture description here

Analysis: Note (float f = 123.0F;) this, the default is double type, and F to float type is added after it

2) }

class Test {
	public static void main(String args[]) {
	    int j, k;
	    BT:
	    for (j = 8; j >1; j--) {
	        for (k=1; k<=9; k++) {
	            if (k ==5)
	            	break;
	            	
		 
		 if (j==6) {
			 break BT;
		 }
		System.out.print(j*k+ " ");
	        }
	        System.out.println("<>");
        }
	}
}

operation result:

Insert picture description here

Analysis: Goto is abandoned in java, you can use (break tagname;) (continue tagname;) to achieve jump

2. 
public class Work {
	
	public static void main(String args[]) {
	    int n = Integer.parseInt(args[0]);
	    int m = Integer.parseInt(args[1]);
	    int maxF = maxFactor(m, n);
	    System.out.println(m*n/maxF);
	}
	
	public static int maxFactor(int m, int n) { // 求最大公约数
		return n > 0 ? maxFactor(n, m%n) : m;
	}
}

operation result:

Insert picture description here

Analysis: First recursively find the greatest common divisor, the least common multiple is equal to (a * b) / greatest common divisor

3.  public static void main(String args[]) {
	  String a = "1345";
	  String b = "23.6";
	  String c = "45.888";
	  String d = "222";
	  String e = "true";
	  int a1 = Integer.parseInt(a);
	  float b1 = Float.parseFloat(b);
	  double c1 = Double.parseDouble(c);
	  int d1 = Integer.parseInt(d);
	  boolean e1 = Boolean.parseBoolean(e);
	 System.out.printf("a1:%s b1:%s c1:%s d1:%s e1:%s",a1,b1,c1,d1,e1);
	    
	}

operation result:

Insert picture description here

分析: 基本类型都有其对应的包装类和parseXXX方法,注意int 对应 Integer

to sum up:

Java has eight basic types and packaging classes, namely:
 
byte packaging class is Byte
short packaging class is Short
int packaging class is Integer
long packaging class is Long
float packaging class is Float
double packaging class is Double
char packaging class is Character
boolean packaging class Is Boolean

However, these types of packaging are in JAVA.lang.

Among them, Integer, Byte, Float, Double, Short, and Long are all subclasses of Number, and Character and Boolean are subclasses of Object.

Show a pop-up window: (Useful gadgets)

import javax.swing.JOptionPane;
JOptionPane.showMessageDialog(null,"平均值为: " + result / 5); //弹出对话框输出

Skip the enter key, otherwise read enter next time

System.in.skip(2);

Read a character:

system.in.read();  // 读取的为字符对应的ascii码,类型:int

Array note:

声明数组不能写具体的大小,需要在new时指定,或初始化时(间接指定)
如 int a[5] = {1,2,3,4,5};  // 错误

Guess you like

Origin blog.csdn.net/qq_46456049/article/details/108671392