java的彩票
1编写Lottery类,它有:(1)成员变量:用于存放随机产生的7个号码;(2)成员方法:generate():实现7个号码的产生;每注号码为7个1~35的随机整数,每注...
1编写Lottery类,它有:
(1)成员变量:用于存放随机产生的7个号码;
(2)成员方法:
generate():实现7个号码的产生;每注号码为7个1~35的随机整数,每注7个号码中不能有重复号码。
pringTitle():实现票头的打印;
printData():实现号码的打印;
2.编写Lottey类的测试程序,实现35选7随机号码的生成:
用户从键盘输入要买彩票的注数n(通过输入对话框实现),程序随机产生n注号码并输出。 展开
(1)成员变量:用于存放随机产生的7个号码;
(2)成员方法:
generate():实现7个号码的产生;每注号码为7个1~35的随机整数,每注7个号码中不能有重复号码。
pringTitle():实现票头的打印;
printData():实现号码的打印;
2.编写Lottey类的测试程序,实现35选7随机号码的生成:
用户从键盘输入要买彩票的注数n(通过输入对话框实现),程序随机产生n注号码并输出。 展开
2个回答
展开全部
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
/**
* 测试彩票类
* @author doulmi
*
*/
public class LotteryDemo {
public static void main( String[] args ) {
Scanner scanner = new Scanner( System.in );
System.out.print( "请输入要购买的注数:" );
System.out.println();
int buyN = 0;
if ( scanner.hasNextInt() ) {
buyN = scanner.nextInt();
} else {
System.out.println( "请输入正确的数字" );
}
for ( int i = 0; i < buyN; i ++ ) {
Lottery lot = new Lottery();
lot.generate();
System.out.print( "第" );
System.out.print( i );
System.out.print( "注" );
lot.printData();
}
}
}
/**
* 彩票类
* @author Administrator
*
*/
class Lottery {
private static final int NUM_TO_GETOUT = 7;
//能产生的最大数值为99
private static final int MAX_NUM = 35;
public ArrayList< Integer > list;
private int[] result;
public Lottery() {
list = new ArrayList< Integer >();
for ( int i = 1; i <= MAX_NUM; i ++ ) {
list.add( i );
}
result = new int[ NUM_TO_GETOUT ];
}
public void generate() {
Random r = new Random();
for ( int i = 0; i < NUM_TO_GETOUT; i ++ ) {
result[ i ] = list.remove( r.nextInt( list.size() ) );
}
}
/**
* 关于票头的打印。。我不知道票头是什么。
*/
public void pringTitle() {
}
/**
* 实现号码的打印
*/
public void printData() {
for ( int n : result ) {
System.out.print( n );
System.out.print( " " );
}
System.out.println();
}
}
import java.util.Random;
import java.util.Scanner;
/**
* 测试彩票类
* @author doulmi
*
*/
public class LotteryDemo {
public static void main( String[] args ) {
Scanner scanner = new Scanner( System.in );
System.out.print( "请输入要购买的注数:" );
System.out.println();
int buyN = 0;
if ( scanner.hasNextInt() ) {
buyN = scanner.nextInt();
} else {
System.out.println( "请输入正确的数字" );
}
for ( int i = 0; i < buyN; i ++ ) {
Lottery lot = new Lottery();
lot.generate();
System.out.print( "第" );
System.out.print( i );
System.out.print( "注" );
lot.printData();
}
}
}
/**
* 彩票类
* @author Administrator
*
*/
class Lottery {
private static final int NUM_TO_GETOUT = 7;
//能产生的最大数值为99
private static final int MAX_NUM = 35;
public ArrayList< Integer > list;
private int[] result;
public Lottery() {
list = new ArrayList< Integer >();
for ( int i = 1; i <= MAX_NUM; i ++ ) {
list.add( i );
}
result = new int[ NUM_TO_GETOUT ];
}
public void generate() {
Random r = new Random();
for ( int i = 0; i < NUM_TO_GETOUT; i ++ ) {
result[ i ] = list.remove( r.nextInt( list.size() ) );
}
}
/**
* 关于票头的打印。。我不知道票头是什么。
*/
public void pringTitle() {
}
/**
* 实现号码的打印
*/
public void printData() {
for ( int n : result ) {
System.out.print( n );
System.out.print( " " );
}
System.out.println();
}
}
展开全部
下面是显示红色文字的代码例子,其他的设置类似,是由AttributeSet设置的。
package baidu.zhidao;
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
public class TextPane extends JFrame
{
JTextPane jtp = null;
DefaultStyledDocument doc = null;
SimpleAttributeSet attrset = null;
public TextPane()
{
super("JTextPane model");
doc = new DefaultStyledDocument();
jtp = new JTextPane(doc);
attrset = new SimpleAttributeSet();
StyleConstants.setForeground(attrset, Color.red);
try
{
doc.insertString(doc.getLength(), "你好,我是jink2005。\n", attrset);
}
catch (BadLocationException e)
{
}
this.getContentPane().add(new JScrollPane(jtp));
this.pack();
this.setVisible(true);
}
public static void main(String[] args)
{
new TextPane();
}
}
package baidu.zhidao;
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
public class TextPane extends JFrame
{
JTextPane jtp = null;
DefaultStyledDocument doc = null;
SimpleAttributeSet attrset = null;
public TextPane()
{
super("JTextPane model");
doc = new DefaultStyledDocument();
jtp = new JTextPane(doc);
attrset = new SimpleAttributeSet();
StyleConstants.setForeground(attrset, Color.red);
try
{
doc.insertString(doc.getLength(), "你好,我是jink2005。\n", attrset);
}
catch (BadLocationException e)
{
}
this.getContentPane().add(new JScrollPane(jtp));
this.pack();
this.setVisible(true);
}
public static void main(String[] args)
{
new TextPane();
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询