给丁丁的复习宝典
/**
1、定義一個整型的長度為5 x 4的二維數組k[5][4],并將數組中元素k[i][j] 值初始化為值i x j。然后,將元素k[2][3]打印出來。(本題8分)
*/
package cn.itcase.kaoshi;
public class test1 {public static void main(String[] args){int[][] k=new int[6][5];for(int i=1;i<=5;i++){for(int j=1;j<=4;j++){k[i][j]=i*j;}}System.out.println(k[2][3]);}
}//創建二維數組 int[][] k=new int[6][5];
/**
2.從命令行輸入幾個字符串,統計并打印出輸入字符串的個數、以及各字符串的字符個數。(提示:args.length / args[k].length() )
*/
public class test1{public static void main(String[] args){int n=args.length;System.out.println("字符串的個數是"+n);for(int i=0;i<n;i++){System.out.println("第"+(i+1)+"個字符串的長度為"+args[i].length());}}
}//字符串的個數 args.length
//每個字符串的長度 args[i].length()
/**
3、編程:從鍵盤讀取星號數,采用循環語句打印如下圖形。如:從鍵盤輸入10,則星號排列最大數是10。注意星號數每行需要為奇數。(本題12分)
*/
import java.util.Scanner;
public class test1{public static void main(String[] args){Scanner input=new Scanner(System.in);int n=input.nextInt();for(int i=1;i<=n;i++){for(int j=1;j<=n-i;j++){System.out.print(" ");}for(int j=1;j<=2*i-1;j++){System.out.print("*");}System.out.println();}}
}//import java.util.Scanner;
//Scanner input=new Scanner(System.in);
//注意每一行之前有空行
//注意每一行后有回車
/**
4、編程:從鍵盤讀取星號數,采用循環語句打印如下圖形。如:從鍵盤輸入10,則星號排列最大數是10。(12分)
*/import java.util.Scanner;
public class test1{public static void main(String[] args){Scanner input=new Scanner(System.in);int n=input.nextInt();for(int i=1;i<=n;i++){for(int j=1;j<=n-i;j++){System.out.print(" ");}for(int j=1;j<=i;j++){System.out.print("*");}System.out.println();}}
}
/**
5、在com.graphic包中定義一個圓柱體類Cylinder,其半徑r,高h都為私有屬性,有構造方法和求體積的方法volume()。在com.test包中定義一個測試類test,測試一個半徑為5.34、高為2的圓柱體體積。(計算時π的值可以用Math.PI, 圓柱體體積公式為π*r *r *h)
*/
package com.graphic;
public class Cylinder {private double r;private double h;public Cylinder(double r,double h){this.r=r;this.h=h;}public double volume(){return Math.PI*r*r*h;}
}package com.test;
import com.graphic.Cylinder;
public class test {public static void main(String[] args){Cylinder c=new Cylinder(5.34,2);System.out.println(c.volume());}
}//用double
//import com.graphic.Cylinder;
/**
6、首先定義一個計算二維坐標系中圓面積的類CircleClass,要求類中有一個定義圓心座標,圓上一點座標的構造函數,以及一個通過圓上一點座標與圓心座標計算圓面積的方法area。然后,通過上述類生成兩個圓對象circle1、circle2進行測試:一個圓心、圓上一點座標分別為(0,0)、(8.5,9),另一個圓心、圓上一點座標分別為(2,3.5)、(9,6),并分別顯示各自面積。 (計算時π的值可以用Math.PI)
*/package timu6;
public class test {public static void main(String[] args){CircleClass c1=new CircleClass(0,0,8.5,9);CircleClass c2=new CircleClass(2,3.5,9,6);System.out.println(c1.area());System.out.println(c2.area());}
}
class CircleClass {private double x0,y0,x,y;public CircleClass(double x0,double y0,double x,double y){this.x0=x0;this.y0=y0;this.x=x;this.y=y;}public double area(){return Math.PI*Math.sqrt((x-x0)*(x-x0)+(y-y0)*(y-y0));}
}//構造函數括號里不要忘記寫變量類型
//可以直接放在同一個類里面 但是只有一個public(test那個)
/**
7、設計一個學生抽象類Student,其數據成員有name(姓名)、age(年齡)和degree(學位),以及一個抽象方法show()。然后由Student類派生出本科生類Undergraduate和研究生類Graduate,本科生類Undergraduate增加成員specialty(專業),研究生類增加成員direction(研究方向)。并且每個類都有show()方法,用于輸出數據成員信息。最后請定義幾個對象“張三”、“李四”、“王五”、“劉六”及其屬性,并打印輸出下列信息:(本題18分)
張三:20,本科,通信專業
李四:21,本科,電子專業
王五:25,碩士,無線通信
劉六:32,博士,光纖通信
*/public class test1{ public static void main(String[] args){Undergraduate s1=new Undergraduate("張三",20,"本科","通信專業");Undergraduate s2=new Undergraduate("李四",21,"本科","電子專業");Graduate s3=new Graduate("王五",25,"碩士","無線通信");Graduate s4=new Graduate("劉六",32,"博士后","光纖通信");s1.show();s2.show();s3.show();s4.show();}
}abstract class Student {String name,degree;int age;public Student(String name,int age,String degree){this.name=name;this.age=age;this.degree=degree;}public abstract void show();
}class Undergraduate extends Student{private String specialty;public Undergraduate(String name,int age,String degree,String specialty){super(name,age,degree);this.specialty=specialty;}public void show(){System.out.println(name+":"+age+","+degree+","+specialty);}
}class Graduate extends Student{private String direction;public Graduate(String name,int age,String degree,String direction){super(name,age,degree);this.direction=direction;}public void show(){System.out.println(name+":"+age+","+degree+","+direction);}
}//抽象類內不用private
//子類里的構造函數要用super
/**
8、設計一個Shape接口和它的兩個實現類Square和Circle,要求如下:1)Shape接口中有一個抽象方法,方法名字是area,方法接收一個double類型的參數,返回一個double類型的結果。2)Square和Circle中實現了Shape接口,分別求正方形和圓形的面積并返回。在測試類中創建Square和Circle類的對象,計算邊長為2的正方形面積和半徑為3的圓面積。
*/public class test1{public static void main(String[] args){Square s1=new Square(2);Circle s2=new Circle(3);System.out.println(s1.area(2));System.out.println(s2.area(3));}
}interface Shape{double area(double x);
}class Square implements Shape{double bc;public Square(double bc){this.bc=bc;}public double area(double x){return x*x;}
}class Circle implements Shape{double r;public Circle(double r){this.r=r;}public double area(double x){return Math.PI*x*x;}
}//用的是接口 interface Shape
//子類用 implements Shape
/**
9、定義一個接口RectangleInterface,要求接口中定義長方形的求面積的抽象方法void rectArea()和求周長的抽象方法void rectLen()。類RectangleClass實現該接口,并定義兩個參數分別為長方形的長和寬的構造方法。通過上述RectangleClass類生成兩個長寬分別為(7.8,4.5)和(10.1,2.8)的長方形對象rectangle1, rectangle2,并計算和打印出他們的面積和周長。
*/public class test1{public static void main(String[] args){RectangleClass r1=new RectangleClass(7.8,4.5);RectangleClass r2=new RectangleClass(10.1,2.8);r1.rectArea();r1.rectLen();r2.rectArea();r2.rectLen();}
}interface RectangleInterface{public void rectArea();public void rectLen();
}class RectangleClass implements RectangleInterface{double x,y;public RectangleClass(double x,double y){this.x=x;this.y=y;}public void rectArea(){System.out.println(x*y);}public void rectLen(){System.out.println(2*(x+y));}
}
/**
10、對字符串“23 10 -8 0 3 7 108”中的數值進行升序排序,生成一個數值有序的字符串 “-8 0 3 7 10 23 108”。
*/
import java.util.*;
public class test1{public static void main(String[] args){String a="23 10 -8 0 3 7 108";String[] b=a.split(" ");int[] c=new int[b.length];for(int i=0;i<b.length;i++){c[i]=Integer.parseInt(b[i]);}Arrays.sort(c);String ans="";ans=ans+c[0];for(int i=1;i<b.length;i++){ans=ans+" "+c[i];}System.out.println(ans);}
}
//數組排序要用到 import java.util.*;
//字符串分開到數組 String[] b=a.split(" ");
//c[i]=Integer.parseInt(b[i]); 字符變成數字
/**
11、編寫程序統計一個字符子串在一個字符串中出現的次數和位置。打印子字符串“nba”在字符串“asfasfnabaasdfnbasdnbasnbasdnbadfasdf”中出現的次數和出現的位置。
*/public class test1{public static void main(String[] args){String s1="nba";String s2="asfasfnabaasdfnbasdnbasnbasdnbadfasdf";int cnt=0;for(int i=0;i<s2.length()-s1.length();i++){if(s2.substring(i,i+s1.length()).equals(s1)){System.out.print(i+1+" ");cnt++;}}System.out.println("\n共出現了"+cnt+"次");}
}
//子串相等 s2.substring(i,i+s1.length()).equals(s1)
/**
12.從命令行輸入一個數字,判斷它是奇數還是偶數。(提示:利用%;三元條件 或 if/else ; int a=Integer.parseInt(args[0]) //數據輸入)
*/
public class test1{public static void main(String[] args){int n=Integer.parseInt(args[0]);if(n%2==0) System.out.println(n+"是偶數");else System.out.println(n+"是奇數");}
}
//獲取命令行數字 int n=Integer.parseInt(args[0]);
/**
13.從命令行輸入兩個數字,判斷兩個數誰大誰小。(提示:讀輸入參數args[];三元條件 或 if/else )
*/public class test1{public static void main(String[] args){int n=Integer.parseInt(args[0]);int m=Integer.parseInt(args[1]);if(n>m) System.out.println(n+">"+m);else if(n<m) System.out.println(n+"<"+m);else System.out.println(n+"="+m);}
}
/**
14. 設計二維數組,輸出、處理楊輝三角形,顯示10行的楊輝三角形:
*/public class test1{public static void main(String args[]){int[][] a=new int[11][11];for(int i=1;i<=10;i++){a[i][1]=1;a[i][i]=1;}for(int i=3;i<=10;i++){for(int j=2;j<i;j++){a[i][j]=a[i-1][j-1]+a[i-1][j];}}for(int i=1;i<=10;i++){for(int j=1;j<=i;j++){if(j!=1) System.out.print(" ");System.out.print(a[i][j]);}System.out.println();}}
}//先對1賦值 再對3到10行 第2列到i-1賦值
/**
15. 猜數字游戲:
編寫一個猜數字游戲的程序,預先生成一個0-9的隨機數,用戶鍵盤錄入一個所猜的數字,如果輸入的數字和后臺預先生成的數字相同,則表示猜對了,這時,程序會打印“恭喜您,答對了!”如果不相同,則比較輸入的數字和后臺預先生成的數字大小,如果大了,打印“sorry,您猜大了!”如果小了,打印“sorry,您猜小了!”如果一直猜錯,則游戲一直繼續,直到數字猜對為止。(教材任務2-2)*/import java.util.Scanner;
public class test1{public static void main(String[] args){int num=(int)(Math.random()*10);Scanner input=new Scanner(System.in);int n=input.nextInt();while(num!=n){if(num>n) System.out.println("sorry,您猜小了!");else if(num<n) System.out.println("sorry,您猜大了!");n=input.nextInt();}System.out.println("恭喜您,答對了!");}
}//隨機數 int num=(int)(Math.random()*10);
/**
17. 設計一個表示圖書的Book類,它包含圖書的書名、作者、月銷售量等屬性,另有兩個構造方法(一個不帶參數,另一個帶參數),成員方法setBook( ) 和printBook()分別用于設置和輸出書名、作者、月銷售量等數據。并設計相應的測試Book類的應用程序主類,測試并顯示輸出提供所有功能的結果。
*/public class test1{public static void main(String[] args){Book b=new Book("西游記","吳承恩",200);b.printBook();}
}class Book{private String name,author;int sale;public Book(){}public Book(String name,String author,int sale){this.name=name;this.author=author;this.sale=sale;}public void setBook(String name,String author,int sale){this.name=name;this.author=author;this.sale=sale;}public void printBook(){System.out.println("書名"+name+" 作者"+author+" 月銷量"+sale);}
}// 有參和無參的構造函數
/**
public Book(){}
public Book(String name,String author,int sale){this.name=name;this.author=author;this.sale=sale;
}
*/
/**
18. 請創建一個銀行帳戶類,要求如下:(1)類包括帳戶名、帳戶號、存款額等屬性;(2)可實現余額查詢,存款和取款的操作。(3)創建該類的對象,驗證以上兩項。
*/public class test1{public static void main(String[] args){Account a=new Account("abc","123",100);a.show();a.add(100);a.show();a.qu(150);a.show();a.qu(100);a.show();}
}class Account{String name,id;double money;public Account(String name,String id,double money){this.name=name;this.id=id;this.money=money;}public void show(){System.out.println("余額為"+money);}public void add(double x){money=money+x;}public void qu(double x){if(money<x){System.out.println("余額不足,操作失敗");}else money=money-x;}
}//注意取款時要判斷余額是否充足
/**
19. 在biology包中的animal包中有human類,它具有name,height,weight的屬性,還具有eat(),sleep()和work()的行為,在biology包中的plant包中有flower類,它具有name,color,smell的屬性,還具有drink()和blossom()的行為.現在在一個school包中的garden包中一個張三的人,他是一個human類的對象,種植的rose是一個flower類對象,編程實現并測試各自的方法.
*/package school.garden;import biology.animal.human;
import biology.plant.flower;public class test {public static void main(String[] args){human h=new human("張三",175,55);flower f=new flower("rose","res","wonderful");h.eat();h.sleep();h.work();f.drink();f.blossom();}
}package biology.animal;public class human {private String name;double height,weight;public human(String name,double height,double weight){this.name=name;this.height=height;this.weight=weight;}public void eat(){System.out.println("eat");}public void sleep(){System.out.println("sleep");}public void work(){System.out.println("work");}
}package biology.plant;public class flower {private String name,color,smell;public flower(String name,String color,String smell){this.name=name;this.color=color;this.smell=smell;}public void drink(){System.out.println("drink");}public void blossom(){System.out.println("blossom");}
}
/**
20.設計一個接口circleInterface,要求接口中有一個定義PI的常量以及一個計算圓面積的空方法circleArea()。然后設計一個類circleClass實現該接口,通過構造函數circleClass(double r)定義圓半徑,并增加一個顯示圓面積的方法。最后,通過上述類生成兩個半徑分別為3.5、5.0的圓對象circle1、circle2進行測試。
*/package cn.itcase.kaoshi;//public class test1 {
// public static void main(String[] args){
// int[][] k=new int[6][5];
// for(int i=1;i<=5;i++){
// for(int j=1;j<=4;j++){
// k[i][j]=i*j;
// }
// }
// System.out.println(k[2][3]);
// }
//}//public class test1{
// public static void main(String[] args){
// int n=args.length;
// System.out.println("字符的個數是"+n);
// for(int i=0;i<n;i++){
// System.out.println("第"+(i+1)+"個字符串的長度為"+args[i].length());
// }
// }
//}//import java.util.Scanner;
//public class test1{
// public static void main(String[] args){
// Scanner input=new Scanner(System.in);
// int n=input.nextInt();
// for(int i=1;i<=n;i++){
// for(int j=1;j<=n-i;j++){
// System.out.print(" ");
// }
// for(int j=1;j<=2*i-1;j++){
// System.out.print("*");
// }
// System.out.println();
// }
// }
//}//import java.util.Scanner;
//public class test1{
// public static void main(String[] args){
// Scanner input=new Scanner(System.in);
// int n=input.nextInt();
// for(int i=1;i<=n;i++){
// for(int j=1;j<=n-i;j++){
// System.out.print(" ");
// }
// for(int j=1;j<=i;j++){
// System.out.print("*");
// }
// System.out.println();
// }
// }
//}///777777777777777777777777777777777777
//public class test1{
// public static void main(String[] args){
// Undergraduate s1=new Undergraduate("張三",20,"本科","通信專業");
// Undergraduate s2=new Undergraduate("李四",21,"本科","電子專業");
// Graduate s3=new Graduate("王五",25,"碩士","無線通信");
// Graduate s4=new Graduate("劉六",32,"博士后","光纖通信");
// s1.show();
// s2.show();
// s3.show();
// s4.show();
// }
//}//abstract class Student {
// String name,degree;
// int age;
// public Student(String name,int age,String degree){
// this.name=name;
// this.age=age;
// this.degree=degree;
// }
// public abstract void show();
//}
//
//class Undergraduate extends Student{
// private String specialty;
// public Undergraduate(String name,int age,String degree,String specialty){
// super(name,age,degree);
// this.specialty=specialty;
// }
// public void show(){
// System.out.println(name+":"+age+","+degree+","+specialty);
// }
//}
//
//class Graduate extends Student{
// private String direction;
// public Graduate(String name,int age,String degree,String direction){
// super(name,age,degree);
// this.direction=direction;
// }
// public void show(){
// System.out.println(name+":"+age+","+degree+","+direction);
// }
//}///8888888888888888888888888888888
//public class test1{
// public static void main(String[] args){
// Square s1=new Square(2);
// Circle s2=new Circle(3);
// System.out.println(s1.area(2));
// System.out.println(s2.area(3));
// }
//}
//
//interface Shape{
// double area(double x);
//}
//
//class Square implements Shape{
// double bc;
// public Square(double bc){
// this.bc=bc;
// }
// public double area(double x){
// return x*x;
// }
//}
//
//class Circle implements Shape{
// double r;
// public Circle(double r){
// this.r=r;
// }
// public double area(double x){
// return Math.PI*x*x;
// }
//}//99999999999999999999999
//public class test1{
// public static void main(String[] args){
// RectangleClass r1=new RectangleClass(7.8,4.5);
// RectangleClass r2=new RectangleClass(10.1,2.8);
// r1.rectArea();
// r1.rectLen();
// r2.rectArea();
// r2.rectLen();
// }
//}
//
//interface RectangleInterface{
// public void rectArea();
// public void rectLen();
//}
//
//class RectangleClass implements RectangleInterface{
// double x,y;
// public RectangleClass(double x,double y){
// this.x=x;
// this.y=y;
// }
// public void rectArea(){
// System.out.println(x*y);
// }
// public void rectLen(){
// System.out.println(2*(x+y));
// }
//}10 10 10 10 10 10 10 10 10 10 10 10 10
//import java.util.*;
//public class test1{
// public static void main(String[] args){
// String a="23 10 -8 0 3 7 108";
// String[] b=a.split(" ");
// int[] c=new int[b.length];
// for(int i=0;i<b.length;i++){
// c[i]=Integer.parseInt(b[i]);
// }
// Arrays.sort(c);
// String ans="";
// ans=ans+c[0];
// for(int i=1;i<b.length;i++){
// ans=ans+" "+c[i];
// }
// System.out.println(ans);
// }
//}//111111111111111111111111
//public class test1{
// public static void main(String[] args){
// String s1="nba";
// String s2="asfasfnabaasdfnbasdnbasnbasdnbadfasdf";
// int cnt=0;
// for(int i=0;i<s2.length()-s1.length();i++){
// if(s2.substring(i,i+s1.length()).equals(s1)){
// System.out.print(i+1+" ");
// cnt++;
// }
// }
// System.out.println("\n共出現了"+cnt+"次");
// }
//}//12 12 12 12 12 12 12 12 12 12 12 12 12 12
//public class test1{
// public static void main(String[] args){
// int n=Integer.parseInt(args[0]);
// if(n%2==0) System.out.println(n+"是偶數");
// else System.out.println(n+"是奇數");
// }
//}//13 13 13 13 13 13 13 13 13 13 13 13 13 13
//public class test1{
// public static void main(String[] args){
// int n=Integer.parseInt(args[0]);
// int m=Integer.parseInt(args[1]);
// if(n>m) System.out.println(n+">"+m);
// else if(n<m) System.out.println(n+"<"+m);
// else System.out.println(n+"="+m);
// }
//}14 14 14 14 14 14 14 14 14 14 14 14 14 14
//public class test1{
// public static void main(String args[]){
// int[][] a=new int[11][11];
// for(int i=1;i<=10;i++){
// a[i][1]=1;
// a[i][i]=1;
// }
// for(int i=3;i<=10;i++){
// for(int j=2;j<i;j++){
// a[i][j]=a[i-1][j-1]+a[i-1][j];
// }
// }
// for(int i=1;i<=10;i++){
// for(int j=1;j<=i;j++){
// if(j!=1) System.out.print(" ");
// System.out.print(a[i][j]);
// }
// System.out.println();
// }
// }
//}15 15 15 15 15 15 15 15 15 15 15 15 15 15 15
//import java.util.Scanner;
//public class test1{
// public static void main(String[] args){
// int num=(int)(Math.random()*10);
// Scanner input=new Scanner(System.in);
// int n=input.nextInt();
// while(num!=n){
// if(num>n) System.out.println("sorry,您猜小了!");
// else if(num<n) System.out.println("sorry,您猜大了!");
// n=input.nextInt();
// }
// System.out.println("恭喜您,答對了!");
// }
//}/17 17 17 17 17 17 17 17 17 17 17 17
//public class test1{
// public static void main(String[] args){
// Book b=new Book("西游記","吳承恩",200);
// b.printBook();
// }
//}
//
//class Book{
// private String name,author;
// int sale;
// public Book(){}
// public Book(String name,String author,int sale){
// this.name=name;
// this.author=author;
// this.sale=sale;
// }
// public void setBook(String name,String author,int sale){
// this.name=name;
// this.author=author;
// this.sale=sale;
// }
// public void printBook(){
// System.out.println("書名"+name+" 作者"+author+" 月銷量"+sale);
// }
//}/18 18 18 18 18 18 18 18 18 18 18 18
//public class test1{
// public static void main(String[] args){
// Account a=new Account("abc","123",100);
// a.show();
// a.add(100);
// a.show();
// a.qu(150);
// a.show();
// a.qu(100);
// a.show();
// }
//}
//
//class Account{
// String name,id;
// double money;
// public Account(String name,String id,double money){
// this.name=name;
// this.id=id;
// this.money=money;
// }
// public void show(){
// System.out.println("余額為"+money);
// }
// public void add(double x){
// money=money+x;
// }
// public void qu(double x){
// if(money<x){
// System.out.println("余額不足,操作失敗");
// }
// else money=money-x;
// }
//}///20 20 20 20 20 20 20 20 20
public class test1{public static void main(String[] args){circleClass c1=new circleClass(3.5);circleClass c2=new circleClass(5.0);c1.show();c2.show();}
}interface circleInterface{static final double PI=Math.acos(-1.0);double circleArea();
}class circleClass implements circleInterface{private double r;public circleClass(double r){this.r=r;}public double circleArea() {return PI*r*r;}public void show(){System.out.println("圓的面積為"+circleArea());}
}//注意計算PI static final double PI=Math.acos(-1.0);
/**
維護有序的圖書列表(注意學習并使用字符串的比較方法)
*/package cn.itcase.chapter01;import java.util.Scanner;public class Main {public static void main(String[] args) {Scanner input = new Scanner(System.in);String[] books= new String[]{"Computer","Hibernate","Java","Struts"};String[] newbook= new String[books.length+1];System.out.println("插入前的數組為:Computer Hibernate Java Struts");System.out.print("請輸入新書名稱:");String book=input.next();int index=0;for(int i=0;i<books.length;i++){newbook[i]=books[i];}for(int i=0;i<books.length;i++){if(books[i].compareToIgnoreCase(book)>0){index=i;break;}}for(int i=newbook.length-1;i>index;i--){newbook[i]=newbook[i-1];}newbook[index]=book;System.out.print("插入后的數組為:");for(int i=0;i<newbook.length;i++){if(i!=0){System.out.print(" ");}System.out.print(newbook[i]);}System.out.println("\n");}
}//字符串比較 books[i].compareToIgnoreCase(book)>0
//字符串比較 compareTo( ) :不忽略大小寫;
//compareToIgnoreCase( ):忽略大小寫
/**
1、角谷猜想:任何一個正整數n,如果它是偶數則除以2,如果是奇數則乘3加1,這樣得到一個新的整數,如此繼續進行上述處理,則最后得到的數一定是1。編寫應用程序證明:在3~10000之間的所有正整數都符合上述規則。
*/package cn.itcase.chapter01;import java.util.Scanner;public class Main {public static void main(String[] args) {Scanner input = new Scanner(System.in);int cnt=0;for(int i=3;i<=10000;i++){int x=i;while(x!=1){if(x%2==0){x=x/2;}else{x=x*3+1;}}cnt++;}System.out.println("3-10000之間的所有正整數(共9998個)中有" + cnt + "個數符合角谷猜想");}
}
/**
2、編寫一個模擬同時擲骰子的程序。要用Math.random()模擬產生兩個骰子,將兩個結果相加,相加的和等于7的可能性最大,等于2和12的可能性最小。程序模投擲3600次,判斷求和的結果是否合理。
*/package cn.itcase.chapter01;import java.util.Scanner;public class Main {public static void main(String[] args) {Scanner input = new Scanner(System.in);int[] a = new int[13];for(int i=1;i<=3600;i++){int num1=(int)(Math.random()*6+1);int num2=(int)(Math.random()*6+1);int sum=num1+num2;a[sum]++;} for(int i=2;i<=12;i++){System.out.println("3600次中和為"+i+"的個數為"+a[i]);}}
}
/**
1. 定義一個復數類complex,它的內部具有兩個實例變量:realPart和imagPart,分別代表復數的實部和虛部,編程實現要求的數學運算:1)實現兩個復數相加;2)實現兩個復數相減;3)輸出運算的結果。然后,調用上述方法實現兩個復數18+2i、19-13i的相加、相減,并打印出結果。
*/package cn.itcase.kaoshi;//public class test1 {
// public static void main(String[] args){
// int[][] k=new int[6][5];
// for(int i=1;i<=5;i++){
// for(int j=1;j<=4;j++){
// k[i][j]=i*j;
// }
// }
// System.out.println(k[2][3]);
// }
//}//public class test1{
// public static void main(String[] args){
// int n=args.length;
// System.out.println("字符的個數是"+n);
// for(int i=0;i<n;i++){
// System.out.println("第"+(i+1)+"個字符串的長度為"+args[i].length());
// }
// }
//}//import java.util.Scanner;
//public class test1{
// public static void main(String[] args){
// Scanner input=new Scanner(System.in);
// int n=input.nextInt();
// for(int i=1;i<=n;i++){
// for(int j=1;j<=n-i;j++){
// System.out.print(" ");
// }
// for(int j=1;j<=2*i-1;j++){
// System.out.print("*");
// }
// System.out.println();
// }
// }
//}//import java.util.Scanner;
//public class test1{
// public static void main(String[] args){
// Scanner input=new Scanner(System.in);
// int n=input.nextInt();
// for(int i=1;i<=n;i++){
// for(int j=1;j<=n-i;j++){
// System.out.print(" ");
// }
// for(int j=1;j<=i;j++){
// System.out.print("*");
// }
// System.out.println();
// }
// }
//}///777777777777777777777777777777777777
//public class test1{
// public static void main(String[] args){
// Undergraduate s1=new Undergraduate("張三",20,"本科","通信專業");
// Undergraduate s2=new Undergraduate("李四",21,"本科","電子專業");
// Graduate s3=new Graduate("王五",25,"碩士","無線通信");
// Graduate s4=new Graduate("劉六",32,"博士后","光纖通信");
// s1.show();
// s2.show();
// s3.show();
// s4.show();
// }
//}//abstract class Student {
// String name,degree;
// int age;
// public Student(String name,int age,String degree){
// this.name=name;
// this.age=age;
// this.degree=degree;
// }
// public abstract void show();
//}
//
//class Undergraduate extends Student{
// private String specialty;
// public Undergraduate(String name,int age,String degree,String specialty){
// super(name,age,degree);
// this.specialty=specialty;
// }
// public void show(){
// System.out.println(name+":"+age+","+degree+","+specialty);
// }
//}
//
//class Graduate extends Student{
// private String direction;
// public Graduate(String name,int age,String degree,String direction){
// super(name,age,degree);
// this.direction=direction;
// }
// public void show(){
// System.out.println(name+":"+age+","+degree+","+direction);
// }
//}///8888888888888888888888888888888
//public class test1{
// public static void main(String[] args){
// Square s1=new Square(2);
// Circle s2=new Circle(3);
// System.out.println(s1.area(2));
// System.out.println(s2.area(3));
// }
//}
//
//interface Shape{
// double area(double x);
//}
//
//class Square implements Shape{
// double bc;
// public Square(double bc){
// this.bc=bc;
// }
// public double area(double x){
// return x*x;
// }
//}
//
//class Circle implements Shape{
// double r;
// public Circle(double r){
// this.r=r;
// }
// public double area(double x){
// return Math.PI*x*x;
// }
//}//99999999999999999999999
//public class test1{
// public static void main(String[] args){
// RectangleClass r1=new RectangleClass(7.8,4.5);
// RectangleClass r2=new RectangleClass(10.1,2.8);
// r1.rectArea();
// r1.rectLen();
// r2.rectArea();
// r2.rectLen();
// }
//}
//
//interface RectangleInterface{
// public void rectArea();
// public void rectLen();
//}
//
//class RectangleClass implements RectangleInterface{
// double x,y;
// public RectangleClass(double x,double y){
// this.x=x;
// this.y=y;
// }
// public void rectArea(){
// System.out.println(x*y);
// }
// public void rectLen(){
// System.out.println(2*(x+y));
// }
//}10 10 10 10 10 10 10 10 10 10 10 10 10
//import java.util.*;
//public class test1{
// public static void main(String[] args){
// String a="23 10 -8 0 3 7 108";
// String[] b=a.split(" ");
// int[] c=new int[b.length];
// for(int i=0;i<b.length;i++){
// c[i]=Integer.parseInt(b[i]);
// }
// Arrays.sort(c);
// String ans="";
// ans=ans+c[0];
// for(int i=1;i<b.length;i++){
// ans=ans+" "+c[i];
// }
// System.out.println(ans);
// }
//}//111111111111111111111111
//public class test1{
// public static void main(String[] args){
// String s1="nba";
// String s2="asfasfnabaasdfnbasdnbasnbasdnbadfasdf";
// int cnt=0;
// for(int i=0;i<s2.length()-s1.length();i++){
// if(s2.substring(i,i+s1.length()).equals(s1)){
// System.out.print(i+1+" ");
// cnt++;
// }
// }
// System.out.println("\n共出現了"+cnt+"次");
// }
//}//12 12 12 12 12 12 12 12 12 12 12 12 12 12
//public class test1{
// public static void main(String[] args){
// int n=Integer.parseInt(args[0]);
// if(n%2==0) System.out.println(n+"是偶數");
// else System.out.println(n+"是奇數");
// }
//}//13 13 13 13 13 13 13 13 13 13 13 13 13 13
//public class test1{
// public static void main(String[] args){
// int n=Integer.parseInt(args[0]);
// int m=Integer.parseInt(args[1]);
// if(n>m) System.out.println(n+">"+m);
// else if(n<m) System.out.println(n+"<"+m);
// else System.out.println(n+"="+m);
// }
//}14 14 14 14 14 14 14 14 14 14 14 14 14 14
//public class test1{
// public static void main(String args[]){
// int[][] a=new int[11][11];
// for(int i=1;i<=10;i++){
// a[i][1]=1;
// a[i][i]=1;
// }
// for(int i=3;i<=10;i++){
// for(int j=2;j<i;j++){
// a[i][j]=a[i-1][j-1]+a[i-1][j];
// }
// }
// for(int i=1;i<=10;i++){
// for(int j=1;j<=i;j++){
// if(j!=1) System.out.print(" ");
// System.out.print(a[i][j]);
// }
// System.out.println();
// }
// }
//}15 15 15 15 15 15 15 15 15 15 15 15 15 15 15
//import java.util.Scanner;
//public class test1{
// public static void main(String[] args){
// int num=(int)(Math.random()*10);
// Scanner input=new Scanner(System.in);
// int n=input.nextInt();
// while(num!=n){
// if(num>n) System.out.println("sorry,您猜小了!");
// else if(num<n) System.out.println("sorry,您猜大了!");
// n=input.nextInt();
// }
// System.out.println("恭喜您,答對了!");
// }
//}/17 17 17 17 17 17 17 17 17 17 17 17
//public class test1{
// public static void main(String[] args){
// Book b=new Book("西游記","吳承恩",200);
// b.printBook();
// }
//}
//
//class Book{
// private String name,author;
// int sale;
// public Book(){}
// public Book(String name,String author,int sale){
// this.name=name;
// this.author=author;
// this.sale=sale;
// }
// public void setBook(String name,String author,int sale){
// this.name=name;
// this.author=author;
// this.sale=sale;
// }
// public void printBook(){
// System.out.println("書名"+name+" 作者"+author+" 月銷量"+sale);
// }
//}/18 18 18 18 18 18 18 18 18 18 18 18
//public class test1{
// public static void main(String[] args){
// Account a=new Account("abc","123",100);
// a.show();
// a.add(100);
// a.show();
// a.qu(150);
// a.show();
// a.qu(100);
// a.show();
// }
//}
//
//class Account{
// String name,id;
// double money;
// public Account(String name,String id,double money){
// this.name=name;
// this.id=id;
// this.money=money;
// }
// public void show(){
// System.out.println("余額為"+money);
// }
// public void add(double x){
// money=money+x;
// }
// public void qu(double x){
// if(money<x){
// System.out.println("余額不足,操作失敗");
// }
// else money=money-x;
// }
//}///20 20 20 20 20 20 20 20 20
//public class test1{
// public static void main(String[] args){
// circleClass c1=new circleClass(3.5);
// circleClass c2=new circleClass(5.0);
// c1.show();
// c2.show();
// }
//}
//
//interface circleInterface{
// static final double PI=Math.acos(-1.0);
// double circleArea();
//}
//
//class circleClass implements circleInterface{
// private double r;
// public circleClass(double r){
// this.r=r;
// }
// public double circleArea() {
// return PI*r*r;
// }
// public void show(){
// System.out.println("圓的面積為"+circleArea());
// }
//}
public class test1{public static void main(String[] args){complex p1=new complex();complex p2=new complex();p1.setrealPart(18);p1.setimagPart(2);p2.setrealPart(19);p2.setimagPart(-13);complex resultplus=p1.add(p2);complex resultminus=p1.minus(p2);System.out.print("兩個復數相加的結果為:");resultplus.print();System.out.print("兩個復數相減的結果為:");resultminus.print();}
}class complex{private int realPart;private int imagPart;public int getrealPart(){return realPart;}public void setrealPart(int realPart){this.realPart=realPart;}public int getimagPart(){return imagPart;}public void setimagPart(int imagPart){this.imagPart=imagPart;}public complex add(complex c){int real=this.realPart+c.realPart;int imag=this.imagPart+c.imagPart;complex result=new complex();result.setrealPart(real);result.setimagPart(imag);return result;}public complex minus(complex c){int real=this.realPart-c.realPart;int imag=this.imagPart-c.imagPart;complex result=new complex();result.setrealPart(real);result.setimagPart(imag);return result;}public void print(){if(imagPart>0){System.out.println(realPart+"+"+imagPart+"i");}else if(imagPart<0){System.out.println(realPart+"-"+(-imagPart)+"i");}else{System.out.println(realPart);}}
}
/**
首先定義一個計算長方形面積的類rectangleClass,要求類中有一個定義長方形左上角和右下角座標的構造函數,以及一個通過長方形右下角座標與左上角座標計算長方形面積,并實例化兩個長方形進行測試.
*/import java.util.Scanner;
public class test1{public static void main(String[] args){Scanner input = new Scanner(System.in);System.out.println("請輸入長方形左上角坐標");double x1=input.nextDouble();double y1=input.nextDouble();System.out.println("請輸入長方形右下角坐標");double x2=input.nextDouble();double y2=input.nextDouble();rectangleClass r1 =new rectangleClass(x1,y1,x2,y2);r1.area();}
}class rectangleClass{private double x1,x2,y1,y2;public rectangleClass(double x1,double y1,double x2,double y2){this.x1=x1;this.y1=y1;this.x2=x2;this.y2=y2;}public void area(){System.out.println("以("+x1+","+y1+")為左上角頂點坐標,以("+x2+","+y2+")在右下角頂點坐標的長方形的面積為:"+(x2-x1)*(y1-y2));}
}
/**
設計一個學生類Student,其數據成員有name(姓名)、age(年齡)和degree(學位)。由Student類派生出本科生類Undergraduate和研究生類Graduate,本科生類Undergraduate增加成員specialty(專業),研究生類增加成員direction(研究方向)。每個類都有show()方法,用于輸出數據成員信息。最后請輸出下列信息:
*/package cn.itcase.chapter08;public class Student {private String name;private int age;private String degree;public Student(String name, int age, String degree) {super();this.name = name;this.age = age;this.degree = degree;}public Student(){super();}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 String getDegree() {return degree;}public void setDegree(String degree) {this.degree = degree;}public void show(){System.out.print("姓名:"+this.name+"\t年齡:"+this.age+"\t學位:"+this.degree);}
}
package cn.itcase.chapter08;class Undergraduate extends Student{private String specialty;public String getSpecialty() {return specialty;}public void setSpecialty(String specialty) {this.specialty = specialty;}public Undergraduate(String name, int age, String degree, String specialty) {super(name, age, degree);this.specialty = specialty;}public void show(){super.show();System.out.println("\t專業:"+this.specialty);}}
package cn.itcase.chapter08;class Graduate extends Student{private String direction;public String getDirection() {return direction;}public void setDirection(String direction) {this.direction = direction;}public Graduate(String name, int age, String degree, String direction) {super(name, age, degree);this.direction = direction;}public void show(){super.show();System.out.println("\t研究方向:"+this.direction);}
}
package cn.itcase.chapter08;public class test {public static void main(String[] ages){Undergraduate stu1=new Undergraduate("張三",20,"本科","通信");Undergraduate stu2=new Undergraduate("李四",21,"本科","電子");Graduate stu3=new Graduate("王五",25,"碩士","通信");Graduate stu4=new Graduate("劉六",36,"博士","通信");stu1.show();stu2.show();stu3.show();stu4.show();}
}
/**
編程實現有一個電話類Phone,它有號碼的屬性number,是一個12位的字符數組,它有四個功能,設置電話號碼setNumber(),顯示電話號碼getNumber(),接電話answer(),撥打電話dial();移動電話mobilePhone和固定電話fixPhone是電話的兩個子類, 但移動電話號碼為11位, 并且移動電話和固定電話接聽和撥打電話的方式不同.固定電話又有一個子類:無繩電話cordlessPhone,無繩電話號碼為4位,它相對固定電話還多一個移動功能move().實現這幾個類,并且測試它們的功能.
*/package cn.itcase.chapter09;public class Phone {char[] number=new char[12];public char[] getNumber() {System.out.print("本機號碼是:");for(int i=0;i<number.length;i++){System.out.print(number[i]);}System.out.println();return number;}public void setNumber(char[] number) {this.number = number;}
}
package cn.itcase.chapter09;class mobilePhone extends Phone{public void answer(){System.out.println("正通過移動網絡接聽電話....");}public void dail(){System.out.println("正通過電信固網撥打電話....");}
}
package cn.itcase.chapter09;class fixPhone extends Phone{public void answer(){System.out.println("正通過電信固網接聽電話....");}public void dail(){System.out.println("正通過電信固網撥打電話....");}
}
package cn.itcase.chapter09;class cordlessPhone extends fixPhone{public void move(){System.out.println("正在移動通話....");}
}
package cn.itcase.chapter09;public class test {public static void main(String[] args){fixPhone fp=new fixPhone();mobilePhone mp=new mobilePhone();cordlessPhone cp=new cordlessPhone();fp.setNumber(new char[]{'0','5','7','4','8','8','2','2','2','0','9','6'});fp.getNumber();fp.dail();fp.answer();mp.setNumber(new char[]{'1','3','7','8','8','8','8','6','6','6','6'});mp.getNumber();mp.dail();mp.answer();cp.setNumber(new char[]{'2','0','9','6'});cp.getNumber();cp.dail();cp.answer();cp.move();}
}
/**
首先設計一個學生抽象類Student,其數據成員有name(姓名)、age(年齡)和degree(學位),以及一個抽象方法show()。然后由Student類派生出本科生類Undergraduate和研究生類Graduate,本科生類Undergraduate增加成員specialty(專業),研究生類增加成員direction(研究方向)。并且每個類都有show()方法,用于輸出數據成員信息。請定義對象,并打印輸出下列信息:
*/package cn.itcase.chapter08;
public abstract class Student {private String name;private int age;private String degree;public Student(String name, int age, String degree) {super();this.name = name;this.age = age;this.degree = degree;}public Student(){super();}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 String getDegree() {return degree;}public void setDegree(String degree) {this.degree = degree;}public void show1(){System.out.print("姓名:"+this.name+"\t年齡:"+this.age+"\t學位:"+this.degree);}public abstract void show();
}
package cn.itcase.chapter08;class Undergraduate extends Student{private String specialty;public String getSpecialty() {return specialty;}public void setSpecialty(String specialty) {this.specialty = specialty;}public Undergraduate(String name, int age, String degree, String specialty) {super(name, age, degree);this.specialty = specialty;}public void show(){super.show1();System.out.println("\t專業:"+this.specialty);}
}
package cn.itcase.chapter08;class Graduate extends Student{private String direction;public String getDirection() {return direction;}public void setDirection(String direction) {this.direction = direction;}public Graduate(String name, int age, String degree, String direction) {super(name, age, degree);this.direction = direction;}public void show(){super.show1();System.out.println("\t研究方向:"+this.direction);}
}
package cn.itcase.chapter08;public class test {public static void main(String[] ages){Student stu1=new Undergraduate("張三",20,"本科","計算機科學");Student stu2=new Undergraduate("李四",21,"本科","物聯網");Student stu3=new Graduate("王五",25,"碩士","軟件工程");Student stu4=new Graduate("劉六",36,"博士","通信工程");stu1.show();stu2.show();stu3.show();stu4.show();}
}
/**
1、設計一個接口circle Interface,要求接口中有一個定義PI的常量以及一個計算圓面積的空方法circleArea()。然后設計一個類circleClass實現該接口,通過構造函數circleClass(double r)定義圓半徑,并增加一個顯示圓面積的方法。最后,通過上述類生成兩個半徑分別為3.5、5.0的圓對象circle1、circle2進行測試。
*/package cn.itcase.chapter15;public interface Circlelnterface {final double PI=3.14;public double circleArea(double r);
}
package cn.itcase.chapter15;public class CircleClass implements Circlelnterface{double r;public CircleClass(double r) {super();this.r = r;}public double circleArea(double r) {return (double)(PI*r*r);}public void printcircle(){System.out.println("半徑為"+r+"的圓面積為:"+circleArea(r));}
}
package cn.itcase.chapter15;public class test {public static void main(String[] args){CircleClass c1=new CircleClass(3.5);CircleClass c2=new CircleClass(5.0);c1.printcircle();c2.printcircle();}
}
/**
2. 設計一個Shape接口和它的兩個實現類Square和Circle,要求如下:1)Shape接口中有一個抽象方法area(),方法接收一個double類型的參數,返回一個double類型的結果。2)Square和Circle中實現了Shape接口的area()抽象方法,分別求正方形和圓形的面積并返回。在測試類中創建Square和Circle對象,計算邊長為2的正方形面積和半徑為3的園面積。
*/package cn.itcase.chapter16;public interface Shape {double area(double l);
}
package cn.itcase.chapter16;public class Square implements Shape{public double area(double l){return l*l;}
}
package cn.itcase.chapter16;public class Circle {public double area(double r){return Math.PI*r*r;}
}
package cn.itcase.chapter16;public class test {public static void main(String[] args){Shape square= new Square();Circle circle= new Circle();System.out.println("邊長為2的正方形的面積為:"+square.area(2));System.out.println("半徑為3的圓的面積為"+circle.area(3));}
}
/**
2. 在biology包中的animal包中有human類,它具有name,height,weight的屬性,還具有eat(),sleep()和work()的行為,在biology包中的plant包中有flower類,它具有name,color,smell的屬性,還具有drink()和blossom()的行為.現在在一個school包中的garden包中一個張三的人,他是一個human類的對象,種植的rose是一個flower類對象,編程實現并測試各自的方法.
*/package human;public class Human {String name;double height,weight;public Human(String name) {super();this.name = name;}public void eat(){System.out.println(name+"正在吃");}public void sleep(){System.out.println(name+"正在睡覺");}public void work(){System.out.println(name+"正在工作");}
}
package plant;public class Flower {String name,color,smell;public Flower(String name) {super();this.name = name;}public void drink(){System.out.println(name+"正在吸收水分");}public void blossom(){System.out.println(name+"正在開花");}
}
package school;import human.Human;
import plant.Flower;public class Garden {public static void main(String[] args){Human human=new Human("張三");Flower flower=new Flower("rose");human.eat();human.sleep();human.work();flower.drink();flower.blossom();}
}
/**
汽車總租金
*/
package cn.itcase.chapter10;public abstract class MotoVehicle {private String No;private String Brand;public MotoVehicle(){}public MotoVehicle(String no, String brand) {super();No = no;Brand = brand;}public MotoVehicle(String no) {super();No = no;}public String getNo() {return No;}public void setNo(String no) {No = no;}public String getBrand() {return Brand;}public void setBrand(String brand) {Brand = brand;}public abstract int calcRent(int day);
}
package cn.itcase.chapter10;public class Bus extends MotoVehicle{private int seatcount;public Bus(){} public Bus(String No,String Brand,int seatcount) {super(No,Brand);this.seatcount = seatcount;}public int getSeatcount() {return seatcount;}public void setSeatcount(int seatcount) {this.seatcount = seatcount;}public int calcRent(int day){if(seatcount>16){return 1500*day;}else{return 800*day;}}
}
package cn.itcase.chapter10;public class Car extends MotoVehicle{private String Brand;public Car(){}public Car(String No,String Brand) {super(No);this.Brand=Brand;}public String getBrand() {return Brand;}public void setBrand(String brand) {Brand = brand;}public int calcRent(int day){if(Brand.equals("別克商務艙GL8")){return 600*day;}else if(Brand.equals("寶馬550i")){return 500*day;}else if(Brand.equals("別克林蔭大道")){return 300*day;}else{System.out.println("輸入錯誤");return 0;}}
}
package cn.itcase.chapter10;public class Customer {private String name;public Customer(String name) {super();this.name = name;}public static double CalcTotalRent(MotoVehicle[] motos){double totalRent = 0; for(int i=0;i<motos.length;i++){totalRent += motos[i].calcRent(5);} return totalRent;}
}
package cn.itcase.chapter10;import java.util.Scanner;public class test {public static void main(String[] args) {MotoVehicle[] motos=new MotoVehicle[4];motos[0] = new Car("京NY28588","寶馬550i");motos[1] = new Car("京NNN3288","寶馬550i");motos[2] = new Car("京NY28588","別克林蔭大道");motos[3] = new Bus("京NY28589","金龍",34);Customer cust=new Customer("AAA");double total=cust.CalcTotalRent(motos);System.out.println("客戶名為AAA的某客戶"+"租用以下車輛:");for(int i=0;i<4;i++){System.out.println(motos[i].getNo()+ "\t" +motos[i].getBrand());}System.out.println("總租金為:"+total);}
}
/**
調色板
*/package cn.itcase.chapter0527;import java.awt.*;
import java.awt.event.*;
import javax.swing.JFrame;public class test1{public static void main(String[] args){TiaoSe tiaose=new TiaoSe();}
}
class TiaoSe extends Frame{Label l1,l2,l3;Button b1,b2,b3,b4,b5,b6;TextField tf1,tf2,tf3;int count1,count2,count3;Color c;private class changeText implements ActionListener,TextListener{public void actionPerformed(ActionEvent e){if(e.getSource()==b1) tf1.setText((count1++)+"");if(e.getSource()==b2) tf2.setText((count2++)+"");if(e.getSource()==b3) tf3.setText((count3++)+"");if(e.getSource()==b4) tf1.setText((count1--)+"");if(e.getSource()==b5) tf2.setText((count2--)+"");if(e.getSource()==b6) tf3.setText((count3--)+"");int c1=Integer.parseInt(tf1.getText());int c2=Integer.parseInt(tf2.getText());int c3=Integer.parseInt(tf3.getText());if(c1>=255){c1=0;count1=0;}if(c2>=255){c2=0;count2=0;}if(c3>=255){c3=0;count3=0;}if(c1==-1){c1=0;count1=0;}if(c2==-1){c2=0;count2=0;}if(c3==-1){c3=0;count3=0;}c=new Color(c1,c2,c3);p1.setBackground(c);}public void textValueChanged(TextEvent e) {c=new Color(Integer.parseInt(tf1.getText()),Integer.parseInt(tf2.getText()),Integer.parseInt(tf3.getText()));p1.setBackground(c);}}Panel p=new Panel();Panel p1=new Panel();GridBagLayout gbl=new GridBagLayout();GridBagConstraints gbc=new GridBagConstraints();public TiaoSe(){setSize(500,400);p.setLayout(gbl);gbc.fill=GridBagConstraints.BOTH;this.setLabel();this.setButton_add();this.setTextField();this.setButton_sub();this.setPanel();add(p);setVisible(true);this.addWindowFocusListener(new WindowAdapter(){public void windowClosing(WindowEvent e){System.exit(0);}});}private void setPanel(){gbc.weightx=10;gbc.weighty=10;gbc.gridx=0;gbc.gridy=4;gbc.gridwidth=4;gbc.insets=new Insets(10,10,10,10);p1.setBackground(new Color(220,220,220));gbl.setConstraints(p1, gbc);p.add(p1);}private void setButton_sub(){b4=new Button("-");b5=new Button("-");b6=new Button("-");b4.setFont(new Font("宋體",Font.PLAIN,20));b5.setFont(new Font("宋體",Font.PLAIN,20));b6.setFont(new Font("宋體",Font.PLAIN,20));gbc.gridx=3;gbc.gridy=0;gbl.setConstraints(b4, gbc);gbc.gridy=1;gbl.setConstraints(b5, gbc);gbc.gridy=2;gbl.setConstraints(b6, gbc);p.add(b4);p.add(b5);p.add(b6);b4.addActionListener(new changeText());b5.addActionListener(new changeText());b6.addActionListener(new changeText());}private void setTextField(){tf1=new TextField(5);
tf1.setText("220");
count1=Integer.parseInt(tf1.getText());tf2=new TextField(5);tf2.setText("220");
count2=Integer.parseInt(tf2.getText());
tf3=new TextField(5);tf3.setText("220");count3=Integer.parseInt(tf3.getText());gbc.weightx=1;gbc.weighty=1;gbc.gridx=2;gbc.gridy=0;gbl.setConstraints(tf1, gbc);gbc.gridy=1;gbl.setConstraints(tf2, gbc);gbc.gridy=2;gbl.setConstraints(tf3, gbc);p.add(tf1);p.add(tf2);p.add(tf3);tf1.addTextListener(new changeText());tf2.addTextListener(new changeText());tf3.addTextListener(new changeText());this.setKeyPress(tf1);this.setKeyPress(tf2);this.setKeyPress(tf3);}private void setKeyPress(TextField tf){tf.addKeyListener(new KeyAdapter(){ public void keyPressed(KeyEvent e){char ch = e.getKeyChar(); if(!(ch>='0'&&ch<='9')){e.consume();}}});}private void setButton_add(){ b1=new Button("+");b2=new Button("+");b3=new Button("+");gbc.insets=new Insets(10,80,10,10);gbc.gridx=1;gbc.gridy=0;gbc.weightx=1;gbc.weighty=1;gbl.setConstraints(b1, gbc);gbc.gridy=1;gbl.setConstraints(b2, gbc);gbc.gridy=2;gbl.setConstraints(b3, gbc);p.add(b1);p.add(b2);p.add(b3);b1.addActionListener(new changeText());b2.addActionListener(new changeText());b3.addActionListener(new changeText());}private void setLabel(){ l1=new Label("紅色",Label.CENTER);l2=new Label("綠色",Label.CENTER);l3=new Label("藍色",Label.CENTER);l1.setBackground(Color.RED);l2.setBackground(Color.GREEN);l3.setBackground(Color.BLUE);gbc.gridx=0;gbc.gridy=0;gbc.weightx=1;gbc.weighty=1;gbc.insets=new Insets(10,10,10,10);gbl.setConstraints(l1, gbc);gbc.gridy=1;gbl.setConstraints(l2, gbc);gbc.gridy=2;gbl.setConstraints(l3, gbc);p.add(l1);p.add(l2);p.add(l3);}
}
package cn.itcase.chapter0527;import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.JButton;
import javax.swing.JFrame;class Chuangkou extends JFrame implements ActionListener{JButton b1=new JButton("紅色");JButton b2=new JButton("綠色");JButton b3=new JButton("藍色");int count1=0;int count2=0;int count3=0;Container con = this.getContentPane();public Chuangkou(){super("按鈕和框架");this.setSize(320,240);this.setLocation(220,160);this.setLayout(null);b1.setSize(80,40);b2.setSize(80,40);b3.setSize(80,40);b1.setLocation(20, 80);b2.setLocation(120,80);b3.setLocation(220,80);b1.setBackground(Color.red);b2.setBackground(Color.green);b3.setBackground(Color.blue);con.add(b1);con.add(b2);con.add(b3);b1.addActionListener(this);b2.addActionListener(this);b3.addActionListener(this);}@Overridepublic void actionPerformed(ActionEvent e) {// TODO Auto-generated method stubif(e.getSource()==b1){count1+=10;if(count1>255) count1=0;Color c=new Color(count1,count2,count3);con.setBackground(c);}if(e.getSource()==b2){count2+=10;if(count2>255) count2=0;Color c=new Color(count1,count2,count3);con.setBackground(c);}if(e.getSource()==b3){count3+=10;if(count3>255) count3=0;Color c=new Color(count1,count2,count3);con.setBackground(c);}}
}public class test2 {public static void main(String args[]){Chuangkou ck=new Chuangkou();ck.setVisible(true);}
}
總結
- 上一篇: 安卓apkcpu占用过高_Android
- 下一篇: 1044