我现在需要让我的程序输出这样的:正确while循环输出(爪哇)
输入一个单词:房子
你想要什么字母代替?:一个
有一个在房子里没有一个。
你想要替换什么字母?:b
没有内部。
你想替换什么字母?:e
你想用什么字母来代替它? w
新单词是housw。
_____________________________________。
这里是我的电流输出:
输入一个单词:房子
你想要什么字母代替?:一个
有一个在房子里没有一个
什么信你要替换?: b
你想用什么字母来代替它?
_____________________________________。
我遇到了麻烦,我的while循环。而我不知道如何纠正错误。这里是我的代码:
import java.util.Scanner;
public class WordScrambler {
//name
public static void main(String[] args) {
String word = "";
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter a word: " + word);
word = keyboard.nextLine();
String readChar = null;
System.out.print("What letter do you want to replace?: ");
readChar = keyboard.next();
while(true)
{
if(word.contains(readChar))
{
String changeChar;
System.out.print("With what letter do you wish to replace it? ");
changeChar = keyboard.next();
System.out.println(word.replace(readChar, changeChar));
}
else
{
System.out.print("There is no "+ readChar + " in " + word);
System.out.println();
System.out.print("What letter do you want to replace?: ");
readChar = keyboard.next();
String changeChar;
System.out.print("With what letter do you wish to replace it? ");
changeChar = keyboard.next();
System.out.println(word.replace(readChar, changeChar));
}
}
}
}
我们没有为你做功课。询问一个合适的问题。 – VeenarM 2014-10-28 21:19:06
你得到了什么错误。请问一个具体的问题 – Chiseled 2014-10-28 21:20:19
这不是while循环的问题。你可以标题你的问题有关吗? – khelwood 2014-10-28 21:20:28