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+"谢谢您的支持!");
}
}
}
您好:package test;import java.util.HashSet;
import java.util.Set;
import java.util.Iterator;public class test {
public static void main(String[] args) {
Set set = new HashSet();
for (int i = 1; i = 100; i++) {
set.add(i);
}
Iterator it = set.iterator();
for (int i = 0; i set.size(); i++) {
if(it.hasNext()){
System.out.println(it.next());
}
}
}
}先把100放入set然后再随即找出来。另外一个想法是随即产生数,放入list中判断是否重复,这样也达到这样的效果。
// 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
帮你做了一个,不知是否满意呢?
import java.applet.Applet;
import java.awt.Button;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.util.Random;
import java.util.Vector;
public class Lottery extends Applet {
private static final long serialVersionUID = 1L;
int w,h;
Button ok,out,setup;
String[] msg;
TextField[] gaiLv;
TextField[] jiangPin;
int mx,ml;
int maxNum;
Random ran;
VectorInteger fist;
VectorInteger sec;
VectorInteger third;
VectorInteger lucky;
boolean lot=false;
boolean iserr=false;
boolean issetup=false;
String mesg="输入错误";
String priseMsg="继续努力!";
public void init(){
w=400;
h=220;
mx=20;
ml=40;
ran=new Random();
this.setSize(w, h);
this.setLayout(null);
ok=new Button("抽奖");
out=new Button("退出");
setup=new Button("确认设置");
msg=new String[4];
msg[0]="一等奖";
msg[1]="二等奖";
msg[2]="三等奖";
msg[3]="幸运奖";
gaiLv=new TextField[4];
jiangPin=new TextField[4];
for(int i=0;i4;i++){
gaiLv[i]=new TextField("0.0"+(i+1));
this.add(gaiLv[i]);
gaiLv[i].setBounds(mx+ml, 75+i*26, 60, 18);
jiangPin[i]=new TextField();
this.add(jiangPin[i]);
jiangPin[i].setBounds(mx+ml*3, 75+i*26, 80, 18);
}
this.add(ok);
ok.setBounds(260, 180, 60, 28);
ok.addActionListener(new LotButtonAction(this));
this.add(out);
out.setBounds(330, 180, 60, 28);
out.addActionListener(new LotButtonAction(this));
this.add(setup);
setup.setBounds(110, 180, 80, 24);
setup.addActionListener(new LotButtonAction(this));
}
public void paint(Graphics g){
g.setColor(Color.white);
g.fillRect(0, 0, this.getWidth(), this.getHeight());
g.setColor(new Color(230,255,230));
g.fillRect(0, 0, w, 30);
g.setColor(Color.BLUE);
g.drawString("JAVA抽奖系统", 130, 20);
g.setColor(Color.ORANGE);
g.drawRect(10, 40, 230, 170);
g.setColor(Color.BLACK);
g.drawString("设置", mx, 60);
g.drawString("概率", mx+ml, 60);
g.drawString("奖品", mx+ml*3, 60);
for(int i=0;imsg.length;i++){
g.setColor(new Color(255-(i*30),45,89));
g.drawString(msg[i], 20, 90+i*26);
}
if(lot==true){
g.setColor(new Color(ran.nextInt(255),ran.nextInt(255),ran.nextInt(255)));
if(priseMsg.length()=7){
g.drawString(priseMsg, 260, 100);
}else{
g.drawString(priseMsg.substring(0, 5), 260, 100);
g.drawString(priseMsg.substring(5), 260, 120);
}
}
if(iserr==true){
g.drawString(mesg, 260, 100);
}
}
public void getLucky(){
float firu=1;
float secu=1;
float thiu=1;
float fouu=1;
float minu=1;
if(gaiLv[0].getText().trim().length()1){
firu=Float.parseFloat(gaiLv[0].getText());
if(firu=0||firu=1){
iserr=true;
return;
}
if(firuminu){
minu=firu;
}
}
if(gaiLv[1].getText().trim().length()1){
secu=Float.parseFloat(gaiLv[1].getText());
if(secu=0||secu=1){
iserr=true;
return;
}
if(secuminu){
minu=secu;
}
}
if(gaiLv[2].getText().trim().length()1){
thiu=Float.parseFloat(gaiLv[2].getText());
if(thiu=0||thiu=1){
iserr=true;
return;
}
if(thiuminu){
minu=thiu;
}
}
if(gaiLv[3].getText().trim().length()1){
fouu=Float.parseFloat(gaiLv[3].getText());
if(fouu=0||fouu=1){
iserr=true;
return;
}
if(fouuminu){
minu=fouu;
}
}
if(minu=1||minu=0){
iserr=true;
return;
}
float aNum=1/minu;
maxNum=(int)aNum;
int count=(int)(firu/minu);
if(firu!=1){
fist=getLotteryVec(maxNum,count);
}else{
fist.removeAllElements();
}
count=(int)(secu/minu);
if(secu!=1){
sec=getLotteryVec(maxNum,count);
}else{
sec.removeAllElements();
}
count=(int)(thiu/minu);
if(thiu!=1){
third=getLotteryVec(maxNum,count);
}else{
third.removeAllElements();
}
count=(int)(fouu/minu);
if(fouu!=1){
lucky=getLotteryVec(maxNum,count);
}else{
lucky.removeAllElements();
}
issetup=true;
iserr=false;
}
protected VectorInteger getLotteryVec(int maxNum,int num){
VectorInteger result=new VectorInteger();
for(int i=0;inum;i++){
result.add(ran.nextInt(maxNum));
}
return result;
}
protected int getaNum(){
return ran.nextInt(maxNum);
}
public int isLucky(int pNum){
for(int i=0;ifist.size();i++){
if(fist.get(i)==pNum){
return 1;
}
}
for(int i=0;isec.size();i++){
if(sec.get(i)==pNum){
return 2;
}
}
for(int i=0;ithird.size();i++){
if(third.get(i)==pNum){
return 3;
}
}
for(int i=0;ilucky.size();i++){
if(lucky.get(i)==pNum){
return 4;
}
}
return -1;
}
public void ButtonActionPerformed(ActionEvent e){
String acName=e.getActionCommand();
if(acName.equals("抽奖")){
if(issetup==false){
priseMsg="请先设置参数!";
lot=true;
repaint();
lot=false;
return;
}
lot=true;
priseMsg=getResult(getaNum());
repaint();
// lot=false;
}else if(acName.equals("退出")){
this.setVisible(false);
this.stop();
this.destroy();
System.exit(0);
}else if(acName.equals("确认设置")){
lot=false;
getLucky();
repaint();
}
}
public String getResult(int num){
int resu=isLucky(num);
String result="";
switch(resu){
case -1:
result="继续努力!";
break;
case 1:
result="恭喜你!一等奖!\n获得"+jiangPin[0].getText();
break;
case 2:
result="恭喜你!二等奖!\n获得"+jiangPin[1].getText();
break;
case 3:
result="恭喜你!三等奖!\n获得"+jiangPin[2].getText();
break;
case 4:
result="恭喜你!幸运奖!\n获得"+jiangPin[3].getText();
break;
default:
result="继续努力!";
}
return result+num;
}
}
class LotButtonAction implements java.awt.event.ActionListener{
Lottery su;
public LotButtonAction(Lottery bun){
this.su=bun;
}
@Override
public void actionPerformed(ActionEvent e) {
su.ButtonActionPerformed(e);
}
}
代码如下:
public class HelloWorld {
public static void main(String []args) {
int a = 3, b = 7 ;
System.out.println("Hello World!");
}
public static int f(int a, int b){
return a*a + a*b + b*b;
}
}
结果如下: