1、矩形面積,周長封裝測試。
/**
* @author Administrator
*封裝好的矩形類
*自己私有的長寬屬性
*開放 求面積求周長的方法 和設置長寬的方法
*/
public class Jx {
// 自己的屬性長 私有的屬性private
private int chang;
// 自己的屬性寬
private int kuan;
//構造我這個矩形 開放的方法public
public Jx(int chang, int kuan) {
this.chang = chang;
this.kuan = kuan;
}
//求面積的方法
public int getMianji(){
return chang*kuan;
}
//求周長的方法
public int getZhouchang(){
return (chang+kuan)*2;
}
//得到矩形的長
public int getChang() {
return chang;
}
//設置長
public void setChang(int chang) {
this.chang = chang;
}
public int getKuan() {
return kuan;
}
public void setKuan(int kuan) {
this.kuan = kuan;
}
}
測試:
public class TestJx {
public static void main(String[] args) {
//生成一個矩形的實例 傳入一個長度和寬度過去
Jx jx=new Jx(5,6);
//求他的面積 周長 拿到他的長度 和寬度
System.out.println(jx.getMianji());
System.out.println(jx.getZhouchang());
System.out.println(jx.getChang());
System.out.println(jx.getKuan());
}
}
2、輸入一個字符串,統計元音個數。
import java.util.Scanner;
public class YuanYin {
public static void main(String[] args) {
System.out.println("請輸入一個字符串:");
Scanner input =new Scanner(System.in); /*從控制台輸入*/
String s= input.next();/*輸入一個字符*/
int count=0;/*定義變量,不用不出錯,一用就出錯所以要賦初始值*/
for (int i = 0; i < s.length(); i++) {
char a=s.charAt(i);/*表示每一個字母的位置*/
if (a=='a'||a=='e'||a=='i'||a=='o'||a=='u') {
count++;/*統計個數*/
}
}
System.out.println("元音的個數為:"+count);
}
}
3、回文數
public class HuiWenShu {
public static void main(String[] args) {
for (int i = 10000; i < 100000; i++) {
int a,b,c,d,e;
a=i/10000;
b=i%10000/1000;
c=i%1000/100;
d=i%100/10;
e=i%10;
if (a==e&b==d) {
System.out.println(i+"是回文數");
}
}
}
}
回文數加強版
import java.util.Scanner;
public class HWSPlus {
public static void main(String[] args) {
HWSPlus hws=new HWSPlus();
System.out.println("請輸入一個五位數:");
//類名 實例名 = new 類名();
Scanner input=new Scanner(System.in);
int i=input.nextInt();
hws.hws(i);
}
public void hws(int i){
int a,b,c,d,e;
a=i/10000;
b=i%10000/1000;
c=i%1000/100;
d=i%100/10;
e=i%10;
if (a==e&&b==d) {
System.out.println(i+"是回文數");
}
}
}
4、水仙花數
public class ShuixianHua {
public static void main(String[] args) {
for (int i = 0; i < 1000; i++) {
int a,b,c;
a=i/100;
b=i%100/10;
c=i%10;
if (a*a*a+b*b*b+c*c*c==i) {
System.out.println(i+"是水仙花數");
}
}
}
}
5、九九乘法表
public class JJCFB {
public static void main(String[] args) {
System.out.println("九九乘法表正這來");
for (int i = 1; i <= 9; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(j + "*" + i + "=" + j * i + "\t");
}
System.out.println();
}
System.out.println("九九乘法表逆這來");
for (int i = 9; i >= 1; i--) {
for (int j = 1; j <= i; j++) {
System.out.print(j + "*" + i + "=" + j * i + "\t");
}
System.out.println();
}
}
}
6、一球從100米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在第10次落地時,共經過多少米?第10次反彈多高?
public class Qiu {
public static void main(String[] args) {
int g=100,tq=50;
int total=0+100;
for (int i = 0; i < 10; i++) {
total=total+2*tq;
tq=tq/2 ;
}
System.out.println(total);
System.out.println(tq);
}
}
7、求1+2!+3!+...+20!的和
public class JieCheng {
public static void main(String[] args) {
int sum=0;
for (int i = 1; i <= 20; i++) {
sum+=jc(i);
}
System.out.println("1到20的階乘是:"+sum);
}
public static int jc(int i){
if (i>1) {
return i*jc(i-1);
}
else{
return 1;
}
}
}
8驗證身份證是否合法。
import java.util.Scanner;
public class Sfzjs {
public static void main(String[] args) {
System.out.println("請輸入您的身份證號碼:");
Scanner input = new Scanner(System.in);
String i = input.next();
int count = i.length();
System.out.println("身份證位數為:"+count);
if (count==18||count==15) {
System.out.println("合法");
}
else{
System.out.println("不合法");
}
}
}
9、郵箱驗證
import java.util.Scanner;
/**
* 1234@qq.com 必須要包含@和.
*
* @前必須要有七位
* @.只有一個 必須由.org .cn .com 結尾
* @和.不能連續
*
* 驗證郵箱1234@qq.com ①必須要包含@和. indexof ②@前必須要有七位 split @ 0的長度是否大於7 ③@.只有一個
* indexof==lastindexof ④必須由.org .cn .com 結尾 endswith ⑤@和.不能連續 contains @.
*/
public class Yxyz {
public static void main(String[] args) {
Scanner sca = new Scanner(System.in);
String s = sca.nextLine();
// String s = "12356724@qq.cn";
// tring if (s.indexOf("@") == -1) {
// System.out.println("必須包含@");
// }
// String []strs=s.split(",");//1 2 3@
// for (int i = 0; i < strs.length; i++) {
// System.out.println(strs[i]);
// }
String[] strs = s.split("@");
if (strs[0].length() < 7) {
System.out.println("@前必須大於7位");
}
pd(s, "@");
pd(s, ".");
if (!(s.endsWith(".org") || s.endsWith(".cn") || s.endsWith(".com"))) {
System.out.println("必須以指定格式結尾");
}
if (s.contains("@.") || s.contains(".@")) {
System.out.println("@.不能連續");
}
}
/**
* 傳入原始的字符串 和判斷字符串 打印是否包含此字符串
* @param s
* @param str
*/
private static void pd(String s, String str) {
int index = s.indexOf(str);
// System.out.println(index);
int lastindex = s.lastIndexOf(str);
// System.out.println(lastindex);
if (index != lastindex) {
System.out.println(str + "僅有一個");
}
}
}
10、冒泡排序
import java.util.Arrays;
public class BJdx {
public static void main(String[] args) {
//1.定義一組數組
int[] arr = { 23, 4, 6, 78, 12 };
//2.對數組排序
arr = SF.sort(arr);
//3.打印數組
System.out.println(Arrays.toString(arr));
}
}
class SF{
/**
* 給定一個數組,返回一個排好序的數組
* @param arr 帶排序的數組
* @return 排序完成的數組
*/
public static int[] sort(int[] arr){
//如果arr為null或者空,則返回空數組
if(arr==null||arr.length==0)
return new int[]{};
//循環遍歷數組元素
for (int i = 0; i < arr.length; i++) {
//循環遍歷i之后的元素
for (int j = i; j < arr.length; j++) {
//比較該輪元素是否比第i個元素小,如果小,則交換
if(arr[j] < arr[i]){
int tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
}
}
//返回排好序的數組
return arr;
}
}
11、統計勸學中的,。
public class SavaQX {
public static void main(String[] args) {
String s = " 青,取之於藍而青於藍;冰,水為之而寒於水。木直中繩,輮(左應為'車',原字已廢除)以為輪,其曲中規。雖有槁暴,不復挺者,輮使之然也。故木受繩則直,金就礪則利,君子博學而日參省乎己,則知明而行無過矣。 故不登高山,不知天之高也;不臨深溪,不知地之厚也;不聞先王之遺言,不知學問之大也。干、越、夷、貉之子,生而同聲,長而異俗,教使之然也。 曰:“嗟爾君子,無恆安息。靖共爾位,好是正直。神之聽之,介爾景福。”神莫大於化道,福莫長於無禍。吾嘗終日而思矣,不如須臾之所學也;吾嘗跂而望矣,不如登高之博見也。登高而招,臂非加長也,而見者遠;順風而呼,聲非加疾也,而聞者彰。假輿馬者,非利足也,而致千里;假舟楫者,非能水也,而絕江河。君子生非異也,善假於物也。南方有鳥焉,名曰蒙鳩,以羽為巢,而編之以發,系之葦苕,風至苕折,卵破子死。巢非不完也,所系者然也。西方有木焉,名曰射干,莖長四寸,生於高山之上,而臨百仞之淵,木莖非能長也,所立者然也。蓬生麻中,不扶而直;白沙在涅,與之俱黑。蘭槐之根是為芷,其漸之滫,君子不近,庶人不服。其質非不美也,所漸者然也。故君子居必擇鄉,游必就士,所以防邪辟而近中正也。物類之起,必有所始。榮辱之來,必象其德。肉腐出蟲,魚枯生蠹。怠慢忘身,禍災乃作。強自取柱,柔自取束。邪穢在身,怨之所構。施薪若一,火就燥也,平地若一,水就濕也。草木疇生,禽獸群焉,物各從其類也。是故質的張,而弓矢至焉;林木茂,而斧斤至焉;樹成蔭,而眾鳥息焉。醯酸,而蚋聚焉。故言有招禍也,行有招辱也,君子慎其所立乎!積土成山,風雨興焉;積水成淵,蛟龍生焉;積善成德,而神明自得,聖心備焉。故不積跬步,無以至千里;不積小流,無以成江海。騏驥一躍,不能十步;駑馬十駕,功在不舍。鍥而舍之,朽木不折;鍥而不舍,金石可鏤。蚓無爪牙之利,筋骨之強,上食埃土,下飲黃泉,用心一也。蟹六跪而二螯,非蛇鱔之穴無可寄托者,用心躁也。是故無冥冥之志者,無昭昭之明;無惛惛之事者,無赫赫之功。行衢道者不至,事兩君者不容。目不能兩視而明,耳不能兩聽而聰。螣蛇無足而飛,鼫鼠五技而窮。《詩》曰:“屍鳩在桑,其子七兮。淑人君子,其儀一兮。其儀一兮,心如結兮!”故君子結於一也。昔者瓠巴鼓瑟,而流魚出聽;伯牙鼓琴,而六馬仰秣。故聲無小而不聞,行無隱而不形 。玉在山而草潤,淵生珠而崖不枯。為善不積邪?安有不聞者乎? 學惡乎始?惡乎終?曰:其數則始乎誦經,終乎讀禮;其義則始乎為士,終乎為聖人, 真積力久則入,學至乎沒而后止也。故學數有終,若其義則不可須臾舍也。為之,人也;舍 之,禽獸也。故書者,政事之紀也;詩者,中聲之所止也;禮者,法之大分,類之綱紀也。 故學至乎禮而止矣。夫是之謂道德之極。禮之敬文也,樂之中和也,詩書之博也,春秋之微 也,在天地之間者畢矣。 君子之學也,入乎耳,着乎心,布乎四體,形乎動靜。端而言,蝡而動,一可以為法則。小人之學也,入乎耳,出乎口;口耳之間,則四寸耳,曷足以美七尺之軀哉!古之學者為己,今之學者為人。君子之學也,以美其身;小人之學也,以為禽犢。故不問而告謂之傲,問一而告二謂之囋。傲、非也,囋、非也;君子如向矣。";
int conut = 0;
int conut1 = 0;
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c == ',') {
conut++;
}
if (c == '。') {
conut1++;
}
}
System.out.println(",的個數為" + conut);
System.out.println("。的個數為" + conut1);
}
}
13、構造一個People方法
/**
*
* 姓名name、性別gender、年齡age
*
*/
public class People {
private String name;
private int age;
private boolean gender;
public static void walk(String name) {
System.out.println(name + "正在走路");
}
public static void eat(String name) {
System.out.println(name + "正在吃飯");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public boolean isGender() {
return gender;
}
public void setGender(boolean gender) {
this.gender = gender;
}
}
public class TestPeople {
public static void main(String[] args) {
People p=new People();
p.walk("lucy");
p.eat("李四");
}
}