Java実験1:基本的なJava文法とオブジェクト指向はじめに:単純なクラスが一緒に実装され、線形スクリーニングメソッドが一緒に実装され、カレンダーと永久カレンダーの曜日が一緒に実装されます(Western Electric Experiment)

最初の質問:円形クラスと長方形クラスを実現する

私の解決策:

import java.math.*;

class Rectangle{
    
    
private
		int w, h;
		static int AllCnt;
		static int AllArea;
public
	Rectangle(int w_, int h_) {
    
    
		w = w_;
		h = h_;
		AllCnt++;
		AllArea += w * h;
	}
	static void Show() {
    
    
		System.out.println("AllCnt = " + AllCnt + "  " + "  AllArea = " + AllArea);
	}
	int CirRec() {
    
    
		return 2 * (w + h);
	}
	int AreRec() {
    
    
		return w * h;
	}
}


class Circle{
    
    
final double pi = Math.acos(-1);
private
	double r;
protected
	Circle(double rr) {
    
    
		r = rr;
	}

	double GetCir() {
    
    
		return 2.0 * pi * r;
	}
	
	double GetAre() {
    
    
		return pi * r * r;
	}
	
public
	void Show() {
    
    
		System.out.println("Circle = " + GetCir() + "  Area = " + GetAre());
	}
}

public class Main6 {
    
    
	public static void main(String[] args) {
    
    
		System.out.println("下面测试矩形类:");
		Rectangle.AllArea = 0;
		Rectangle.AllCnt = 0;
		Rectangle r1 = new Rectangle(1, 2);
		Rectangle.Show();
		System.out.println("r1 : Cir = " + r1.CirRec() + "  Area = " + r1.AreRec());
		Rectangle r2 = new Rectangle(3, 4);
		Rectangle.Show();
		System.out.println("r2 : Cir = " + r2.CirRec() + "  Area = " + r2.AreRec());
		System.out.println("下面测试圆形类:");
		Circle c1 = new Circle(2.0);
		c1.Show();
		Circle c2 = new Circle(3.0);
		c2.Show();
	}
}

質問2:1から100までの素数の合計

私の解決策:線形ふるい法

import java.util.Arrays;

public class Main1 {
    
    
	static int primeS() {
    
    
		int cnt = 0, ans = 0;
		boolean[] vis = new boolean[105];
		int[] isPrime = new int[105];
		Arrays.fill(isPrime, 0);
		Arrays.fill(vis, true);
		for(int i = 2;i <= 100;++i) {
    
    
			if(vis[i]) {
    
    
				isPrime[cnt++] = i;
				ans += i;
			}
			for(int j = 0;j < cnt && i * isPrime[j] <= 100;++j) {
    
    
				vis[i * isPrime[j]] = false;
				if(0 == i % isPrime[j])
					break;
			}
		}
		return ans;
	}
	public static void main(String[] args) {
    
    
		int ans = primeS();
		System.out.println(ans);
	}
}

3番目の質問:

2つの機能を提供するプログラムを作成します。

  1. ユーザーが入力した年に従ってその年のカレンダーを出力します。
  2. ユーザーが入力した日付に従って、その日の週を出力します。
    プログラムは、人間とコンピュータの良好な対話パフォーマンスを備えている必要があります。つまり、プログラムはユーザーに機能の説明を表示し、ユーザーの機能選択に従って対応する機能を実行し、最終的な実行結果を詳細な説明情報とともに提供する必要があります。

既知の日付の週を見つける方法を紹介します。

平日=(dd + 2 * dm + 3 *(dm + 1)/ 5 + dy + dy / 4-dy / 100 + dy / 400)%7 + 1

dy =年
dm =月
dd =日

ここ注意すべき点
、月が3未満の場合です!1年借りる必要があり、月に12を加える!(ポイント!)
(この計算方法では、月、日、年の計算がわかりませんでした。知っている人がコメントしたり、プライベートメッセージを送ったりしてくれることを願っています!兄弟に感謝しています!!!)
もちろん、これを使用しない場合計算式は、サイクルに依存して答えを書くこともできます。

私の解決策:(この質問を40分以上書いた...少し難しい)

import java.util.Scanner;
public class Main2 {
    
    
	static boolean checkLeap(int Ly) {
    
    
		if((0 == Ly % 4 && 0 != Ly % 100) || 0 == Ly % 400)
			return true;
		else
			return false;
	}
	
	public static int GetWeek(int dy, int dm, int dd) {
    
    
		return (dd + 2 * dm + 3 * (dm + 1) / 5 + dy + dy / 4 - dy / 100 + dy / 400) % 7 + 1;
	}
	
	public static void main(String[] args) {
    
    
		Scanner sc = new Scanner(System.in);
		System.out.println("请选择万年历的功能:1-> 查询日历?    2->查询某日期是星期几?");
		int cmd = sc.nextInt();
		if(1 == cmd) {
    
    
			System.out.println("请输入您要查询日历的年份:");
			int Year = sc.nextInt();
			int day = GetWeek(Year - 1, 13, 1);
			for(int i = 1;i <= 12;++i) {
    
    
				int TempDay = 0, cnt = 1;
				switch(i) {
    
    
				case 1:
				case 3:
				case 5:
				case 7:
				case 8:
				case 10:
				case 12:
					TempDay = 31;
					break;
				case 2:
					if(checkLeap(Year))
						TempDay = 29;
					else
						TempDay = 28;
					break;
				default:
					TempDay = 30;	
				}
				System.out.println("万年历:======第 " + i + " 月份:==========");
				System.out.println("日\t一\t二\t三\t四\t五\t六");
				boolean book = false;
				for(int j = 0;j < 6;++j) {
    
    
					for(int k = 0;k < 7;++k) {
    
    
						if(0 == j && k < day) {
    
    
							System.out.print("\t");
							continue;
						}
						if(cnt > TempDay) {
    
    
							book = true;
							break;
						}
						System.out.print(cnt + "\t");
						cnt++;
					}
					if(book)
						break;
					System.out.println();
				}
				day = (day + TempDay) % 7;
				System.out.println();
				System.out.println();
			}
		}
		else {
    
    
			System.out.println("请按照如下格式:YYYY-MM-DD输入,如果非法输入将不受理!");
			int dy = 0, dm = 0, dd = 0, i = 0, j = 0;
			@SuppressWarnings("unused")
				String getChar = sc.nextLine();
			String DataInfo = sc.nextLine();
			while(i < DataInfo.length()) {
    
    
				if(0 == dy) {
    
    
					j = i;
					while(j < DataInfo.length() && '-' != DataInfo.charAt(j)) {
    
    
						dy = dy * 10 + (DataInfo.charAt(j) - '0');
						j++;
					}
				}
				else if(0 == dm) {
    
    
					j = i;
					while(j < DataInfo.length() && '-' != DataInfo.charAt(j)) {
    
    
						dm = dm * 10 + (DataInfo.charAt(j) - '0');
						j++;
					}
				}
				else if(0 == dd) {
    
    
					j = i;
					while(j < DataInfo.length() && '-' != DataInfo.charAt(j)) {
    
    
						dd = dd * 10 + (DataInfo.charAt(j) - '0');
						j++;
					}
				}
				i = ++j;
			}
			if(dm < 3) {
    
    
				dm += 12;
				dy--;
			}
			int AnsDay = GetWeek(dy, dm, dd);
			System.out.println("您输入的日期对应的星期是:" + AnsDay);
		}
		sc.close();
	}
}

おすすめ

転載: blog.csdn.net/qq_44274276/article/details/104790889