1
因此,爲了測試我迄今爲止學到的Java的一些小挑戰,我決定製作一個程序,將webcomic(來自Heartstuck的Sol Captor,對那些好奇的人)的角色的寫作怪癖應用到用戶的使用switch語句輸入。到目前爲止,程序工作正常,怪癖得到很好的應用,但是當我去做程序時詢問用戶他們是否想要再次運行程序,然後說是的時候,程序會再次運行,除非程序沒有運行, t暫停以允許用戶鍵入另一個語句以應用古怪。我試圖把所有的聲明,除了焦炭重新啓動,在while循環內,並添加行「sol.Next();」在「System.out.println(」Normal「);」之後「這些都沒有工作,我無法找到任何關於如何解決這個問題的任何信息。這裏是我的代碼供參考:如何讓掃描儀在再次運行程序時暫停輸入?
import java.io.IOException;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
public class SolQuirk {
public static void main(String[] args) {
char restart = 'y';
Scanner sol = new Scanner(System.in).useDelimiter("");
String input = "";
while(restart == 'y' | restart == 'n'){
System.out.println("Normal:");
while(!sol.hasNext("\n")){
char ch = sol.next().charAt(0);
switch(ch){
case 's': case 'S':
input += '2';
break;
case ' ':
input += ch;
ch = sol.next().charAt(0);
if(ch == 't' || ch == 'T'){
ch = sol.next().charAt(0);
if(ch == 'o' || ch == 'O'){
ch = sol.next().charAt(0);
if(ch == 'o' || ch == 'O'){
ch = sol.next().charAt(0);
if(ch == ' ' || ch == '.'){
input += "two";
input += ch;
break;
}
else{
input += "too";
input += ch;
break;
}
}
else if(ch == ' ' || ch == '.'){
input += "two";
input += ch;
break;
}
else{
input += "to";
input += ch;
break;
}
}
else{
input += "t";
input += ch;
break;
}
}
default:
input += ch;
break;
}
}
String solQuirk = input.toLowerCase();
System.out.println("Sol:");
System.out.println(solQuirk);
System.out.println("Would you like to go again? y/n");
try {
restart = (char) System.in.read();
} catch (IOException ex) {
Logger.getLogger(SolQuirk.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
在此先感謝您。
最美麗的縮進,我見過! – 2013-05-11 21:22:50
厄運的金字塔! – 2013-05-11 21:28:39
你如何編輯和編譯你的代碼?如果您使用的是NetBeans或Eclipse等IDE,則應使用調試器來遍歷代碼,以瞭解發生的情況。或者,您可以將'System.out.println()'調用添加到代碼中,以便跟蹤變量的執行和查看值。 – 2013-05-11 21:29:08