2015-09-27 21 views
0

我使用循环在文本文件上生成1个数字。我开始在用户开始玩游戏时遇到问题。使用循环和条件来改变游戏中的分数

我要正前方去,给我的伪代码,以便你能明白我想做...

  1. 随机生成介于0和10号10
  2. 存储这些数据到一个名为mysteryNumbers.txt文件,每行一个数
  3. 提示用户介于0和10
  4. 检查文件
  5. 中如果用户猜测正确,subtra输入一个号码这个号码对第一号ct 10从分数开始(分数为0)
  6. 如果用户猜测错误,将正确数量的分数添加到用户的当前分数(在开始时,分数为0)
  7. 提示用户输入一个数字0这个数字之间和10
  8. 核对文件
  9. 如果用户猜测就在第二个数字,减去10从得分
  10. 如果用户猜测错误,添加正确的得分到当前用户分数
  11. 提示用户输入0到10之间的数字
  12. 核对文件
  13. 如果用户猜测正确的第三号这个号码,从比分
  14. 如果用户猜测错误减去10,成绩的正确数量添加到用户
  15. 目前比分
  16. ...等等...
  17. 显示用户的分数:当然,越低越好!

- 我也按照伪代码大纲来组织下面的代码。

/************************************************************************************ 
    PrintWriter mywriter = new PrintWriter("mysteryNumber.txt"); 
    int randomNumber; 
    int i = 0; 
    i=1; 
    while (i<=10) { 
    Random randomGen = new Random(); 
     randomNumber= randomGen.nextInt(11); 
    // then store (write) it in the file 
    mywriter.println(randomNumber); 
     i = i + 1; 
    } 
    //numbers are now generated onto text file... 

    /************************************************************************************ 
    * 3. Prompt the user to enter a number between 0 and 10       */ 
    Scanner myscnr= new Scanner (System.in); 

    System.out.print ("Please enter a number between 0 and 10: "); 

    int userNumber= myscnr.nextInt(); 

    /************************************************************************************ 
    * 4. Check this number against the first number in the file      */ 
    // to check the user's number against the file's, you need to be able to read 
    // the file. 
    FileReader fr = new FileReader("./mysteryNumber.txt"); 
    BufferedReader textReader = new BufferedReader(fr); 
    int numberInFile; 
    // the number in your file is as follows: 
    numberInFile = Integer.valueOf(textReader.readLine()); 

    /************************************************************************************ 
    * 5. If the user guesses right, subtract 10 from the score      */ 
    /************************************************************************************ 
    * 6. If the user guesses wrong, add the correct amount of score to the current * 
    * score of the user (at the start, the score is 0         */ 


    /************************************************************************************ 
    * 7. Prompt the user to enter a number between 0 and 10       */ 
    //Sytem.out.println ("Enter a number between 0"); 

    /************************************************************************************ 
    * 8. Check this number against the second number in the file      */ 

    etc etc... 

我很困惑从评论第5部分开始。我知道我必须创建一个新的整数分数,但之后,我迷路了! 我试图在if if statemnent工作,但我不能去它..

回答

0

我想你创建了10个随机数并存储在文件没有问题。

其余代码在array中有数字会更容易。你可以按顺序存储它们。接下来创建一个for循环来获取输入数字并检查数组中相应的数字。

例如:

考虑到您的数组编号[]包含您的数字。

for(int i=0;i<10,i++){ 
    System.out.print ("Please enter a number between 0 and 10: "); 
    int userNumber= myscnr.nextInt(); 

    if(userNumber==numbers[i]) 
     score-=10; 
    else 
     score++; //the addition you wanna make 
}