public static void main(String[] args){
System.out.println("请输入您的4位会员卡号:")
Scanner sc = new Scanner(System.in)
int number = sc.nextInt() //接收用户从控制台输入的会员卡号,并保存在会员卡号变量中
int a = number/1000//千位
int b = number%1000/100//百位
int c = number%100/10//十位
int d = number%10 //个位
if((a+b+c+d)>20){
System.out.println("恭喜中奖!您是幸运客户")
}else{
System.out.println("谢谢参与!")
}
}
}
最基础的 没有异常判断 无限循环输入什么东西
我写给你,如果是本专业,以后要多多学习。JDK 1.6import java.awt.GridLayout
import java.awt.event.MouseEvent
import java.awt.event.MouseListener
import java.util.Iterator
import java.util.Random
import java.util.Set
import java.util.TreeSet
import javax.swing.JButton
import javax.swing.JFrame
import javax.swing.JTextField
public class LuckyDrawn {
private JFrame f = new JFrame("Lucky Drawn")
private JButton btn = new JButton("Drawn")
private JTextField result = new JTextField("Result")
private static final int MIN = 1
private static final int MAX = 30
private static final int RESULT_COUNT = 7
public LuckyDrawn(){
f.add(btn)
f.add(result)
result.setEditable(false)
btn.addMouseListener(new MouseListener(){
public void mouseClicked(MouseEvent e) {
Random rand = new Random()
Set<Integer>set = new TreeSet<Integer>()
while(set.size() <RESULT_COUNT){
set.add(new Integer(rand.nextInt(MAX)+ 1))
}
StringBuffer sb = new StringBuffer("Lucky numbers are: ")
for (Iterator iter = set.iterator()iter.hasNext()) {
sb.append(((Integer) iter.next()).intValue()).append(", ")
}
result.setText(sb.substring(0, sb.length() - 2).toString())
f.pack()
}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
})
f.setLayout(new GridLayout(2, 1))
f.setVisible(true)
f.setLocation(500, 200)
f.pack()
f.validate()
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
}
public static void main(String[] args) {
new LuckyDrawn()
}
}
你是要JAVA可视化的 还是控制台的啊?
控制台的话你可以用循环来取中奖号,将其存入list或set里(最好是set,因为set本来就不会重复)。如果用list存就另外写一个循环方法,来挨个判定该元素是不是已经在list中存在了,如果存在,就让选号的再加选一次,如果不重复,就放到list里去。。。。OK
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)