實驗一 Java語言基礎
技術標籤:# Java實驗合集java程式語言實驗程式碼Java實驗設計Java 九九乘法表
目錄
1.分別用for和while迴圈計算1!+2!+…+10!的值。
2. 編寫程式輸出26個大寫字母的ASCII對照表,程式執行參考結果如1-5所示。
3.編寫Java程式,從鍵盤輸入年份和月份,然後輸出該月份的天數(考慮是否閏年)
4.請設計一個方法判斷使用者輸入月份計算機出該月的天數,並進行測試
6.使用選擇法(冒泡法、快速排序法、直接插入法)對陣列排序,並進行測試
點選這裡下載本文及所有實驗程式碼合集,超實惠喲!
一、實驗目的
1.掌握Java 開發環境的搭建,系統環境變數path、classpath的配置;
2.掌握Java 的執行機制;
3.掌握Java的基本語法格式,識別符號的命名規範;
4.掌握Java語言中的常量與變數、變數的作用域;
5.掌握Java語言運算子與表示式的使用、運算子的優先順序;
6.掌握Java程式的流程控制(即順序結構、選擇結構和迴圈結構)的使用。
7.掌握Java中方法的定義、過載與使用;
8.掌握 Java 陣列的定義與遍歷、求最值、排序等操作。
二、實驗程式碼
1.分別用for和while迴圈計算1!+2!+…+10!的值。
package 作業練習.test1; public class Exer01 { public static void main(String[] args) { // 用for迴圈實現1-10的階乘之和! int total_1 = 1; double num_1 = 0; // 設定迴圈,首先定義判斷值,後面椒判斷條件,最後是判斷次數 for (int i = 1; i <= 10; i++) { total_1 *= i; num_1 += total_1; } System.out.println("1-10的階乘之和為:" + num_1); // 用while迴圈實現1-10的階乘之和! int i = 1; int total = 1; double num = 1; while (i < 10) { i++; total *= i; num += total; } System.out.println("1-10的階乘之和為:" + num); } }
2. 編寫程式輸出26個大寫字母的ASCII對照表,程式執行參考結果如1-5所示。
package 作業練習.test1;
public class Exer02 {
public static void main(String[] args) {
System.out.println("\t\t" + "大寫字母的ASCLL對照表");
int n = 0;
for (int i = 65; i <= 90; i++) {
System.out.print((char) i + "\t");
System.out.print(i + "\t");
n++;
if (n % 5 == 0)
System.out.println();
}
}
}
3.編寫Java程式,從鍵盤輸入年份和月份,然後輸出該月份的天數(考慮是否閏年)
package 作業練習.test1;
import java.util.Scanner;
public class Exer03 {
public static void main(String[] args) {
System.out.println("請輸入年份:");
Scanner sc = new Scanner(System.in);
int year = sc.nextInt();
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
System.out.println("這是閏年!!");
} else {
System.out.println("這不是閏年!!");
}
}
}
4.請設計一個方法判斷使用者輸入月份計算機出該月的天數,並進行測試
package 作業練習.test1;
import java.util.Scanner;
public class EXer04 {
public static void main(String[] args) {
// 例項化Scanner
Scanner scan = new Scanner(System.in);
// 提示使用者輸出月份
System.out.println("請輸入月份:");
// 接收使用者輸入的月份
int month = scan.nextInt();
// 判斷月份(注意break位置)
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
System.out.println("31天");
break;
case 4:
case 6:
case 9:
case 11:
System.out.println("30天");
break;
case 2:
System.out.println("請輸入對應年份:");
int year = scan.nextInt();
// 閏年判斷
if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0)) {
System.out.println("29天");
} else {
System.out.println("28天");
}
break;
default:
System.out.println("請輸入正確月份~");
break;
}
}
}
5.請設計一個方法輸出實心的菱形,並進行測試
package 作業練習.test1;
public class Exer05 {
public static void main(String[] args) {
int n = 4;
//前4行.上半部分
for (int i = 1; i <= n; i++)//控制行數
{
for (int k = n - 1; k >= i; k--)//列印空格
{
System.out.print(" ");
}
for (int j = 1; j <= 2 * i - 1; j++)//列印*
{
System.out.print("*");
}
System.out.println();
}
//後3行,下半部分
for (int i = n - 1; i >= 1; i--) {
for (int k = i; k <= n - 1; k++) {
System.out.print(" ");
}
for (int j = 1; j <= 2 * i - 1; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
6.使用選擇法(冒泡法、快速排序法、直接插入法)對陣列排序,並進行測試
package 作業練習.test1;
import java.util.Scanner;
public class Exer08 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int Max=0;
int[] score = new int[5]; //自定義陣列長度
System.out.println("please input five numbers:");
for(int i=0; i< score.length; i++){
score[i] = input.nextInt();
}
for(int j=0; j<score.length-1; j++){
swap(score); //呼叫陣列排序方法
}
System.out.println("########## the result: ###########");
for(int i=0; i<score.length; i++){
System.out.print(score[i]+"\t");
}
}
public static void swap(int[] arr){ //冒泡法排序
for(int i=0; i<arr.length-1; i++){
if(arr[i]>arr[i+1]){
int temp = arr[i];
arr[i] = arr[i+1];
arr[i+1] = temp;
}
}
}
}
7.九九乘法表列印
public class Exer01 {
/**
* 實現一個簡單的乘法表
*/
public static void main(String[] args) {
for(int i=1;i<=9;i++)
{
for(int j=1;j<=i;j++)
{
System.out.print(j+"*"+i+"="+i*j+" ");
}
System.out.println("");//必須在第二個for迴圈之外
}
}
}
8.階梯陣列輸出練習
package 作業練習.test1;
public class Exer07 {
public static void main(String[] args) {
int triangle[][]=new int[10][];// 建立二維陣列
// 遍歷二維陣列的第一層
for (int i = 0; i < triangle.length; i++) {//控制行數,輸出10行
triangle[i]=new int[i+1];// 初始化第二層陣列的大小
// 遍歷第二層陣列
for(int j=0;j<=i;j++){//控制列數
// 將兩側的陣列元素賦值為1
if(i==0||j==0||j==i){
triangle[i][j]=1;
}else{//其他數值通過公式計算
triangle[i][j]=triangle[i-1][j]+triangle[i-1][j-1];
}
System.out.print(triangle[i][j]+"\t");// 輸出陣列元素
}
System.out.println();//換行
}
}
}
9.求sin和cos值
package 作業練習.test1;
public class Exer06 {
private static double pi=3.141592658;
//定義一個求絕對值的方法
double myabs(double n){
if(n<0){
n*=(-1);
}
return n;
}
//求sin
double mysin(double x){
int i=1,sign=1;
double item=x,frac=0,fz=x,fm=1;
for(;myabs(item)>=10E-5;i+=2){
frac+=item;//累加
fz=fz*x*x;//分子
fm=fm*(i+1)*(i+2);//分母
sign=-sign;//符號
item=sign*(fz/fm);//臨時變數
}
return (frac);
}
public static void main(String[] args){
Exer06 test = new Exer06();
System.out.println(test.mysin(0.25*pi));
}
}
三、實驗補充
一、命名規範
1、專案名全部小寫
2、包名全部小寫
3、類名、介面名首字母大寫,如果類名、介面名由多個單片語成,每個單詞的首字母都要大寫。
如:public class MyFirstClass{}
4、變數名、方法名首字母小寫,如果名稱由多個單片語成,每個單詞的首字母都要大寫。
如:int index=0;
public void toString(){}
5、常量名全部大寫
如:public static final String GAME_COLOR=”RED”;
6、所有命名規則必須遵循以下規則:
1)、名稱只能由字母、數字、下劃線、$符號組成
2)、不能以數字開頭
3)、名稱不能使用JAVA中的關鍵字。
4)、堅決不允許出現中文及拼音命名。
二、註釋規範
1、類註釋
在每個類前面必須加上類註釋,註釋模板如下:
/**
* Copyright (C), 2006-2010, ChengDu Lovo info. Co., Ltd.
* FileName: Test.java
*類的詳細說明
*
* @author類建立者姓名
* @Date建立日期
* @version 1.00
*/
2、屬性註釋
在每個屬性前面必須加上屬性註釋,註釋模板如下:
/**提示資訊 */
private String strMsg = null;
3、方法註釋
在每個方法前面必須加上方法註釋,註釋模板如下:
/**
*類方法的詳細使用說明
*
* @param引數1引數1的使用說明
* @return返回結果的說明
* @throws異常型別.錯誤程式碼 註明從此類方法中丟擲異常的說明
*/
4、構造方法註釋
在每個構造方法前面必須加上註釋,註釋模板如下:
/**
*構造方法的詳細使用說明
*
* @param引數1引數1的使用說明
* @throws異常型別.錯誤程式碼 註明從此類方法中丟擲異常的說明
*/
5、方法內部註釋
在方法內部使用單行或者多行註釋,該註釋根據實際情況新增。
如://背景顏色
Color bgColor = Color.RED
每文一語
有時候遇見了錯誤,也要享受美麗的邂逅,盡力所為,不留遺憾才是正確的選擇!
本文幸運色是淺藍色喲!天空很大,可以任憑你飛躍!加油!