文章目录:
java抽奖小程序源码
import java.util.Scanner;
public class ChouJiang {
public static void main(String[] args) {
System.out.println("我行我素购物管理系统幸运抽奖");
System.out.println("");
System.out.println("");
Scanner input=new Scanner(System.in);
System.out.print("请输入四位会员号:");
int hyh=input.nextInt();
int q,b,s,g,sum;
q=hyh/1000;
b=hyh/100%10;
s=hyh/10%10;
g=hyh%10;
sum=q+b+s+g;
if(sum=23)
{
System.out.println(hyh+"是幸运客户,获精美MP3一台!");
}else
{
System.out.println(hyh+"谢谢您的支持!");
}
}
}
求JAVA制作小程序方法
Java Applet程序开发步骤
Java Applet程序开发主要步骤如下:
1)选用EDIT或WindowsNotepad等工具作为编辑器建立Java Applet源程序。
2)把Java Applet的源程序转换为字节码文件。
3)编制使用class的HTML文件。在HTML文件内放入必要的<Java Applet>语句。
下面举一个最简单的HelloWorld例子来说明Java Applet程序的开发过程:
(1)编辑Java Applet的java源文件
创建文件夹C:\ghq,在该文件夹下建立HelloWorld.java
文件的源代码如下:
importjava.awt.*;
importjava.Java Applet.*;
publicclassHelloWorldextendsJava Applet//继承Appelet类,这是AppeletJava程序的特点
{
publicvoidpaint(Graphicsg)
{
g.drawString("HelloWorld!",5,35);
}
}
保存上述程序在C:\ghq\HelloWorld.java文件里。
(2)编译Java Applet
编译HelloWorld.java源文件可使用如下JDK命令:
C:\ghq\>javacHelloWorld.java<Enter>
注意:如果编写的源程序违反了Java编程语言的语法规则,Java编译器将在屏幕上显示语法错误提示信息。源文件中必须不含任何语法错误,Java编译器才能成功地把源程序转换为Java Appletviewer和浏览器能够执行的字节码程序。
成功地编译Java Applet之后生成响应的字节码文件HelloWorld.class的文件。用资源管理器或DIR命令列出目录列表,将会发现目录C:\ghq中多了一个名为HelloWorld.class的文件。
(3)创建HTML文件
在运行创建的HelloWorld.class之前,还需创建一个HTML文件,Java Appletviewer或浏览器将通过该文件访问创建的Java Applet。为运行HelloWorld.class,需要创建包含如下HTML语句的名为HelloWorld.html的文件。
<HTML>
<TITLE>HelloWorld!Java Applet</TITLE>
<Java Applet
CODE="JavaWorld.class"
WIDTH=200
HEIGHT=100>
</Java Applet>
</HTML>
本例中,<Java Applet>语句指明该Java Applet字节码类文件名和以像素为单位的窗口的尺寸。虽然这里HTML文件使用的文件名为HelloWorld.HTML,它对应于HelloWorld.java的名字,但这种对应关系不是必须的,可以用其他的任何名字(比如说Ghq.HTML)命名该HTML文件。但是使文件名保持一种对应关系可给文件的管理带来方便。
(4)执行HelloWorld.html
如果用Java Appletviewer运行HelloWorld.html,需输入如下的命令行:
C:\ghq\>Java AppletviewerJavaWorld.html<ENTER>
可以看出,该命令启动了Java Appletviewer并指明了HTML文件,该HTML文件中包含对应于HelloWorld的<Java Applet>语句。
如果用浏览器运行HelloWorldJava Applet,需在浏览器的地址栏中输入HTML文件URL地址。
至此,一个Java Applet程序的开发运行整个过程结束了(包括java源文件、编译的class文件、html文件以及用Java Appletviewer或用浏览器运行)。
java 小程序源代码
求素数,比较经典的,下面是代码及注释
================================================
public class Sushu {
/**
* 判断一个数是不是素数
* @param a 被判断的数
* @return 是素数返回真
*/
public boolean isSuhu(int a){
boolean isSushu=true;
//根据素数的性质判断一个数是否为素数
for(int i=2;ia;i++){
if(a%i==0){
isSushu=false;
break;
}
}
return isSushu;
}
/**
* 判断连续若干个数中那些是素数
* @param start 起始数
* @param end 终止数
*/
public void selectSushu(int start,int end){
//判断一串数中那些为素数,并将结果打印出来
for(int i=start;i=end;i++){
if(isSuhu(i)){
System.out.println(i);
}
}
}
public static void main(String []args){
//定义起始位置和终止位置
int start=1;
int end=100;
//声明变量
Sushu s=new Sushu();
//调用方法
s.selectSushu(start, end);
}
}
java小程序源代码,简单点的,100多行,谁有啊??
// My car shop.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
public class carshop extends JFrame
{
// JPanel to hold all pictures
private JPanel windowJPanel;
private String[] cars = { "","阿斯顿马丁", "美洲虎", "凯迪拉克",
"罗孚", "劳斯莱斯","别克"};
private int[] jiage = { 0,150000, 260000, 230000,
140000, 290000, 150000};
// JLabels for first snack shown
private JLabel oneJLabel;
private JLabel oneIconJLabel;
// JLabels for second snack shown
private JLabel twoJLabel;
private JLabel twoIconJLabel;
// JLabels for third snack shown
private JLabel threeJLabel;
private JLabel threeIconJLabel;
// JLabels for fourth snack shown
private JLabel fourJLabel;
private JLabel fourIconJLabel;
// JLabels for fifth snack shown
private JLabel fiveJLabel;
private JLabel fiveIconJLabel;
// JLabels for sixth snack shown
private JLabel sixJLabel;
private JLabel sixIconJLabel;
// JTextField for displaying snack price
private JTextArea displayJTextArea;
// JLabel and JTextField for user input
private JLabel inputJLabel;
private JComboBox selectCountryJComboBox;
private JLabel inputJLabel2;
private JTextField inputJTextField2;
// JButton to enter user input
private JButton enterJButton;
//JButton to clear the components
private JButton clearJButton;
// no-argument constructor
public carshop()
{
createUserInterface();
}
// create and position GUI components; register event handlers
private void createUserInterface()
{
// get content pane for attaching GUI components
Container contentPane = getContentPane();
// enable explicit positioning of GUI components
contentPane.setLayout( null );
// set up windowJPanel
windowJPanel = new JPanel();
windowJPanel.setBounds( 10, 20, 340, 200 );
windowJPanel.setBorder( new LineBorder( Color.BLACK ) );
windowJPanel.setLayout( null );
contentPane.add( windowJPanel );
// set up oneIconJLabel
oneIconJLabel = new JLabel();
oneIconJLabel.setBounds( 10, 20, 100, 65 );
oneIconJLabel.setIcon( new ImageIcon( "images/阿斯顿马丁.jpg" ) );
windowJPanel.add( oneIconJLabel );
// set up oneJLabel
oneJLabel = new JLabel();
oneJLabel.setBounds( 10, 60, 100, 70 );
oneJLabel.setText( "阿斯顿马丁" );
oneJLabel.setHorizontalAlignment( JLabel.CENTER );
windowJPanel.add( oneJLabel );
// set up twoIconJLabel
twoIconJLabel = new JLabel();
twoIconJLabel.setBounds( 120, 20, 100, 65 );
twoIconJLabel.setIcon( new ImageIcon( "images/美洲虎.jpg" ) );
windowJPanel.add( twoIconJLabel );
// set up twoJLabel
twoJLabel = new JLabel();
twoJLabel.setBounds( 110, 60, 100, 70 );
twoJLabel.setText( "美洲虎" );
twoJLabel.setHorizontalAlignment( JLabel.CENTER );
windowJPanel.add( twoJLabel );
// set up threeIconJLabel
threeIconJLabel = new JLabel();
threeIconJLabel.setBounds( 230, 20, 100, 65 );
threeIconJLabel.setIcon( new ImageIcon(
"images/凯迪拉克.jpg" ) );
windowJPanel.add( threeIconJLabel );
// set up threeJLabel
threeJLabel = new JLabel();
threeJLabel.setBounds( 230, 60, 100, 70);
threeJLabel.setText( "凯迪拉克" );
threeJLabel.setHorizontalAlignment( JLabel.CENTER );
windowJPanel.add( threeJLabel );
// set up fourIconJLabel
fourIconJLabel = new JLabel();
fourIconJLabel.setBounds( 10, 100, 100, 65 );
fourIconJLabel.setIcon( new ImageIcon( "images/罗孚.jpg" ) );
windowJPanel.add( fourIconJLabel );
// set up fourJLabel
fourJLabel = new JLabel();
fourJLabel.setBounds( 10, 150, 50, 70 );
fourJLabel.setText( "罗孚" );
fourJLabel.setHorizontalAlignment( JLabel.CENTER );
windowJPanel.add( fourJLabel );
// set up fiveIconJLabel
fiveIconJLabel = new JLabel();
fiveIconJLabel.setBounds( 120, 100, 100, 65 );
fiveIconJLabel.setIcon( new ImageIcon(
"images/劳斯莱斯.jpg" ) );
windowJPanel.add( fiveIconJLabel );
// set up fiveJLabel
fiveJLabel = new JLabel();
fiveJLabel.setBounds( 110, 150, 100, 70 );
fiveJLabel.setText( "劳斯莱斯" );
fiveJLabel.setHorizontalAlignment( JLabel.CENTER );
windowJPanel.add( fiveJLabel );
// set up sixIconJLabel
sixIconJLabel = new JLabel();
sixIconJLabel.setBounds( 230, 100, 100, 65 );
sixIconJLabel.setIcon( new ImageIcon( "images/别克.jpg" ) );
windowJPanel.add( sixIconJLabel );
// set up sixJLabel
sixJLabel = new JLabel();
sixJLabel.setBounds( 230, 150, 100, 70 );
sixJLabel.setText( "别克" );
sixJLabel.setHorizontalAlignment( JLabel.CENTER );
windowJPanel.add( sixJLabel );
// set up enterJButton
enterJButton = new JButton();
enterJButton.setBounds( 390, 160, 135, 30 );
enterJButton.setText( "Enter" );
contentPane.add( enterJButton );
enterJButton.addActionListener(
new ActionListener() // anonymous inner class
{
// event handler called when enterJButton is clicked
public void actionPerformed( ActionEvent event )
{
enterJButtonActionPerformed( event );
}
} // end anonymous inner class
); // end call to addActionListener
// set up clearJButton
clearJButton = new JButton();
clearJButton.setBounds( 390, 200, 135, 30 );
clearJButton.setText( "Clear" );
contentPane.add( clearJButton );
// set up inputJLabel
inputJLabel = new JLabel();
inputJLabel.setBounds( 390, 25, 135, 25 );
inputJLabel.setText( "Please make selection:" );
contentPane.add( inputJLabel );
selectCountryJComboBox = new JComboBox( cars );
selectCountryJComboBox.setBounds( 390, 50, 135, 21 );
selectCountryJComboBox.setMaximumRowCount( 3 );
contentPane.add( selectCountryJComboBox );
// set up inputJTextField
inputJLabel2 = new JLabel();
inputJLabel2.setBounds( 390, 80, 150, 20 );
inputJLabel2.setText( "Input the Numble:" );
contentPane.add( inputJLabel2 );
// set up inputJTextField
inputJTextField2 = new JTextField();
inputJTextField2.setBounds( 390, 100, 135, 25 );
inputJTextField2.setHorizontalAlignment( JTextField.RIGHT );
contentPane.add( inputJTextField2 );
clearJButton.addActionListener(
new ActionListener() // anonymous inner class
{
// event handler called when clearJButton is clicked
public void actionPerformed( ActionEvent event )
{
clearJButtonActionPerformed( event );
}
} // end anonymous inner class
);
// set up displayJTextField
displayJTextArea = new JTextArea();
displayJTextArea.setBounds( 10, 237,515, 70 );
displayJTextArea.setEditable( false );
contentPane.add( displayJTextArea );
// set properties of application's window
setTitle( "My car Shop" ); // set title bar string
setSize( 550, 360 ); // set window size
setVisible( true ); // display window
} // end method createUserInterface
private void clearJButtonActionPerformed( ActionEvent event )
{
// clear the JTextFields
inputJTextField2.setText( "" );
displayJTextArea.setText("");
} // end method clearJButtonActionPerformed
private void enterJButtonActionPerformed( ActionEvent event )
{
double z;
double c;
int x;
int y;
x=selectCountryJComboBox.getSelectedIndex();
y=Integer.parseInt(inputJTextField2.getText());
double discountRate;
int amount = Integer.parseInt( inputJTextField2.getText());
switch (amount/5)
{
case 0:
discountRate = 0;
break;
case 1:
discountRate = 1;
break;
case 2:
discountRate = 2;
break;
case 3:
discountRate = 3;
break;
default:
discountRate = 4;
} // end switch statement
c=1-discountRate/100;
z=jiage[x]*y*c;
displayJTextArea.append("你选择的是:"+cars[x]+";"+
"它的单价是:"+jiage[x]+";" +"你购买该产品的数量是:"+y+"," +"\n"+"该数量的折扣是:"
+discountRate + " %"+";"+"本次消费的总价格是:"+z+"元"+"!"+"\n");
}
public static void main( String args[] )
{
carshop application = new carshop();
application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
} // end method main
} // end class carshop
ic class carshop extends JFrame{ // JPanel to hold all pictures private JPanel windowJPanel; private String[] cars = { "","阿斯顿马丁", "美洲虎",
// event handler called when enterJButton is clicked public void actionPerformed( Act
ntalAlignment( JLabel.CENTER ); windowJPanel.add( oneJLabel ); // set up twoIconJLabel
文章目录:1、java抽奖小程序源码2、求JAVA制作小程序方法3、java 小程序源代码4、java小程序源代码,简单点的,100多行,谁有啊??java抽奖小程序源码import java.util.Scanner;public class ChouJiang {public