程序输出来自多种方法的代码,而不仅仅是一种方法

时间:2016-10-27 13:30:25

标签: java methods

我有一个Java程序可以解决手机的常见问题。为此,我设置了一个扫描程序,用于读取任何关键字的用户输入。如果找到其中一个关键字,则方法将从文本文件中输出该关键字建议的问题的解决方案。

我的问题是,当我运行程序时,从每个方法输出文本文件中的所有行,忽略我的输入。

以下是代码:

    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.Scanner;

    public class task2 {

public static void main(String[] args) {

    Scanner scan = new Scanner(System.in);
    System.out.println("What is your problem?");
    String input =  scan.nextLine();
    String[] problems = {"screen", "display", "broken", "cracked", "camera", "flash", "ports"};
    String[] solutions = input.split("broken");

    for(int x=0; x < problems.length; x++){
        if(input.contains("broken")){
            if(input.contains("screen")){
                brokenScreen();
            } else{

            }
            if(input.contains("display")) {
                brokenDisplay();
            } else{

            }
            if(input.contains("camera")) {
                brokenCamera();
            } else{

            }
            if(input.contains("flash")) {
                brokenFlash();
            } else{

            }
            if(input.contains("ports")) {
                brokenPorts();
            } else{

            }
        }
            else{

            }

        if(input.contains("cracked")) {
            if(input.contains("screen")) {
                crackedScreen();
            } else{

            }
        }
        if(input.contains("water")) {
            waterPhone();
        }
        else{
        }
    }

    brokenScreen();
    brokenDisplay();
    crackedScreen();
    brokenCamera();
    brokenFlash();
    brokenPorts();
    waterPhone();
    noSolution();

    }

public static void noSolution() {
    String file = "C:/Users/Nicholas Gawley/workspace/Second Practice Controlled Assessment/src/solutions.txt"; //Location of the text file
    try {
        FileReader filereader = new FileReader(file);
        BufferedReader bufferedreader = new BufferedReader(filereader);
        while((file = bufferedreader.readLine()) != null){
            System.out.println(file);
    }
        bufferedreader.close();
        } catch (IOException e) {
        e.printStackTrace();
    }

}
public static void waterPhone() {
    String file = "C:/Users/Nicholas Gawley/workspace/Second Practice Controlled Assessment/src/solutions.txt";     try {
        FileReader filereader = new FileReader(file);
        BufferedReader bufferedreader = new BufferedReader(filereader);
        while((file = bufferedreader.readLine()) != null){
            System.out.println(file);
    }
        bufferedreader.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
public static void brokenPorts() {
    String file = "C:/Users/Nicholas Gawley/workspace/Second Practice Controlled Assessment/src/solutions.txt";     try {
        FileReader filereader = new FileReader(file);
        BufferedReader bufferedreader = new BufferedReader(filereader);
        while((file = bufferedreader.readLine()) != null){
            System.out.println(file);
    }
        bufferedreader.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

}
public static void brokenFlash() {
    String file = "C:/Users/Nicholas Gawley/workspace/Second Practice Controlled Assessment/src/solutions.txt";     try {
        FileReader filereader = new FileReader(file);
        BufferedReader bufferedreader = new BufferedReader(filereader);
        while((file = bufferedreader.readLine()) != null){
            System.out.println(file);

    }
        bufferedreader.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

}
public static void brokenCamera() {
    String file = "C:/Users/Nicholas Gawley/workspace/Second Practice Controlled Assessment/src/solutions.txt";     try {
        FileReader filereader = new FileReader(file);
        BufferedReader bufferedreader = new BufferedReader(filereader);
        while((file = bufferedreader.readLine()) != null){
            System.out.println(file);
    }
        bufferedreader.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

}
public static void crackedScreen() {
    String file = "C:/Users/Nicholas Gawley/workspace/Second Practice Controlled Assessment/src/solutions.txt";     try {
        FileReader filereader = new FileReader(file);
        BufferedReader bufferedreader = new BufferedReader(filereader);
        while((file = bufferedreader.readLine()) != null){
            System.out.println(file);
    }
        bufferedreader.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

}
public static void brokenDisplay() {
    String file = "C:/Users/Nicholas Gawley/workspace/Second Practice Controlled Assessment/src/solutions.txt";     try {
        FileReader filereader = new FileReader(file);
        BufferedReader bufferedreader = new BufferedReader(filereader);
        while((file = bufferedreader.readLine()) != null){
            System.out.println(file);
    }
        bufferedreader.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
public static void brokenScreen() {
    String file = "C:/Users/Nicholas Gawley/workspace/Second Practice Controlled Assessment/src/solutions.txt";     try {
        FileReader filereader = new FileReader(file);
        BufferedReader bufferedreader = new BufferedReader(filereader);
        while((file = bufferedreader.readLine()) != null){
            System.out.println(file);
    }
        bufferedreader.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

}
}

任何人都可以解决这个问题吗?任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

删除

brokenScreen();
brokenDisplay();
crackedScreen();
brokenCamera();
brokenFlash();
brokenPorts();
waterPhone();
noSolution();

main方法的最后(for循环之后)。