java猜数游戏(产生1-100的随机数,5次没有猜中就退出,猜错了给予提示)

2025-01-19 19:19:21
推荐回答(4个)
回答1:

package cc.icoc.javaxu;

import java.io.IOException;
import java.util.Random;

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class GuessActivity extends Activity {
/** Called when the activity is first created. */
Button btnOK,btnClean,btnGet;
EditText input;
TextView outputInfo;
MediaPlayer media;

String getStr;
int getNum = 10;
int answer;
int count = 0;
final String TAG = "XU";

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.guess);
new Thread(new MyRunnable()).start();
System.out.println("运行");
//使用随机数来给answer变量赋值
Random random = new Random();
answer = random.nextInt(100);

input = (EditText)this.findViewById(R.id.input);
outputInfo = (TextView)this.findViewById(R.id.info);

btnOK = (Button)this.findViewById(R.id.btnClick);
btnGet = (Button)this.findViewById(R.id.btnGet);
btnClean = (Button)this.findViewById(R.id.btnClean);
btnOK.setOnClickListener(new ButtonEvent());
btnGet.setOnClickListener(new ButtonEvent());
btnClean.setOnClickListener(new ButtonEvent());

System.out.println("运行1");

}

public void getNumber() {
Random random = new Random();
answer = random.nextInt(100);
}

class ButtonEvent implements OnClickListener
{
public void onClick(View v) {
getStr = input.getText().toString();
System.out.println("getStr==="+getStr);
System.out.println("文本框当前值"+input.getText());
System.out.println("文本框当前值"+input.getText().toString().length()+"值:"+Integer.parseInt(getStr));

// 点击猜一下按钮的事件
if(v.getId() == R.id.btnClick)
{
System.out.println("文本框当前值"+input.getText().toString().trim().length());
getNum = Integer.parseInt(getStr);
if(input.getText().toString() == null)
{
Toast.makeText(GuessActivity.this, "你还没有输入数字", Toast.LENGTH_SHORT).show();
}
else
{
if(answer == getNum)
{
outputInfo.setText("");
outputInfo.setText(R.string.right);
Toast.makeText(GuessActivity.this, "你总共猜了 "+count+" 次", Toast.LENGTH_SHORT).show();
getNumber();
}

else if(answer > getNum)
{
outputInfo.setText("");
outputInfo.setText(R.string.tooSmall);
}

else if(answer < getNum)
{
outputInfo.setText("");
outputInfo.setText(R.string.tooBig);
}
}
count++;
}

if(v.getId() == R.id.btnGet)
{
outputInfo.setText("");
outputInfo.setText("答案是: "+answer);
getNumber();
}

//清除按钮的事件
if(v.getId() == R.id.btnClean)
{
input.setText("");
System.out.println("Count= "+count);
}

if(count == 6)
{
outputInfo.setText("");
outputInfo.setText(R.string.bendan);
}

if(count == 10)
{
outputInfo.setText("");
outputInfo.setText(R.string.bendan2);
}
}

}

class MyRunnable implements Runnable
{
public void run() {
System.out.println("MyRunnable"+Thread.currentThread().getId()+Thread.currentThread().getName());
// TODO Auto-generated method stub
System.out.println("运行2");

sound();
}

private void sound() {
// TODO Auto-generated method stub
media = new MediaPlayer();
if (media != null)
{
media.stop();
}
media.reset();
media = MediaPlayer.create(GuessActivity.this,R.raw.guess);

try {
media.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
media.start();
}

}
}

回答2:

你程序里 for循环之外 已经输入了一次,所以for循环改成: for (int i = 1; i < 5;i++) 就行,

我修改了下你的代码:

import java.util.Random;
import java.util.Scanner;

public class test9 {
public static void main(String[] args) {
// 2、猜数游戏(产生1-100的随机数,5次没有猜中就退出,猜错了给予提示)
System.out.println("请输入你猜的数字:");
Scanner sc = new Scanner(System.in);
Random rand = new Random();
int n = rand.nextInt(101);
System.out.println(n);
int c = 0;
while (sc.hasNextInt()) {
if (sc.nextInt() == n) {
System.out.println("恭喜你猜对了!");
break;
} else {
c++;
System.out.println("不好意思你猜错了...");
if(c == 5){
System.out.println("Game Over");
break;
}
}
}
sc.close();
}
}

回答3:

while循环条件改成i<5就成了,当i<=5的时候就会循环5次,再加上你前面还有一次输入,所以输错的情况下6次才会退出。

回答4:

for (int i = 1; i <=5;i++)

你把=去掉
成for (int i = 1; i <5;i++)就可以了

你前面已经输入了一次 又循环了5次 所以可以6次