Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/360.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
猜猜Java中的单词游戏_Java_String - Fatal编程技术网

猜猜Java中的单词游戏

猜猜Java中的单词游戏,java,string,Java,String,我在编写猜字游戏java代码时遇到问题。 我会给你看我当前的代码,直到你找到问题 import java.util.Scanner; class Q { public static void main(String argss[]) { String strs[] = { "kareem", "java", "izz", "tamtam", "anas" }; Scanner input = new Scanner(System.in);

我在编写猜字游戏java代码时遇到问题。 我会给你看我当前的代码,直到你找到问题

import java.util.Scanner;

class Q
{
    public static void main(String argss[])
    {
        String strs[] = { "kareem", "java", "izz", "tamtam", "anas" };
        Scanner input = new Scanner(System.in);
        int index = ((int) (Math.random() * 5));
        int points = 50;
        String c;
        boolean result = false;
        boolean finalResult = false;
        boolean tempResult = false;

        String word = strs[index];
        System.out.println(
                "\t *** Enter a character and guess the world*** \n\t ***You will loose if your points become 0 *** \n ");
        System.out.println(stars(word));
        System.out.println("Your points: " + points);
        System.out.print("Enter and guess a character! ");

        String temp = stars(word);
        String oldTemp = temp;

        c = input.next();
        while (points > 0)
        {

            for (int i = 0; i < word.length(); i++)
            {
                result = (word.charAt(i) + "").equals(c);
                if (result == true)
                {
                    temp = toChar(i, temp, c);
                }

            }

            finalResult = temp.equals(word);
            tempResult = temp.equals(oldTemp);
            System.out.println(temp);
            if (finalResult == true)
            {
                System.out.println("\n \n YOU HAVE GUESSED THE WORLD,YOU WON ! ");
                break;
            }
            if (tempResult == true)
            {
                points = points - 5;
                System.out.println("False and now your points are " + points);
            }

            else if (tempResult == false)
            {
                System.out.println("True and now your points are " + points);

            }
            if (points <= 0)
            {
                System.out.println("\n\n*********************\n* Sorry , you loose *\n*********************  ");
                break;
            }
            System.out.print("Guess another time,enter a character: ");
            c = input.next();
        }

    }

    public static String stars(String word)
    {
        String temp = "";
        for (int i = 0; i < word.length(); i++)
            temp = temp + "*";
        return temp;
    }

    public static String toChar(int index, String temp, String c)
    {
        char[] tempChar = temp.toCharArray();
        char s = c.charAt(0);
        tempChar[index] = s;
        temp = String.valueOf(tempChar);
        return temp;
    }
}
import java.util.Scanner;
Q类
{
公共静态void main(字符串argss[])
{
字符串strs[]={“kareem”、“java”、“izz”、“tamtam”、“anas”};
扫描仪输入=新扫描仪(System.in);
int索引=((int)(Math.random()*5));
积分=50;
字符串c;
布尔结果=假;
布尔finalResult=false;
布尔tempResult=false;
字符串字=字符串[索引];
System.out.println(
“\t***输入一个字符并猜测世界***\n\t***如果分数变为0,您将失去分数***\n”);
System.out.println(stars(word));
System.out.println(“您的积分:+积分”);
System.out.print(“输入并猜测一个字符!”);
字符串温度=星(字);
字符串oldTemp=temp;
c=输入。下一步();
而(点数>0)
{
for(int i=0;iif(points看起来不像是在while循环中更改
oldTemp
。在所有if语句之后,尝试将其设置为等于
temp

if (points <= 0)
{
    System.out.println("\n\n*********************\n* Sorry , you loose *\n*********************  ");
    break;
}
oldTemp = temp; // add this here
System.out.print("Guess another time,enter a character: ");
c = input.next();

if(points Um,哪一行是39?@bradimus tempResult=temp.equals(oldTemp);不幸的是,我的心灵感应现在不起作用,所以我无法分辨哪一行是39行。你能和我们分享一下吗?@vaibhavbaj'tempResult=temp.equals(oldTemp)“@Izzedineda”不清楚你的问题是什么。你能详细说明一下吗。你目前的结果是什么?你预期的结果是什么?你越能解释你的问题,你从这个社区得到的结果就越好。