我花了几个小时试图弄清楚如何编写这段代码,但它行不通。它正在运行特定的字母,但现在开头的两个提示甚至没有显示。作业是提示用户写一个句子,然后让他们选择一个字母,然后编写一个程序来显示所选字母的位置。 这是我的代码:
if (exercise.equals("4c") || exercise.equals("0")){
System.out.println("(problem 4c) Character Location");
System.out.println("Enter a line of text: ");
Scanner reader = new Scanner(System.in);
String lineOfText = reader.nextLine();
System.out.println("Specify a character to be found: ");
Scanner choice = new Scanner(System.in);
String theLetter = choice.nextLine();
char x = theLetter.charAt(0);
for (int z = 0; z < lineOfText.length(); z++)
if (lineOfText.charAt(z) == 'x')
System.out.println(z);`
`
最佳答案
可能与这部分代码if (lineOfText.charAt(z) == 'x')
有关。我认为您的意思是 'x'
实际上是变量 x
而不是字符。
至于未显示的提示,可能与创建 exercise
变量的代码相关。
关于java - 用于在用户输入的句子中查找所选字母(由用户)的位置的代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42333140/