189 8069 5689

猜价格的Java代码 java猜数字代码

求用java语言制作猜物品价格的小游戏的程序代码

import java.awt.*;

创新互联公司专注于网站建设|成都网站维护公司|优化|托管以及网络推广,积累了大量的网站设计与制作经验,为许多企业提供了网站定制设计服务,案例作品覆盖成都展览展示等行业。能根据企业所处的行业与销售的产品,结合品牌形象的塑造,量身开发品质网站。

import java.awt.event.*;

import java.applet.*;

import java.io.*;

public class testframe extends Frame implements ActionListener

{

Button b1;

Panel p1;

Label text1;

TextField text2;

TextArea text3;

int num[];

int i;

String str;

int times=0;

int A,B;

public testframe(String str)

{

b1=new Button("确定");

p1=new Panel();

text1=new Label("输入四位整数:");

text2=new TextField(10);

text3=new TextArea(10,10);

p1.setLayout(null);

p1.setBackground(Color.GRAY);

setSize(300,300);

setVisible(true);

b1.setBackground(Color.BLUE);

b1.setForeground(Color.WHITE);

b1.setSize(80,30);

p1.add(text1);

p1.add(text2);

p1.add(text3);

p1.add(b1);

text1.setBounds(0,0,100,20);

text2.setBounds(0,2*text1.getBounds().height,150,20);

text3.setBounds(0,3*text1.getBounds().height,150,150);

b1.setBounds(200,50,60,30);

add(p1);

b1.addActionListener(this);

addWindowListener(new shut());

}

public void rand()

{ num=new int[10];

for(i=0;i4;i++)

{

num[i]=(int)(Math.random()*10);

int j=0;

while(ji)

{if(num[j]==num[i])

{num[i]=(int)(Math.random()*10);

continue;

}

j++;

}

}

}

public void compair()

{

int x,m,n;

x=Integer.parseInt(text2.getText());

A=0;B=0;

for(m=3;m-1;m--){

for(n=0;n4;n++){

if((int)(x/Math.pow(10,m))==num[n])

{

if(m+n==3)

A++;

else

B++;

}

}

x=x-(int)(x/Math.pow(10,m))*(int)(Math.pow(10,m));

}

}

public void actionPerformed(ActionEvent e)

{

if(e.getSource()==b1)

if(times5){

if(Integer.parseInt(text2.getText())1000||Integer.parseInt(text2.getText())9999)

text2.setText("输入数字不合法");

else

{ if(A==4)

text2.setText("猜对了");

times++;

compair();

text3.append("第");

text3.append(String.valueOf(times));

text3.append("猜的结果是:");

text3.append(String.valueOf(A));text3.append("A");

text3.append(String.valueOf(B));text3.append("B");

text3.append("\n");

}

}

else

{

text3.setText("超过5次,重新开始.\n");

times=0;

rand();

}

}

public static void main(String[] args)

{

new testframe("窗口程序");

}

}

class shut extends WindowAdapter

{

public void windowClosing(WindowEvent e)

{

System.exit(0);

}

}

用JAVA语言写随即猜测商品价格!

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.util.Random;

public class Sort {

public static void main(String args[]) {

Random rand = new Random();

String shop[][]={{"牙膏","7"},{"牙刷","6"},{"飞机","10"}};

int i=(int) (Math.random() * (shop.length));

int price=Integer.parseInt(shop[i][1]);

System.out.println("随即商品:"+shop[i][0]);

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

System.out.println("请竞猜商品价格:(限20次)");

int a=0;

int count=1;

for(int j=0;j20;j++)

{

count++;

try {

a= Integer.parseInt(br.readLine());

if(count=20){

if(a==price){

System.out.println("恭喜你!!价格正确!!");

break;

}

else{

System.out.println("很遗憾,价格错误");

}

}

else{

System.out.println("20次机会用完");

}

}

catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

}

我的测试:

随即商品:飞机

请竞猜商品价格:(限20次)

1

很遗憾,价格错误

2

很遗憾,价格错误

3

很遗憾,价格错误

10

恭喜你!!价格正确!!

猜商品价格游戏程序。利用随机数生成商品的价格,要求价格在0到1000元之间的一个随机整数。然后用户输入一

//这是Java控制台程序,直接拷进去运行

import java.io.*;import java.util.Random;

public class RandomNum{

public static void main(String[] args)

{

Random random = new Random();

int num=random.nextInt(10000);

boolean flag=true;

int count=1;

String s="";

//这个可以不显示

System.out.println("竞猜数字是:"+Integer.toString(num));

while(flag){

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

try{

s=br.readLine();}

catch(Exception ex)

{

System.out.println(ex.toString());

}

int j=Integer.parseInt(s);

if(jnum)

{

System.out.println("竞猜错误小了");

count++;

}

if(jnum)

{

System.out.println("竞猜错误大了");

count++;

}

else if(j==num)

{

System.out.println("竞猜正确,供猜了:"+Integer.toString(count));

System.out.println("竞猜数字是:"+Integer.toString(num));

flag=false;

}

}}

}

java编写竞猜程序

import java.io.*;

class QuessMatchine{

int num;

public QuessMatchine(){

num = (int)(Math.random()*100);//随即生成0到99的整数

}

public String initial(int n){

String result = "";

if(nnum){

result = "您输入的数字过大,请重新输入";

}else

if(nnum){

result = "您输入的数字过小,请重新输入";

}

else{

result = "恭喜您,猜对了";

}

return result;

}

}

public class guess{

public static void main(String[] args){

boolean b = true;

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

String temp ="";

String result = "";

int n;

QuessMatchine qm = new QuessMatchine();

System.out.println("系统已经为你生成了一个0到99的数字,请您猜猜它是几");

while(b){

try{

temp = br.readLine();

n = Integer.parseInt(temp);

}catch(Exception e ){

System.out.println("请输入数字,范围在0到99之间");

continue;

}

result = qm.initial(n);

System.out.println(result);

if(result.equals("恭喜您,猜对了")){

b = false;

}

}

}

}


网页题目:猜价格的Java代码 java猜数字代码
链接分享:http://cdxtjz.cn/article/doggjhs.html

其他资讯