首页 > 解决方案 > 为 Java 调试一个简单的控制台游戏

问题描述

import java.util.Scanner;
public class Project3 {
    public static void main(String[] args) {
        Scanner keys = new Scanner(System.in); 

System.out.println("Welcome to mini mad libs!"); // word1-word3 are inputs that point out which words from the story need to be replaced. 
System.out.printf("Please enter the story: ");
String story = keys.nextLine();
System.out.printf("Please enter the first word type that should be replaced:");
String word1 = keys.nextLine();
System.out.printf("Please enter the second word type that should be replaced:");
String word2 = keys.nextLine();
System.out.printf("Please enter the third word type that should be replaced:");
String word3 = keys.nextLine();
System.out.println();


System.out.println("Ok, the game is ready to play!"); //the replace strings are the new words that are replacing the original words in the story. 
System.out.println("Please enter a word type to replace "+word1);
String replace1 = keys.nextLine();
System.out.println("Please enter a word type to replace "+word2);
String replace2 = keys.nextLine();
System.out.println("Please enter a word to replace "+word3);
String replace3 = keys.nextLine();


String storyV2 = story.toLowerCase();
String word1V2 = word1.toLowerCase();
String word2V2 = word2.toLowerCase();
String word3V2 = word3.toLowerCase();


storyV2=storyV2.replaceAll("[.,!]", " "); 
int positionOf1= storyV2.indexOf(" "+word1V2+" ");
int positionOf2= storyV2.indexOf(" "+word2V2+" ");
int positionOf3= storyV2.indexOf(" "+word3V2+" "); 

int length1 = word1.length();
int length2 = word2.length();
int length3 = word3.length();

String WordMod1 = story.substring(positionOf1,positionOf1+length1);
String WordMod2 = story.substring(positionOf2,positionOf2+length2);
String WordMod3 = story.substring(positionOf3,positionOf3+length3); 

String lib = story.replaceFirst(WordMod1, replace1); //lib serves as a string that has a version of the original story replaced by the three words one by one in the next lines below. 
lib = lib.replaceFirst(WordMod2, replace2);
lib = lib.replaceFirst(WordMod3, replace3);
System.out.println();
System.out.println("Here is your little mad lib: \n"+ lib); 
    }
}

Mad libz 是一款游戏,可以将句子中的选定单词替换为您选择的其他单词。我不能使用 if/else 语句、循环或任何不是字符串方法的东西。我的问题似乎出在这部分代码中。我对 Java 不太熟悉,所以它可能看起来很糟糕。

String WordMod1 = story.substring(positionOf1,positionOf1+length1);
String WordMod2 = story.substring(positionOf2,positionOf2+length2);
String WordMod3 = story.substring(positionOf3,positionOf3+length3); 

这部分正在制作一个获取句子中单词的子字符串,例如,如果我想要单词“名词”,它会在句子中的任何位置查找独立单词,而不是可能从“代词”或“发音”等其他单词中获取单词”。PositionOf1 查找空格之间的位置,lenghtOf1 是我们要替换的原始单词的长度。

这就是为什么这也应该是不区分大小写的,所以这就是我制作字符串storyV2的原因,它是原始设置为小写的副本。

标签: java

解决方案


如果您只想用另一个字符串替换一个字符串,那么为什么不使用 replaceAll() 方法呢?

String story = "...";
String fromWord = "foo";
String toWord = "bar";
String newStory = story.replaceAll(" " + foo + " ", " " + bar + " ");

您可以使用更精细的正则表达式模式来查找 foo 不仅由空格包围,而且还包括各种非单词字符,因此您不需要删除诸如 .,; 之类的字符。等等,就像你现在做的那样。


推荐阅读