0

我想问你如何在处理时制作它并再次执行此操作。我的意思是当程序做他的工作时它必须问:你要搜索其他词,是还是否?如果是程序将重新启动,如果没有退出。

我想想:

 int searchAgain = 0;
            System.out.println("Do you want search for other word?");
            System.out.println("1=Yes 2=No");
            searchAgain = input.nextInt();}
        else {
            System.out.println("Error...");
            }   
                if (searchAgain == 1) {
                    System.out.println("Write your word: ");
                    .....//I dont know what here

                }
                    else{
                        System.out.println("Program closed.");
                    }

这是我当前的代码:

    package lt.kvk.i3_2.kalasnikovas_stanislovas;

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


public class Miestai {


        public static void main(String args[]) throws Exception {
            String kitasStilius = "";
            while (!"Ne".equalsIgnoreCase(kitasStilius )) {
            Scanner input = new Scanner(System.in);
            System.out.println("Pasirinkite muzikos stiliu is saraso:");
            System.out.println();

            try {

                FileReader fr = new FileReader("src/lt/kvk/i3_2/kalasnikovas_stanislovas/Stiliai.txt");
                BufferedReader br = new BufferedReader(fr);
                String stiliuSarasas;
                while((stiliuSarasas = br.readLine()) != null) {
                    System.out.println(stiliuSarasas);
                }
                fr.close();
                String stilius = input.nextLine();   
                BufferedReader bf = new BufferedReader(new FileReader("src/lt/kvk/i3_2/kalasnikovas_stanislovas/Miestai.txt"));
                int counter = 0;                
                String line;

                System.out.println("Ieskoma informacija apie " + stilius);
                ArrayList<String> miestuSarasas = new ArrayList<String>();
                String miestas = null;

                while (( line = bf.readLine()) != null){
                    if (line.trim().length() == 0) miestas = null;
                       else if (miestas == null) miestas = line;
                       int indexfound = line.indexOf(stilius);
                       if (indexfound > -1) {
                       counter++;
                       miestuSarasas.add(miestas);
                       }                   
                }
                if (counter > 0) {
                    System.out.println(stilius + " turi " + counter + " remejus: " + miestuSarasas);                    
                    System.out.println("Ar norite ieskoti informacijos apie kita stiliu?");
                    System.out.println("Iveskite: Taip arba Ne");
                    kitasStilius = input.next();

                    }
                else {
                    System.out.println("Klaida, nerastas muzikos stilius");
                    break;
                    }   
                        if ("Taip".equalsIgnoreCase(kitasStilius)) {
                        }
                        else if ("Ne".equalsIgnoreCase(kitasStilius)) {
                            System.out.println("Darbas baigtas.");

                        }
                            else{
                                System.out.println("Neteisingai pasirinkta.");
                                break;
                            }
                        bf.close();
                        }

            catch (IOException e) {
                System.out.println("Error:" + e.toString());
            }
        }
        }
        private void searchWord(String textToSearch ){
            //your logic here which performs the search and prints the result
         }
     }

从 file.txt 计算单词(通过键盘输入)并选择谁重复了这个单词,例如:如果我输入单词:One它显示:

Word One repeated 3 times by John, Elisa, Albert

file.txt 看起来像:

John //first line - name
One
Three
Four

Peter //first line - name
Two
Three

Elisa //first line - name
One
Three

Albert //first line - name
One
Three
Four

Nicole //first line - name
Two
Four

谢谢你的回答。

4

1 回答 1

2

将您的代码(稍作修改)包装在一个while循环中,条件searchAgain为:

编辑:更新以接受选项作为字符串:

 public class Testing {

   public static void main(String args[]) throws Exception {
       Scanner input = new Scanner(System.in);
        System.out.println("Select word from list:");
        System.out.println();

        String searchAgain = "";
        while(!"No".equalsIgnoreCase(searchAgain )){
           System.out.println("Do you want search for other word?");
           System.out.println("Enter: Yes or No");
           searchAgain = input.next();
           if ("Yes".equalsIgnoreCase(searchAgain)) {
              System.out.println("Write your word: ");
              String textToSearch = input.next();
              searchWord(textToSearch );
           } else if ("No".equalsIgnoreCase(searchAgain)) {
                 System.out.println("Program closed.");
           } else{
                 System.out.println("Invalid Input.");
           }
        }
     }

     private void searchWord(String textToSearch ){
        //your logic here which performs the search and prints the result
     }
 }
于 2012-11-12T17:44:29.527 回答