Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
For循环不';t循环我的else语句(Java)?_Java_Eclipse - Fatal编程技术网

For循环不';t循环我的else语句(Java)?

For循环不';t循环我的else语句(Java)?,java,eclipse,Java,Eclipse,我正在编写一个简单的代码,它接受扫描仪读取的输入并保存到变量word。 赋值需要创建一个单独的方法,该方法将接受该字符串并将所有字母转换为“?”标记。然而,在空间上,应该有一个空间。 问题是每次我运行这段代码时,它都会在空格处停止 这是我的密码: import java.util.Scanner; public class commonPhrase { public static void main(String[] args){ Scanner input = new

我正在编写一个简单的代码,它接受扫描仪读取的输入并保存到变量
word
。 赋值需要创建一个单独的方法,该方法将接受该字符串并将所有字母转换为“?”标记。然而,在空间上,应该有一个空间。 问题是每次我运行这段代码时,它都会在空格处停止

这是我的密码:

import java.util.Scanner;

public class commonPhrase {
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        String word;
        System.out.print("Welcome to the Guessing Game.");
        System.out.println("");
        System.out.println("Please enter a common phrase");
        word = input.next();
        createTemplate(word);
    }
    public static String createTemplate(String word) {
        String sPhrase = "";
        for (int i=0; i < word.length(); i++) {
            if (word.charAt(i) == ' '){
                sPhrase += " ";
            }
            else {
                sPhrase += "?";                 
            }
        }
        System.out.println(sPhrase);
        return sPhrase;
    }
}
import java.util.Scanner;
公共类公共短语{
公共静态void main(字符串[]args){
扫描仪输入=新扫描仪(System.in);
字符串字;
System.out.print(“欢迎来到猜谜游戏”);
System.out.println(“”);
System.out.println(“请输入常用短语”);
word=input.next();
创建模板(word);
}
公共静态字符串createTemplate(字符串字){
字符串sPhrase=“”;
for(int i=0;i
下面是它的一个运行示例:

欢迎参加猜谜游戏。
请输入常用短语。
为什么它不增加空间

扫描器上调用
next()
,但该方法会获取下一个标记,默认情况下,它会用空格分隔标记。你只收集了一个词

调用
nextLine()
,获取整行的文本

word = input.nextLine();
使用上面的修复程序运行示例:

Welcome to the Guessing Game.
Please enter a common phrase
Stack Overflow
????? ????????
扫描器上调用
next()
,但该方法会获取下一个标记,默认情况下,它会用空格分隔标记。你只收集了一个词

调用
nextLine()
,获取整行的文本

word = input.nextLine();
使用上面的修复程序运行示例:

Welcome to the Guessing Game.
Please enter a common phrase
Stack Overflow
????? ????????

哇,我怎么会错过呢。谢谢你,苏MUCH@izzy因为你从来没有读过javadoc。哇,我怎么会错过呢。谢谢你,苏MUCH@izzy因为你从来没有读过javadoc。