重庆分公司,新征程启航

为企业提供网站建设、域名注册、服务器等服务

java猜单词代码,java猜字母

猜单词 1.在一个窗口实现猜单词的游戏 2.定义一个字符串数组String [] s = {“hello","world","student"}

按照你的要求编写的Java猜单词的游戏如下:

创新互联是一家专业的成都网站建设公司,我们专注成都做网站、网站设计、外贸营销网站建设、网络营销、企业网站建设,买友情链接1元广告为企业客户提供一站式建站解决方案,能带给客户新的互联网理念。从网站结构的规划UI设计到用户体验提高,创新互联力求做到尽善尽美。

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.util.Vector;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JOptionPane;

import javax.swing.JPanel;

import javax.swing.JScrollPane;

import javax.swing.JTable;

import javax.swing.JTextField;

import javax.swing.table.DefaultTableModel;

public class GuessWord extends JFrame implements ActionListener{

String [] s = {"hello","world","student"};

JPanel jp=new JPanel();

JLabel jl_guessword=new JLabel();

JLabel jl_displayword=new JLabel();

JLabel jl_guesstimes=new JLabel();

JLabel jl_copyright=new JLabel("版权所有:xxxxxxxxxx");

JLabel jl_resultlist=new JLabel("猜取结果列表");

JTextField jtf=new JTextField(5);

JButton jb_guess=new JButton("我猜");

JButton jb_reset=new JButton("重置");

String[] columnNames={"序号","猜过的单词","猜的结果"};

String[][]data;

DefaultTableModel dtf=new DefaultTableModel(data,columnNames);

JTable jt=new JTable(dtf);

JScrollPane jsp=new JScrollPane(jt);

int count=0;

GuessWord(){

super("猜单词小游戏");

String str=s[(int)(Math.random()*3)];

jl_guessword.setText(str);

String str1="";

for(int i=0;istr.length();i++)

str1=str1+"*";

jl_displayword.setText(str1);

jp.setLayout(null);

jl_guessword.setVisible(false);

jl_guessword.setBounds(20, 0, 120,20);

jl_displayword.setBounds(20, 20, 120,20);

jtf.setBounds(20, 50, 100,20);

jl_guesstimes.setBounds(20, 80, 120,20);

jb_guess.setBounds(20, 115, 60, 20);

jb_reset.setBounds(20, 145, 60, 20);

jl_copyright.setBounds(20,180,200, 20);

jl_resultlist.setBounds(150, 20, 200,20);

jsp.setBounds(150, 40, 200,130);

jb_guess.addActionListener(this);

jb_reset.addActionListener(this);

jp.add(jl_guessword);

jp.add(jl_displayword);

jp.add(jtf);

jp.add(jl_guesstimes);

jp.add(jb_guess);

jp.add(jb_reset);

jp.add(jl_copyright);

jp.add(jl_resultlist);

jp.add(jsp);

add(jp);

setSize(400, 250);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setLocationRelativeTo(null);

setVisible(true);

}

public static void main(String[] args) {

new GuessWord();

}

@Override

public void actionPerformed(ActionEvent ae) {

if(ae.getSource()==jb_guess){

if(jtf.equals("")){

JOptionPane.showMessageDialog(this, "请输入你猜的单词!");

jtf.requestFocus();

}else{

String s1=jtf.getText().trim();

String s2=jl_guessword.getText().trim();

if(s1.length()!=s2.length()){

JOptionPane.showMessageDialog(this, "请输入"+s2.length()+"个字母的单词!");

jtf.requestFocus();

}else{

if(s1.equals(s2)){

jb_guess.setEnabled(false);

count++;

jl_displayword.setText(s2);

jl_guesstimes.setText("共猜了"+count+"次。");

Vector v=new Vector();

v.add(count);

v.add(jtf.getText().trim());

v.add(jtf.getText().trim());

dtf.addRow(v);

}else{

String s3="";

for(int i=0;is2.length();i++){

if(s2.charAt(i)==s1.charAt(i))

s3=s3+s1.charAt(i);

else

s3=s3+"*";

}

count++;

jl_displayword.setText(s3);

jl_guesstimes.setText("共猜了"+count+"次。");

Vector v=new Vector();

v.add(count);

v.add(jtf.getText().trim());

v.add(s3);

dtf.addRow(v);

}

}

}

}

if(ae.getSource()==jb_reset){

String str=s[(int)(Math.random()*3)];

jl_guessword.setText(str);

String str1="";

for(int i=0;istr.length();i++)

str1=str1+"*";

jl_displayword.setText(str1);

jtf.setText("");

jb_guess.setEnabled(true);

for(int i=dtf.getRowCount()-1;i=0;i--)

dtf.removeRow(i);

jl_guesstimes.setText("");

count=0;

}

}

}

运行结果:

用java写一个猜单词的程序 要求:猜不中提示错误,猜中后在该位置显示字母,还可以输入1后自动帮助显示

定义数组a存要显示的字幕,数组长度为n,生成0到n-1的随机数r,接受用户输入字符c,比较c和a[r],记录用户猜的次数,3次后不能继续

java 代码 猜字母

package util;

import java.util.Random;

import java.util.Scanner;

public class test {

public static void main(String[] args) {

Random rnd=new Random();

Scanner in = new Scanner(System.in);

char c=0;

String yn = "";

do{

c=(char)('A'+rnd.nextInt(27)); //产生的是随机字母

System.out.println("输入C:"+c);

for (int i = 0; i 4; i++) {

System.out.println("请输入一个大写字母:");

String at = in.next();

System.out.println(at);

String a = String.valueOf(c);

if (a.equals(at)) {

System.out.println("恭喜你,猜对了!");

break;

}else {

System.out.println("你猜错了!");

}

}

System.out.println("是否继续:y/n");

yn = in.next().trim();

}while("y".equals(yn));

System.out.println("你结束了猜字母的游戏!");

}

}

//直接粘贴,复制,就能运行。

求一个JAVA猜单词游戏的代码,要求如下

import java.util.Random;

import java.util.Scanner;

public class GuessString {

public static void main(String[] args){

String[] strings={"apple","orange","tom","kitty","hello","world","have","fun","hehe","ok"};

Random rand=new Random();

String choosed=strings[rand.nextInt()%10];

StringBuilder guessString=new StringBuilder(choosed);

StringBuilder sb=new StringBuilder();

for(int i=0;iguessString.length();i++)

sb.append("_ ");

System.out.println(sb);

Scanner scanner=new Scanner(System.in);

int numberGuessed=0;

while(numberGuessedguessString.length()){

char c=scanner.next().charAt(0);

int index=guessString.indexOf(c+"");

if(index=0){

if(sb.charAt(2*index)=='_'){

guessString.setCharAt(index, '*');

sb.setCharAt(index*2, c);

numberGuessed++;

}

}

System.out.println(sb);

}

}

}

Java简单编程帮忙谢!

代码如下:猜的次数和单词长度一样。

import java.util.* ;

public class T {

public static void main(String [] args) {

Scanner scanner=new Scanner(System.in);

String word;

String s;

String s1="";

String s2="";

char c;

System.out.println("Please enter a word:");

word=scanner.next();

s1=word;

for(int i=0;is1.length();i++)

s2=s2+"*";

for(int i=0;iword.length();i++){

System.out.println("Please enter a letter:");

s=scanner.next();

if(s.length()1){

System.out.println("Error!");

i--;

}else{

c=s.charAt(0);

int index=s1.indexOf(c);

if(index!=-1){

s2=s2.substring(0,index)+c+s2.substring(index+1);

s1=s1.substring(0,index)+"0"+s1.substring(index+1);

}

System.out.println(s2);

if(word.length()-i-10)

System.out.println("You have "+(word.length()-i-1)+" more tries");

}

}

if(s2.equals(word)){

System.out.println("You Win!");

}else

System.out.println("The answer is"+word+".You Lose!");

}

}

运行结果:

Please enter a word:

hello

Please enter a letter:

o

****o

You have 4 more tries

Please enter a letter:

h

h***o

You have 3 more tries

Please enter a letter:

l

h*l*o

You have 2 more tries

Please enter a letter:

l

h*llo

You have 1 more tries

Please enter a letter:

e

hello

You Win!

有问题就追问,满意请采纳。


新闻标题:java猜单词代码,java猜字母
当前URL:http://cqcxhl.cn/article/dsiccdi.html

其他资讯

在线咨询
服务热线
服务热线:028-86922220
TOP