java猜数字代码怎么用 java猜数字代码怎么用的
java编写一个“猜数”程序:该程序随机在1到1000的范围中选择一个供用户猜测的整数
Java源程序附后。
目前成都创新互联已为近1000家的企业提供了网站建设、域名、网页空间、网站托管、企业网站设计、覃塘网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。
本程序的特点是:
(1) 文本框只能输入纯数字;
(2) 界面较美观;
(3) 代码可读性较好,有适当的注释;
(4) 窗体一出现就在桌面居中。
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
public class GuessNumber {
private static final long serialVersionUID = 1L;
JFrame frame;
JTextField txtNum; //文本框
JButton btnStart; //按钮
JLabel lblPrompt;
JLabel lblMessage;
static int source = 0;
static Random rand = new Random();
public GuessNumber(){
frame = new JFrame("Guess Number");
JPanel pnl1, pnl2, pnl3, pnl4;
pnl1 = new JPanel();
pnl1.setLayout(new FlowLayout(FlowLayout.LEFT));
pnl2 = new JPanel();
pnl2.setLayout(new FlowLayout(FlowLayout.LEFT));
pnl3 = new JPanel();
pnl3.setLayout(new FlowLayout(FlowLayout.LEFT));
pnl4 = new JPanel();
pnl4.setLayout(new FlowLayout(FlowLayout.LEFT));
txtNum = new JTextField(10);
btnStart = new JButton("开始");
lblPrompt = new JLabel("htmlbodyI have a number between 1 and 1000 can you guess my number?br/Please enter your first guess./body/html");
lblMessage = new JLabel();
pnl1.add(lblPrompt);
pnl2.add(txtNum);
pnl3.add(lblMessage);
pnl4.add(btnStart);
frame.setLayout(new GridLayout(4, 1));
frame.add(pnl1);
frame.add(pnl2);
frame.add(pnl3);
frame.add(pnl4);
txtNum.addActionListener(this.new TextAction());
txtNum.addKeyListener(this.new KeyAction());
btnStart.addActionListener(this.new ButtonAction());
frame.setSize(400, 200);
frame.setVisible(true);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
}
public static void main(String[] args) {
new GuessNumber();
while((source=rand.nextInt(1000))==0);
}
//按钮单击后的事件处理
class ButtonAction implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
JButton btn = (JButton)e.getSource();
if(btn == btnStart){
while((source=rand.nextInt(1000))==0);
txtNum.setEditable(true);
}
}
}
//文本框按回车后的事件处理
class TextAction implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
JTextField txt = (JTextField)e.getSource();
if(txt != txtNum){
return;
}
int num = Integer.parseInt(txtNum.getText());
if(num == source){
lblMessage.setText("Correct!");
txtNum.setEditable(false);
txtNum.setBackground(frame.getBackground());
}
else if(num source){
lblMessage.setText("Too High");
txtNum.setBackground(Color.red);
}
else{
lblMessage.setText("Too Low");
txtNum.setBackground(Color.blue);
}
}
}
//限制文本框只能输入数字
class KeyAction implements KeyListener{
@Override
public void keyPressed(KeyEvent e) {
}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void keyTyped(KeyEvent e) {
int k = e.getKeyChar();
String text = ((JTextField)e.getSource()).getText();
if(!((k47 k 58) || (k==8 || k==KeyEvent.VK_PERIOD))){ //限制只能输入数字
e.setKeyChar((char)KeyEvent.VK_CLEAR);
}
if(text.length() 4){ //限制数值的长度
e.setKeyChar((char)KeyEvent.VK_CLEAR);
}
}
}
}
java猜数字,如果猜对了,继续猜,一共猜5次,求代码。
import java.util.Random;
import java.util.Scanner;
public class caishu {
static int i = 0;
static int p;
public static void main(String[] args) {
// TODO Auto-generated method stub
p = (int) (Math.random() * 100);
//System.out.println(p);
while (i 5) {
try {
System.out.println("请输入你要猜的数:");
Scanner scanner = new Scanner(System.in);
int s = scanner.nextInt();
if (p != s) {
if (p s) {
System.out.println("你猜得数小了");
} else {
System.out.println("你猜得数大了");
}
} else {
System.out.println("恭喜你猜对了!");
return;
}
i++;
} catch (Exception ex) {
System.out.println("输入的数据有误!");
}
}
System.out.println("你的猜数次数已经用完了!");
}
}
运行结果
Java 补全下面程序 猜数字
public class GuessNumber {
public static void main(String[] args) {
System.out.println("给你一个0至100之间的整数,请猜测这个数");
//随机生成一个0至100之间的随机整数
int realNumber=(int)Math.round(Math.random()*100);
//从键盘上读入你的猜测;
System.out.print("请输入你的猜测:");
int yourGuess=System.in.read();
//是否猜对的标识
boolean isRight=false;
//循环校验,直到猜对了为止
while(!isRight){
if(yourGuess realNumber){
System.out.print("猜大了,再输入你的猜测:");
yourGuess= System.in.read();
}
else if(yourGuess realNumber){
System.out.print("猜小了,再输入你的猜测:");
yourGuess=System.in.read();
}
else{
isRight=true;
System.out.print("恭喜你,猜对了");
}
}
}
}
用java编猜数字游戏。
import java.util.Scanner;
public class GuessNumber {
public static void main(String[] args) {
//随机生成一个1-100的数
int randomNumber = (int) (Math.random() * 100 + 1);
System.out.println("输入一个number");
//键盘输入一个数
Scanner sc = new Scanner(System.in);
int guessNumber = sc.nextInt();
//
while(guessNumber != randomNumber)
{
if(guessNumber randomNumber)
{
System.out.println("猜大了,请继续……");
}
else
{
System.out.println("猜小了,请继续……");
}
guessNumber = sc.nextInt();
}
System.out.println("恭喜你,猜测正确!是否继续猜数");
}
}
格式有点丑陋。
网站题目:java猜数字代码怎么用 java猜数字代码怎么用的
转载来源:http://pwwzsj.com/article/ddjescs.html