Java关键字平均位置
可能重复:Java关键字平均位置,java,loops,Java,Loops,可能重复: 我试图在字符串中找到用户输入的关键字的平均起始位置。我尝试过使用indexOf(),但似乎无法正确初始化它。有人能帮我吗 我正在寻找的一个输出示例是:平均起始位置为4.5 import java.util.Scanner; public class Lab6Loops { public static void main(String[] args) { String keywordString; String inputString;
我试图在字符串中找到用户输入的关键字的平均起始位置。我尝试过使用indexOf(),但似乎无法正确初始化它。有人能帮我吗 我正在寻找的一个输出示例是:平均起始位置为4.5
import java.util.Scanner;
public class Lab6Loops {
public static void main(String[] args) {
String keywordString;
String inputString;
Scanner keyboard = new Scanner (System.in);
int numofSentences = 0;
int numofKeyword = 0;
System.out.println ("Enter a keyword. We will search each sentence for this word.");
keywordString = keyboard.nextLine ();
System.out.println ("Please enter a sentence or type 'stop' to finish");
inputString = keyboard.nextLine ();
while( !inputString.equals ("stop"))
{
if(inputString.contains (inputString));
numofSentences = numofSentences + 1;
if(inputString.contains (keywordString));
numofKeyword = numofKeyword + 1;
System.out.println ("Enter a line of text or 'stop' to finish");
inputString = keyboard.nextLine();
}
System.out.println ("You entered " + numofSentences + " sentences");
System.out.println ("You have " + numofKeyword + "sentences that contain the keyword");
}
}
while
循环中的2if
s有一个代码>在末尾。您应该删除它们,然后再次尝试运行该程序。Scanner类不会为您提供一种方法来知道该模式在句子中出现的位置。您必须使用Pattern和Matcher类来找出位置。因为这是你的家庭作业,我不会给你代码,而是给你一个如何使用模式的例子
import java.util.regex.*;
class Regex {
public static void main(String [] args) {
Pattern p = Pattern.compile(args[0]);
Matcher m = p.matcher(args[1]);
System.out.println("Pattern is " + m.pattern());
while(m.find()) {
System.out.println(m.start() + " " + m.group());
}
}
}
此程序使用第一个命令行参数(args[0])来表示
要使用的正则表达式,它使用第二个参数(args[1])来
表示要搜索的源数据。下面是一个测试运行:
java正则表达式“56”“ab4 567ab”
生成输出:
Pattern is 56
4 56
提示:我们用(所有起始位置的相加)除以(包含关键字的句子总数)来计算平均起始位置
以下是您的代码:
import java.util.Scanner;
/**
* Program to calculate the average starting position of a given keyword in user input sentences.
* We calculate average starting position by dividing (addition of all starting positions) by (total number of sentences containing keyword)
* @author Garbage
*
*/
public class Lab6Loops {
public static void main(String[] args) {
String keywordString;
String inputString;
Scanner keyboard = new Scanner(System.in);
int totalSentences = 0;
int interestingSentences = 0;
int positionTotal = 0;
System.out.print("Enter a keyword. We will search each sentence for this word. : ");
keywordString = keyboard.nextLine();
System.out.println("Please enter a sentence or type 'stop' to finish");
inputString = keyboard.nextLine();
while (!inputString.equals("stop")) {
totalSentences = totalSentences + 1; //totalSentences++
// Check if the sentence contains our keyword
if (inputString.contains(keywordString)){
interestingSentences = interestingSentences + 1; // This is sentence we would like to check
positionTotal = positionTotal + inputString.indexOf(keywordString); // Add the starting position to our total
}
System.out.println("Enter a line of text or 'stop' to finish");
inputString = keyboard.nextLine();
}
System.out.println("You entered " + totalSentences + " sentences");
System.out.println("You have " + interestingSentences + "sentences that contain the keyword "+keywordString);
System.out.println("The average starting position is "+(positionTotal / interestingSentences));
}
}
您实际得到的输出是什么?在调试器中逐步执行程序时,您学到了什么?我尝试了averagePosition=indexOf(关键字字符串);但那没用。我是否需要像使用numOfSequences和numofKeyword那样初始化变量?就像我知道如何获取字符串中单词的索引一样,但问题是我不知道如何处理用户输入的关键字。就像我可以做indexOf(“word”)一样,例如.averagePosition=inputString.indexOf(keywordString));这行得通吗?在问我们之前最好先检验一下你的假设。或者总是在if子句周围使用大括号!