这是一个Text speak转换器,它可以工作,直到我希望它在用户说“完成”时结束。我会用什么命令?
package textspeak;
import java.util.Scanner;
public class TextSpeak {
byte textspeak;
public static void main(String[] args) {
String brb = new String("brb");
String omw = new String("omw");
String btw = new String("btw");
String lol = new String("lol");
String omg = new String("omg");
String rofl = new String("rofl");
String fyi = new String("fyi");
String ftw = new String("ftw");
String idk = new String("idk");
String jk = new String("jk");
String ikr = new String("ikr");
String smh = new String("smh");
String ttyl = new String("ttyl");
String rn = new String("rn");
String nvm = new String("nvm");
String ily = new String("ily");
String done = new String("done");
System.out.println("Hello! I convert 'text speak' into real english. Type in a commonly used shortcut for a word or phrase and I'll try to convert it. Type 'done' when you're done.");
Scanner scan = new Scanner(System.in);
String user_input = scan.nextLine();
while (!user_input.isEmpty()){ <<<<<<<<<<<<<<<<<What command do I need to put here?
user_input = scan.nextLine();
if (user_input.equals(brb)){
System.out.println("Be Right Back");}
if (user_input.equals(omw)){
System.out.println("On My way");}
if (user_input.equals(btw)){
System.out.println("By The Way");}
if (user_input.equals(lol)){
System.out.println("Laughing Out Loud");}
if (user_input.equals(omg)){
System.out.println("Oh My Gosh");}
if (user_input.equals(rofl)){
System.out.println("Rolling On the Floor Laughing");}
if (user_input.equals(fyi)){
System.out.println("For Your Information");}
if (user_input.equals(ftw)){
System.out.println("For The Win");}
if (user_input.equals(idk)){
System.out.println("I Don't Know");}
if (user_input.equals(jk)){
System.out.println("Just Kidding");}
if (user_input.equals(ikr)){
System.out.println("I Know, Right?");}
if (user_input.equals(smh)){
System.out.println("Shaking My Head");}
if (user_input.equals(ttyl)){
System.out.println("Talk To You Later");}
if (user_input.equals(rn)){
System.out.println("Right Now");}
if (user_input.equals(nvm)){
System.out.println("Nevermind");}
if (user_input.equals(ily)){
System.out.println("I Love You");}
}
}//end of while loop
}
答案 0 :(得分:6)
尝试做:
while (!user_input.equals("done"))
或者Jay表示你可以在循环中break
:
if (user_input.equals("done")) {
break;
}
这会让你走出循环。例如:
while (true) {
break;
System.out.println("Inside");
}
System.out.println("Outside");
只打印:
外
答案 1 :(得分:2)
虽然Anubian Noobs有意义,但你也可以这样做:
if (user_input.equals("Done")) break;
答案 2 :(得分:0)
我强烈建议您使用Map<String, String>
来存储您的首字母缩略词。这样,您可以将代码简化为类似的内容,
public static void main(String[] args) {
Map<String, String> acronyms = new HashMap<>();
acronyms.put("brb", "Be Right Back");
acronyms.put("omw", "On My way");
acronyms.put("btw", "By The Way");
acronyms.put("lol", "Laughing Out Loud");
acronyms.put("omg", "Oh My Gosh");
acronyms.put("rofl", "Rolling On the Floor Laughing");
acronyms.put("fyi", "For Your Information");
acronyms.put("ftw", "For The Win");
acronyms.put("idk", "I Don't Know");
acronyms.put("jk", "Just Kidding");
acronyms.put("ikr", "I Know, Right?");
acronyms.put("smh", "Shaking My Head");
acronyms.put("ttyl", "Talk To You Later");
acronyms.put("rn", "Right Now");
acronyms.put("nvm", "Nevermind");
acronyms.put("ily", "I Love You");
acronyms.put("done", "Goodbye");
System.out.println("Hello! I convert 'text speak' into real english. Type "
+ "in a commonly used shortcut for a word or phrase "
+ "and I'll try to convert it. Type 'done' when you're done.");
Scanner scan = new Scanner(System.in);
if (scan.hasNextLine()) {
String line = null;
do {
line = scan.nextLine().trim();
String str = acronyms.get(line);
System.out.println((str != null) ? str : "Unknown: " + line);
} while (!line.equalsIgnoreCase("done") && scan.hasNextLine());
}
}