代碼如下:
創(chuàng)新互聯(lián)建站主要從事網(wǎng)站設(shè)計(jì)制作、成都網(wǎng)站設(shè)計(jì)、網(wǎng)頁(yè)設(shè)計(jì)、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)環(huán)縣,十載網(wǎng)站建設(shè)經(jīng)驗(yàn),價(jià)格優(yōu)惠、服務(wù)專業(yè),歡迎來(lái)電咨詢建站服務(wù):13518219792
package?com.sxl.gd.test;
public?class?Array?{
private?int?[]a?=?{1,2,3,4,5,6};
private?int?x1,x2,x3,x4,x5,x6;
private?void?aver()?{
int?sum?=?0;
for(int?i=0;ia.length;i++)?{
sum+=a[i];
}
System.out.println("平均數(shù)是:"+(double)sum/(double)a.length);
}
public?Array()?{}
public?Array(int?x1,int?x2,int?x3,int?x4,int?x5,int?x6)?{
a[0]?=?x1;
a[1]?=?x2;
a[2]?=?x3;
a[3]?=?x4;
a[4]?=?x5;
a[5]?=?x6;
}
private?void?min()?{
int?min?=?a[0];
for(int?i=1;ia.length;i++)?{
if(a[i]min)?{
int?temp;
temp?=?a[i];
a[i]=min;
min?=?temp;
}
}
System.out.println("最小值是:"+min);
}
private?void?max()?{
int?max?=?a[0];
for(int?i=1;ia.length;i++)?{
if(a[i]max)?{
int?temp;
temp?=?a[i];
a[i]=max;
max?=?temp;
}
}
System.out.println("最大值是:"+max);
}
public?static?void?main(String[]?args)?{
Array?array1?=?new?Array();
array1.aver();
array1.max();
array1.min();
Array?array2?=?new?Array(6,2,8,4,1,7);
array2.aver();
array2.max();
array2.min();
}
}
運(yùn)行結(jié)果如下:
Java程序代碼:
public?class?test?{
public?static?void?main(String[]?args)?{
if(args.length?!=?2)?{
System.out.println("參數(shù)數(shù)量錯(cuò)誤!必須是2個(gè)參數(shù),以空格隔開(kāi)...");
return;
}
Rectangle?rect?=?new?Rectangle(Double.parseDouble(args[0]),?Double.parseDouble(args[1]));
System.out.println(rect.getArea()?+?"?"?+?rect.getPerimeter());
}
}
class?Rectangle?{
protected?double?height;
protected?double?width;
public?Rectangle(double?height,?double?width)?{
this.height?=?height;
this.width?=?width;
}
public?double?getArea()?{
return?height?*?width;
}
public?double?getPerimeter()?{
return?2?*?(height?+?width);
}
}
運(yùn)行測(cè)試:
第一題: 元素的復(fù)制
import?java.util.Arrays;
public?class?ArrayDemo?{
public?static?void?main(String[]?args)?{
int[]?scores?=?{91,85,98,62,78,93};
int[]?newScores=Arrays.copyOfRange(scores,?0,?5);//復(fù)制元素,?左開(kāi)右閉區(qū)間[0,5)
System.out.println(Arrays.toString(newScores));//調(diào)用數(shù)組工具類的方法轉(zhuǎn)成字符串并打印
}
}
第二題: 這題雖然使用集合更方便 , 但卻是非常好的一維數(shù)組的訓(xùn)練題目.
解法一: 集合解決 隨機(jī)產(chǎn)生7個(gè)不重復(fù)的數(shù)字很簡(jiǎn)單
import?java.util.HashSet;
import?java.util.Random;
public?class?NumberTest?{
public?static?void?main(String[]?args)?{
HashSetInteger?set=??new?HashSetInteger();//元素不可重復(fù)的無(wú)序集合
Random?rd=new?Random();//隨機(jī)產(chǎn)生器
while(set.size()7)?{
set.add(rd.nextInt(36)+1);//產(chǎn)生1~36的隨機(jī)數(shù)
//如果元素重復(fù),?那么添加不上去
}
System.out.println(set);
}
}
解法二:一維數(shù)組 ,解決產(chǎn)生7個(gè)數(shù)字, 并升序排列
int[] ? ? ?nums 數(shù)組存儲(chǔ)1~36個(gè)數(shù)組
boolean[] flags 數(shù)組存儲(chǔ)的是和nums數(shù)組一一對(duì)應(yīng)的true或者false,如果使用了就標(biāo)記為true.,如果沒(méi)有使用標(biāo)記為false,
例如 隨機(jī)產(chǎn)生了一個(gè)下標(biāo)0 ?,那么查看flags[0] ,如果是true, 那么說(shuō)明該元素已經(jīng)使用了,重新產(chǎn)生一個(gè)隨機(jī)數(shù), 如果是false ,那么表示nums[0]沒(méi)有被使用
具體代碼如下(稍微留個(gè)尾巴, 就是中不中的判斷, 可以把兩個(gè)數(shù)組都升序排序,然后元素一一比較,全部相同就是中了)
import?java.util.Arrays;
import?java.util.Random;
public?class?NumberDemo?{
public?static?void?main(String[]?args)?{
int[]?nums=?new?int[36];//長(zhǎng)度為36的數(shù)組?,默認(rèn)全是0
for?(int?i?=?0;?i??nums.length;?i++)?{//利用for循環(huán)賦值1~36
nums[i]=i+1;
}
boolean[]?flags=new?boolean[nums.length];//長(zhǎng)度和nums相同的數(shù)組,默認(rèn)值全是false?,表示全部沒(méi)有使用過(guò)
//用boolean值表示對(duì)應(yīng)的nums里的元素是否被使用
int[]?result=new?int[7];//存儲(chǔ)結(jié)果
Random?rd?=?new?Random();
for?(int?i?=?0;?i??result.length;?i++)?{
int?temp=rd.nextInt(nums.length);//隨機(jī)產(chǎn)生下標(biāo)
//System.out.println(Arrays.toString(result));
if(flags[temp])?{//如果已經(jīng)被使用,那么i-1,并在此循環(huán)
i--;
//System.out.println("號(hào)碼"+nums[temp]+"已經(jīng)存在.再次循環(huán)");
}else?{
result[i]=nums[temp];
flags[temp]=true;//標(biāo)記true表示已經(jīng)使用了
}
}
System.out.println("原始排序:"+Arrays.toString(result));
Arrays.sort(result);//升序排列
System.out.println("升序排列:"+Arrays.toString(result));
}
}
第一題:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
public class RadioTest extends JFrame{
private JRadioButton jrb1;
private JRadioButton jrb2;
private JLabel jlbl;
private JPanel jp;
private JButton jbtn;
private String jlstr;
private ButtonGroup bg;
public RadioTest(){
jlstr = "你選擇的是:";
this.setTitle("實(shí)現(xiàn)單選按鈕的效果");
jrb1 = new JRadioButton("男");
jrb2 = new JRadioButton("女");
bg = new ButtonGroup();
bg.add(jrb1);
bg.add(jrb2);
jlbl = new JLabel(jlstr);
jbtn = new JButton("退出");
jbtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
System.exit(1);
}
});
jrb1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==jrb1){
jlbl.setText(jlstr+jrb1.getText());
}
}
});
jrb2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==jrb2){
jlbl.setText(jlstr+jrb2.getText());
}
}
});
jp = new JPanel();
jp.add(jrb1);
jp.add(jrb2);
jp.add(jlbl);
jp.add(jbtn);
this.add(jp);
this.setBounds(300, 300, 230, 200);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
RadioTest rt = new RadioTest();
}
}