導航:首頁 > 編程語言 > java實例代碼

java實例代碼

發布時間:2021-12-04 02:39:11

java代碼示例

importjava.awt.*;importjava.awt.event.*;{Labellabel1,label2,label3,label4;Buttonbutton1,button2,button3,button4,button5;TextAreatext;Panelpanel1,panel2;staticfloatsum=0.0f;ShopFrame(Strings){super(s);setLayout(newBorderLayout());label1=newLabel("面紙:3元",Label.LEFT);label2=newLabel("鋼筆:5元",Label.LEFT);label3=newLabel("書:10元",Label.LEFT);label4=newLabel("襪子:8元",Label.LEFT);button1=newButton("加入購物車");button2=newButton("加入購物車");button3=newButton("加入購物車");button4=newButton("加入購物車");button5=newButton("查看購物車");text=newTextArea("商品有:"+"\n",5,10);text.setEditable(false);addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEvente){System.exit(0);}});button1.addActionListener(this);button2.addActionListener(this);button3.addActionListener(this);button4.addActionListener(this);button5.addActionListener(this);panel1=newPanel();panel2=newPanel();panel1.add(label1);panel1.add(button1);panel1.add(label2);panel1.add(button2);panel1.add(label3);panel1.add(button3);panel1.add(label4);panel1.add(button4);panel2.setLayout(newBorderLayout());panel2.add(button5,BorderLayout.NORTH);panel2.add(text,BorderLayout.SOUTH);this.add(panel1,BorderLayout.CENTER);this.add(panel2,BorderLayout.SOUTH);setBounds(100,100,350,250);setVisible(true);validate();}publicvoidactionPerformed(ActionEvente){if(e.getSource()==button1){text.append("一個面紙、");sum=sum+3;}elseif(e.getSource()==button2){text.append("一隻鋼筆、");sum=sum+5;}elseif(e.getSource()==button3){text.append("一本書、");sum=sum+10;}elseif(e.getSource()==button4){text.append("一雙襪子、");sum=sum+8;}elseif(e.getSource()==button5){text.append("\n"+"總價為:"+"\n"+sum);}}}publicclassShopping{publicstaticvoidmain(String[]args){newShopFrame("購物車");}}我沒用Swing可能顯示不出來你的效果。不滿意得話我在給你編一個。

❷ 請用java代碼實現如下方法

//供參考,代碼應該沒問題,難點主要是在如何匹配連續字元串,這點有小技巧
publicclassTestBaiKnow{

publicstaticvoidmain(Stringargs[])throwsException{
Strings="";

char[]in=s.toCharArray();
char[]out="abcd".toCharArray();

intcnt=0;
cnt=maxfleg(in,out);
System.out.println("出現次數:"+cnt);
}

privatestaticintmaxfleg(char[]inputstr,char[]outputstr){

Strings="";

Strings2="";
for(inti=0;i<outputstr.length;i++)
s2+=outputstr[i];

intcnt=0;
intmaxcnt=0;
for(inti=0;i<inputstr.length-4;i++){
for(intj=i;j<i+4;j++)
s+=inputstr[j];

if(s.equals(s2)){
cnt+=1;
i+=3;
if(cnt>maxcnt)
maxcnt=cnt;
}else
cnt=0;
s="";
}

returnmaxcnt;
}
}

❸ 用java代碼實現

packagetest;

importjava.awt.FlowLayout;
importjava.text.SimpleDateFormat;
importjava.util.Calendar;
importjava.util.Date;
importjava.util.GregorianCalendar;

importjavax.swing.JFrame;
importjavax.swing.JLabel;{

publicvoidrun(){
try{
while(true){
Datedate=newDate();
SimpleDateFormatsdf=newSimpleDateFormat("yyyy年MM月");
SimpleDateFormatsdf1=newSimpleDateFormat("dd");
SimpleDateFormatsdf2=newSimpleDateFormat("hh:mm:ss");
Stringweek=getWeekOfDate(date);
GregorianCalendarca=newGregorianCalendar();
Stringapm="";
intcheck=ca.get(GregorianCalendar.AM_PM);
if(check==0){
apm="上午";
}elseif(check==1){
apm="下午";
}
JLabeljl=newJLabel("<html>"
+"<divalign='center'>"+sdf.format(date)+"</div>"
+"<divalign='center'><fontsize=50>"+sdf1.format(date)+"</font>日</div>"
+"<divalign='center'>"+week+"</div>"
+"<divalign='center'>"+apm+sdf2.format(date)+"</div>"
+"</html>");
add(jl);
setLayout(newFlowLayout());
setSize(150,150);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
Thread.sleep(1000);
//jl.setText(null);
remove(jl);
}
}catch(Exceptione){}
}
(Datedt){
String[]weekDays={"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
Calendarcal=Calendar.getInstance();
cal.setTime(dt);
intw=cal.get(Calendar.DAY_OF_WEEK)-1;
if(w<0)
w=0;
returnweekDays[w];
}

publicstaticvoidmain(String[]args){
FrameDemomr=newFrameDemo();
Threadt1=newThread(mr);
t1.start();
}
}

❹ 求一段JAVA代碼(實例代碼)

import java.awt.event.*;
import java.util.ArrayList;
import java.util.List;

import javax.swing.*;

public class FrameTest {
public static void main(String[] args) {
new F().setVisible(true);
}
}

class F extends JFrame implements ActionListener {
JCheckBox[] jCheckBoxs;

JRadioButton d, e, f;

ButtonGroup g;

JButton bt;

F() {
this.setBounds(100, 300, 400, 150);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.getContentPane().setLayout(new java.awt.GridLayout(3, 3));
jCheckBoxs = new JCheckBox[3];
jCheckBoxs[0] = new JCheckBox("JCheckBox A", true);// 默認為選中
jCheckBoxs[1] = new JCheckBox("JCheckBox B");
jCheckBoxs[2] = new JCheckBox("JCheckBox C");
jCheckBoxs[0].addActionListener(this);
jCheckBoxs[1].addActionListener(this);
jCheckBoxs[2].addActionListener(this);
add(jCheckBoxs[0]);
add(jCheckBoxs[1]);
add(jCheckBoxs[2]);

d = new JRadioButton("JRadioButton 1", true);
e = new JRadioButton("JRadioButton 2");
f = new JRadioButton("JRadioButton 3");
d.addActionListener(this);
e.addActionListener(this);
f.addActionListener(this);
add(d);
add(e);
add(f);

g = new ButtonGroup();
g.add(d);
g.add(e);
g.add(f);

bt = new JButton("OK");
add(bt);
bt.addActionListener(this);
}
private List checkSelectJCheckBox(){
List list = new ArrayList();
int num = 0;
for(num=0;num<3;num++){
if(jCheckBoxs[num].isSelected()){
list.add(jCheckBoxs[num].getText());
}
}
return list;
}
private String message(){
List list = checkSelectJCheckBox();
String str = "您選擇的復選框為:\n";
for(int i=0;i<list.size();i++){
str = str+list.get(i)+"\n";
}
return str;
}
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("OK")) {
JOptionPane.showConfirmDialog(null, message(), "提示!",
JOptionPane.OK_OPTION);
return;
}
this.setTitle("最後動作: " + e.getActionCommand());
}
}

您想要的是這個嘛?

❺ 用java編程實現,求代碼

javarrr閱讀數:872018-11-16
剛剛開始學習Java,你可能會想Java用在什麼地方。你可能會想,除了《我的世界》這款游戲外其他游戲很少用Java開發,桌面工具如Adobe Acrobat、Microsoft Office也沒有用Java開發,甚至操作系統Linux、Windows也沒用。那麼人們到底會在什麼地方用到Java呢?

Java到底有沒有現實世界的應用?好吧,其實並不是只有你一個人對這個問題感到困惑。很多程序員在開始Java之前或者在畢業時選擇Java作為編程語言時也問過相同的問題。順便說一下,你可以通過在你計算機上安裝Java獲得Java在哪些地方使用的相關線索。Oracle聲稱超三十億的設備上運行著Java,那是一個巨大的數字,不是嗎?大多數公司用一種或者其他方式使用Java。很多伺服器端使用Java來處理每天數十百萬的請求,高頻率的交易應用同樣也使用Java,例如LMAX交易程序,這套程序基於他們開創性的線程間通訊庫——Disruptor。在這篇文章中,我們將會看到一些更精確的例子。什麼種類的項目使用Java開發?在哪些領域內Java是占統治地位的?現實生活中Java到底用在什麼地方?

現實生活中的Java應用
在現實生活中,很多地方都用到了Java,從商業上的電子商務網站安卓App,從科學應用到經濟應用,如電子交易系統,從游戲如《我的世界》到桌面應用,如Eclipse、Netbeans、Interllij,從開源類庫到J2ME應用。讓我們更詳細地看一下它們。

❻ java基礎代碼案例

public class HelloWorld {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

System.out.println("Hello,World !"); }
}

❼ Java代碼實現

public class Fibonacci {
public int Fibonacci(int i) {
if (i <= 2) {
return 1;
}
return Fibonacci(i - 1) + Fibonacci(i - 2);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Fibonacci fc= new Fibonacci();
System.out.print("斐波拉切數列第二十位數=" + fc.Fibonacci(20));
}
}

❽ 求JAVA源代碼

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class GradeStatistic {

public static void main(String[] args) {

GradeStatistic gs = new GradeStatistic();

List<Mark> list = new ArrayList<Mark>();

float sum = 0;

while(true){

Scanner sc = new Scanner(System.in);

System.out.print("Please input student name: ");
String name = sc.nextLine();

if(name.equals("end")){
break;
}

System.out.print("Please input student score: ");
float score = sc.nextFloat();
sum += score;

list.add(gs.new Mark(name, score));

}

float max = list.get(0).getScore();
float min = list.get(0).getScore();

for(Mark mark: list){
if(max < mark.getScore()){
max = mark.getScore();
}

if(min > mark.getScore()){
min = mark.getScore();
}

}

float average = sum / list.size();

System.out.println("Average is: " + average);
System.out.println("Max is: " + max);
System.out.println("Min is: " + min);
}

private class Mark{
private String name;
private float score;

public Mark(String name, float score){
this.name = name;
this.score = score;
}
public String getName() {
return name;
}
public float getScore() {
return score;
}
}
}
----------------------
Please input student name: Zhang san
Please input student score: 100
Please input student name: Li Si
Please input student score: 91
Please input student name: Ec
Please input student score: 35
Please input student name: ma qi
Please input student score: 67
Please input student name: end
Average is: 73.25
Max is: 100.0
Min is: 35.0

❾ 求編寫java代碼

packageorg.easynoder;

publicclassPerson{
privateStringname;
privateStringsex;
privateintage;

publicPerson(){
}

publicPerson(Stringname){
this.name=name;
}

publicPerson(Stringname,Stringsex){
this(name);
this.sex=sex;
}

publicPerson(Stringname,Stringsex,intage){
this(name,sex);
this.age=age;
}

publicintgetAge(){
returnage;
}

publicvoidsetAge(intage){
this.age=age;
}

//列印用戶信息
publicvoidprint(){
StringBuffersb=newStringBuffer();
sb.append(this.name+"詳情如下:");
sb.append("[姓名:"+this.name);
sb.append(";年齡:"+getAge());
sb.append(";性別:"+this.sex+"]");
System.out.println(sb.toString());
}

publicstaticvoidmain(String[]args){
//測試
Personliyapeng=newPerson("李亞鵬","男",43);
Personwangfei=newPerson("王飛","女",45);
Personxietingfeng=newPerson("謝挺峰","男",43);
Personzhangbo=newPerson("張柏汁","女",43);

Person[]persons=newPerson[]{liyapeng,wangfei,xietingfeng,
zhangbo};
for(Personperson:persons){
person.print();
}
System.out.println("咦?冠希哥呢?");
}

}

閱讀全文

與java實例代碼相關的資料

熱點內容
linuxfstab掛載 瀏覽:750
mac版本qq怎麼改密碼錯誤 瀏覽:553
define組合一般用於頭文件中 瀏覽:165
電影里有個叫大衛的男主角 瀏覽:584
三陰僵屍電影的別名 瀏覽:703
DP 電影 瀏覽:979
有部電影講8個人分娩鏡頭 瀏覽:129
ps4文件夾怎麼加封面 瀏覽:444
網站開發怎麼選擇 瀏覽:446
魅族root升級 瀏覽:46
送快遞電影賣內衣 瀏覽:691
電信網路怎麼繳費 瀏覽:423
杜文澤電影日本 瀏覽:312
看片群qq 瀏覽:516
法國愛情電影完整免費版 瀏覽:421
韓國電影一個女人的手被個被換了什麼電影 瀏覽:228
變身之後我與她的狂想曲下載 瀏覽:903
催眠科技晶元陳銀 瀏覽:332
求免費看片 瀏覽:910

友情鏈接