使用用户输入运行循环时保持读入文件的Java代码

时间:2014-11-30 16:11:57

标签: java file-io while-loop

所以我对java有点新意,我需要一些帮助来编程。 我正在构建一个允许用户输入颜色(字符串)和运行while循环以在文件中查找该颜色的程序。如果文件中存在颜色,则用户将获得积分。在文本文件中,我添加了带整数的纵梁,因此字符串是颜色,它们旁边的整数是它们值得的点。

文本文件Ex: 粉红色:PINK:30 黄:黄色:20

这是代码

import java.io.*;
import java.util.Scanner;

public class MainGame {
    public static void main (String[] args) throws Exception{

Scanner filechecker = new Scanner(new File("FILE_NAME"));

        int val=0;
        int gamePoints=0;
        int noStrikes=3;
        int strikes=0;

        Scanner Color1 = new Scanner(System.in);
        System.out.println("Enter A color");
        String ColorTaken = Color1.next();

        while (noStrikes>strikes){//added this while loop to keep the loop going if I take this out the program works perfectly but for only for one color search


        while (filechecker.hasNextLine())//checks if color exists in file
        {
        String line =filechecker.nextLine();
        String[] details = line.split(":");//checks for the integer next to the color
        if (line.indexOf(ColorTaken)!=-1)
        {   
            int points = Integer.parseInt(details[2]);// converts the string to int
            System.out.println(""+ColorTaken+" Exists! You Got "+details[2]+" Points ");
            val = 1;//makes statement true
            gamePoints=points;// still working on this not sure how it will work later on but its for the points earned
            noStrikes=3;
        }

        if (noStrikes==3){ //Part of the add-on
             Scanner Color2 = new Scanner(System.in);
             System.out.println("Enter A color");
             String ColorTaken2 = Color2.next();

             while (filechecker.hasNextLine())//checks if color exists in file
             {
             String line2 =filechecker.nextLine();
             String[] details2 = line2.split(":");//checks for the integer next to the color
             if (line2.indexOf(ColorTaken2)!=-1)
             {   
                int points = Integer.parseInt(details2[2]);// converts the string to int
                System.out.println(""+ColorTaken2+" Exists! You Got "+details2[2]+" Points ");
                val = 1;//makes statement true
                 gamePoints=points;// still working on this not sure how it will work later on but its for the points earned

             }
             }
        }




        else
        {
            val = 0; //makes statement false
            continue;

        }
    if(val == 0)
    {

        System.out.println("You Have a Strike! Next Person Must Answer");
        strikes++;
    }
        }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

尝试更好地整理代码......

while(/*condition for running the game = true*/){
    Color color = askForInput();
    boolean fileHasColor = checkTheFile(color);
    //do something based on the true/false value
}

然后创建输入方法......

private Color askForInput(){
    //code for getting an input from the player
    Color colorToReturn = new Color(/*based on player input*/);
    return colorToReturn;
}

最后检查文件中的颜色......

private boolean checkTheFile(Color color){
    //code to search the file for color
    while(filechecker.hasNextLine()){
        //compare the colors
        if(/*colors match*/) return true;
    }
return false; //this line is reached if no colors match
}

通过这种方式,您可以将代码分成功能部分,并且可以更轻松地查看正在发生的事情......