如果声明不起作用

时间:2013-12-04 23:44:04

标签: java if-statement

好吧我有一个if语句,if else在里面,但if else有一个if语句,因为我需要一张图片只加载如果这个单词是从txt文件加载但是if语句里面加载如果否则它不加载文本文件

import java.util.*;
import java.io.*;
 /**
  * Write a description of class Application here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
 public class Application
{
private WordList words;
private Scanner input;

    public Application(){
    input=new Scanner(System.in);
    words=new WordList();
}
 public void runApplication() throws IOException {  
    String response;
    String p,e;
    do {
        printMenu();          
        response=input.nextLine();

           if (response.equals("1")) {
            words.load("pirate.txt");
        }
          else if (response.equals("2")) {
            System.out.print("enter name to search for: "); 
            p=input.nextLine();   
            words.searchByNameAndPrint(p);

            if(response.equals("scoundrel"));
    {
            TheSimpleFrame frame=new TheSimpleFrame();
            frame.display("f.jpg");
            frame.setVisible(true); 
        }

        }
        else if (response.equals("3")) {   
            System.out.print("enter name to search for: "); 
            e=input.nextLine();   
            words.searchByNameAndPrintenglish(e);
        }
        else if (response.equals("4")) {
            System.out.print("Random Pirate name: "); 
            Random random  = new Random();
            int rgen = random.nextInt(10)+1;
            //System.out.println(rgen); //print random to test     
            String [] arrayList = new String [10];
            arrayList[1] = "Giorgio Adorno";
            arrayList[2] = "James Alday";
            arrayList[3] = "William Aleyn";
            arrayList[4] = "Richard Allen";
            arrayList[5] = "Jean Ango";
            arrayList[6] = "Aruj";
            arrayList[7] = "Awilda";
            arrayList[8] = "Hayreddin Barbarossa";
            arrayList[9] = "Baldassare Cossa";
            System.out.println(arrayList[rgen]);
     }else if (response.equals("5")) {
           System.out.print("enter name to search for: "); 
                           e=input.nextLine();   
            words.searchByNameAndPrintenglishh(e);
            String s1 =     e.replace("hello","ahoy").replace("women","beauty").replace("treasure","booty").replace("food","is into pirates");
         System.out.println("Trolled = " + s1);
        }
                    else if (response.equals("6")) {
            System.out.print("enter name to search for: "); 
            p=input.nextLine();   
            words.searchByNameAndPrintt(p);
            String s2 = p.replace("ahoy","sir").replace("beauty","bangtidy!").replace("booty","ass").replace("grog","alcohol");
            System.out.println("Trolled = " + s2);

        }
        else if (response.equals("7")) {
            System.out.print("enter pirate phrase: "); 
            p=input.nextLine();   
            e=input.nextLine();   
            Word temp=new Word(p,e);
            words.addWord(temp);
        }

        else if (response.equals("8")) {
            words.save("pirate.txt");
        }
        else if (response.equals("9")) {
            words.print();
        }
        else if (response.equals("10"))
                    {
         String anwser = "";
         for(int i=0; i<3;i++){
             Random random  = new Random();
             int which=random.nextInt(words.size());
             anwser+=(words.getWord(which)).getName();
            }       
            System.out.print(anwser);
            input.nextLine(); 
        }
        else if (response.equals("11")) {


        }
        else if (response.equals("Q")|| (response.equals("q"))) {
            break;  //out of loop
        }
        else {    
            System.out.println("sorry - enter again");
        }


    }while (!((response.equals("Q")|| (response.equals("q")))));
    System.out.println("THank you for using the pirate list");
}
//////////////////////////////////////////////////////helper methods
private void printMenu(){
    System.out.println("What would you like to do:");
    System.out.println("1 - load a phrase");
    System.out.println("2 - get a phrase pirate to english");
    System.out.println("3 - get a phrase english to pirate");
    System.out.println("4 - Generate a pirate name");
    System.out.println("5 - type a english sentence");
    System.out.println("6 - type a pirate sentence");
    System.out.println("7 - create new phrase and add");
    System.out.println("8 - save the phrase");                     
    System.out.println("9 - see the situation");
    System.out.println("10 - randomly generate 3 pirate phrases");
    System.out.println("Q - quit");                

}

}

1 个答案:

答案 0 :(得分:2)

我认为你指的是这一行:

if(response.equals("scoundrel"));

它在行的末尾有一个分号,它作为if语句的主体,因此总是执行下面括号中的块。删除分号:

if(response.equals("scoundrel"))