数组问题和麻烦的方法 [英] array problems and method trouble

查看:111
本文介绍了数组问题和麻烦的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图把astrisks到位我随机单词。我已经打印出我的随机字,所以你都可以看到正在发生的事情。

 进口的java.io.File;
进口的java.util。*;公共类Lab12
{
    公共静态最终诠释MAXWORD = 15000;
    公共静态最终诠释MAXCHAR = 20;
    公共静态最终诠释MAXGUESS = 8;公共静态无效的主要(字串[] args)
{
    INT COUT = 0;
    存储();
}公共静态INT pickrandom(诠释计数)
{
    随机数发生器=新的随机();
    返回generator.nextInt(计数);
}公共静态int型存储()
{
    串词表[] =新的String [MAXWORD]
    字符串wordLetter [] =新的String [MAXCHAR]
    字符串字典=dictionary.txt;
    扫描仪readFileIn = NULL;
    串dictionaryVal =;
    诠释计数= 0;
    字符串CONT =;    尝试//尝试读取文件。
    {
        readFileIn =新的扫描仪(新文件(字典));        而(readFileIn.hasNextLine())
        {
            单词表[计] = readFileIn.nextLine();            的System.out.println(+单词表[计数]);
            算上++;
        }        的System.out.println();
        的System.out.println(H A N G - M A N);
        的System.out.println();
        的System.out.println(这是猜谜游戏一个字将随机选择一个字);
        的System.out.println(和存隐忧你会揣摩的密语。);
        的System.out.println(猜你认为是在单词字母,你会猜测。);
        的System.out.println(每次一个字母。如果你猜这封信是正确的,。);
        的System.out.println(中的保密字字母的位置(S)将被显示。);
        的System.out.println(你将被允许8错误的猜测。如果你猜不正确的8。);
        的System.out.println(时代,你输了比赛。如果你猜所有的信件中。);
        的System.out.println(这个词,你赢了。);
        的System.out.println();        //System.out.println(\"Hit进入到contiune:); // CONT = readFileIn.readLine();        INT兰特= pickrandom(计数);
        的System.out.println(+兰特);
        的System.out.println(字在此位置:+单词表[兰德]);
        字符串hiddenWord =单词表[兰德]        clearScreen(续)
        hideWord(hiddenWord);
        的System.out.println(+ hideWord(hiddenWord));
    }
    赶上(例外五)// catch错误试图打开时/查找文件dictionary.txt
    {
        e.printStackTrace();
        的System.out.println(错误无法打开dictionary.txt);
    }
    返回计数;
}公共静态无效clearScreen(字符串续)
{
    扫描仪键盘=新的扫描仪(System.in);
    的System.out.println(preSS回车键继续:);
    CONT = keyboard.nextLine();
    如果(cont.equals())
    {
        的for(int i = 0; I< 100;我++)
        {
            的System.out.println();
        }
    }
}公共静态的String [] hideWord(字符串hiddenWord)
{
    INT字长= hiddenWord.length();
    INT长度=字长* 2;
    的String [] =星号新的String [长度]    的for(int i = 0; I<长度;我+ +)
    {
        如果(我%2 == 0)
        {
            星号[I] =*;
        }
        其他
        {
            星号[I] =;
        }
    }
    返回星号;
}


解决方案

  hideWord(hiddenWord);

你为什么叫这一点,再次打电话了吗?

 的System.out.println(+ hideWord(hiddenWord));

顺便说一句,请记住,你会得到一个数组,而不是一个字符串,错误,你说你是不是一个错误,是一个数组的值调用toString方法时,当您使用串联它的的println 方法,你必须打印数组的内容不是该数组本身。

 的System.out.println(Arrays.toString(hideWord(hiddenWord)));

这应该工作,请记住,并不是要打印的阵列相同的比来打印所述阵列的内容...

I'm trying to put astrisks in place of my random word. I have printed out my random word so you all can see what is going on.

import java.io.File;
import java.util.*;

public class Lab12
{
    public static final int MAXWORD = 15000;
    public static final int MAXCHAR = 20;
    public static final int MAXGUESS = 8;

public static void main(String[] args)
{ 
    int cout = 0;
    storage();
}

public static int pickrandom(int count)
{
    Random generator = new Random();
    return generator.nextInt(count);
}

public static int storage()
{
    String wordList[] = new String[MAXWORD];
    String wordLetter[] = new String[MAXCHAR];
    String dictionary = "dictionary.txt";
    Scanner readFileIn = null;
    String dictionaryVal = " ";
    int count = 0;
    String cont = "";

    try//Try to read in the file.
    {
        readFileIn = new Scanner(new File(dictionary));

        while(readFileIn.hasNextLine())
        {
            wordList[count]= readFileIn.nextLine();

            System.out.println(""+wordList[count]);
            count++;
        }

        System.out.println("");
        System.out.println("H A N G M A N");
        System.out.println("");
        System.out.println("This is a word guessing game A word will be selected at random");
        System.out.println ("and kept hidden. You will try to figure out the secret word by");
        System.out.println("guessing letters which you think are in the word. You will guess");
        System.out.println("one letter at a time. If the letter you guess is correct, the");
        System.out.println("position(s) of the letter in the secret word will be shown.");
        System.out.println("You will be allowed 8 wrong guesses. If you guess incorrectly 8");
        System.out.println("times, you lose the game. If you guess all of the letters in the");
        System.out.println("word, you win.");
        System.out.println("");

        //System.out.println("Hit enter to contiune:");      //cont = readFileIn.readLine();

        int rand = pickrandom(count);
        System.out.println(""+rand);
        System.out.println("Word at this position: "+wordList[rand]);
        String hiddenWord = wordList[rand];

        clearScreen(cont);
        hideWord(hiddenWord);
        System.out.println(""+hideWord(hiddenWord));
    }
    catch(Exception e)//Catch error when trying to open/find the file dictionary.txt
    {
        e.printStackTrace();
        System.out.println("Error can not open dictionary.txt");
    }
    return count;
}

public static void clearScreen(String cont)
{
    Scanner keyboard = new Scanner(System.in);
    System.out.println("Press enter to continue:");
    cont = keyboard.nextLine();
    if(cont.equals(""))
    {
        for(int i=0; i<100; i++)
        {
            System.out.println("");
        }
    }
}

public static String[] hideWord(String hiddenWord)
{     
    int wordLength = hiddenWord.length();
    int length = wordLength * 2;
    String[] asterisks = new String[length];

    for(int i=0; i < length; i++)
    {
        if(i % 2 == 0)
        {
            asterisks[i] = "*";
        }
        else
        {
            asterisks[i] = " ";
        }
    }
    return asterisks;
}

解决方案

 hideWord(hiddenWord);

Why do you call this and call it again on?

 System.out.println(""+hideWord(hiddenWord));

By the way, remember that you get an array, not a string, that "error" that you say you get is not an error, is the value of an array when calling the method toString when you concatenate it with the "" in the println method you have to print the content of the array not the array itself.

System.out.println(Arrays.toString(hideWord(hiddenWord)));

that should work, remember that is not the same to print an array than to print the content of the array...

这篇关于数组问题和麻烦的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆