Java 如果语句不起作用
好的,我有一个do-while语句,里面有if-else,但是if-else里面有if语句,因为我需要一个图片来加载这个单词,如果这个单词是从txt文件加载的,但是if-else里面有if语句,那么它不加载文本文件Java 如果语句不起作用,java,if-statement,Java,If Statement,好的,我有一个do-while语句,里面有if-else,但是if-else里面有if语句,因为我需要一个图片来加载这个单词,如果这个单词是从txt文件加载的,但是if-else里面有if语句,那么它不加载文本文件 import java.util.*; import java.io.*; /** * Write a description of class Application here. * * @author (your name) * @version (a versi
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");
}
}我想你指的是这一行:
if(response.equals("scoundrel"));
它在作为if语句主体的行的末尾有一个分号,因此总是执行下面大括号中的块。删除分号:
if(response.equals("scoundrel"))
-1表示if语句不起作用。你真的认为if/else对你不起作用吗?孩子们。另外,请格式化你的代码。这不是为了我们的利益;这是给你的。你只要仔细检查并清理一下,就可以发现很多虫子。