首页 \ 问答 \ 查找句子的中间十个字符+在每行上打印更多文本(Finding the middle ten characters of a sentence + printing more text on each line)

查找句子的中间十个字符+在每行上打印更多文本(Finding the middle ten characters of a sentence + printing more text on each line)

非常多,我有这段代码。 它打算采取一个句子,并打印每个单词在自己的路线。 它也应该找到一个句子的中间十个字符。

我的问题是,我不确定如何制作它,以便我实际得到一个句子的中间十个字符。 目前在代码中,它的写法是为了找到最后两位数字。 另外,我想这样做是为了不用只在每行上打印出单词,而是希望它说“第一个词:这个”,“第二个单词:是”等等。 不过,每个人都在一个单独的行上。 任何帮助或指导将非常感激,谢谢!

import java.util.Scanner;

public class Sentence {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        System.out.print("Enter a sentence with 5+ words: ");
        String sentence = in.nextLine();
        System.out.println();

        int space = sentence.indexOf(" ");
        sentence = sentence.substring(0, space) + "\n" + sentence.substring(space + 1);
        System.out.println(sentence.replaceAll("\\s+", "\n"));

        String substring = sentence.length() > 10 ? sentence.substring(sentence.length() / 2) : sentence;
        System.out.println();
        System.out.println("MIDDLE 10 CHARACTERS: " + substring);
    }
}

So pretty much, I have this piece of code. It's intended to take a sentence, and print each word on its own line. It's also supposed to find the middle ten characters of a sentence.

My problem is that I'm unsure how to make it so that I actually get the middle ten characters of a sentence. Currently in the code, it's written so that it finds the last two digits. Also, I would like to make it so that instead of having only the words print out on each line, I'd like it to say "first word: this" "second word: is" and so forth. Still, each on a separate line. Any help or guidance would be very appreciated, thank you!

import java.util.Scanner;

public class Sentence {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        System.out.print("Enter a sentence with 5+ words: ");
        String sentence = in.nextLine();
        System.out.println();

        int space = sentence.indexOf(" ");
        sentence = sentence.substring(0, space) + "\n" + sentence.substring(space + 1);
        System.out.println(sentence.replaceAll("\\s+", "\n"));

        String substring = sentence.length() > 10 ? sentence.substring(sentence.length() / 2) : sentence;
        System.out.println();
        System.out.println("MIDDLE 10 CHARACTERS: " + substring);
    }
}

原文:https://stackoverflow.com/questions/39477224
更新时间:2022-05-28 15:05

最满意答案

令人惊讶的是,您可以在数字和数组上使用RegExp:

var answerArray = [1, 2, 2, 2, 1, 1];
var case1 = [
    [1, 2, 3, 1, 1, 1],
    [1, 2, 2, 2, 1, 1],
    [1, 2, 3, 2, 1, 1],
    [1, 2, 2, 1, 1, 1]
];
var case2 = [1, 2, 3, 1, 1, 2];
var case3 = [
    [1, 2, 3, 1, 2],
    [1, 3, 2, 2, 2]
];
var case4 = [1, 4];

var hit=new RegExp("^"+answerArray+"$", "m");
var names=['case1', 'case2', 'case3', 'case4'];
var res=[case1, case2, case3, case4].map(function(a,b,c){

   return hit.test(a.join("\n")) && names[b];

}).filter(Boolean);


alert(res) // shows: "case1"

I figured it out!! For anyone in the future trying to do the same.

I made an array prototype "compare" seen here (SO Answer)

for (var k = 1; k <= 4; k++) {//Loop number of cases
    var caseArray = eval("case" + k);//To be able to loop through each case
    for (var i = 0; i < caseArray.length; i++) {
        if ($.isArray(caseArray[i])) { //Check if array[i] is another array (2d array)
            if (caseArray[i].compare(answerArray)) { //Compare the nested array
                console.log("MATCH " + k);
                return k;
            }
        } else {//Not a 2d array
            if (caseArray.compare(answerArray)) {//compare the array
                console.log("MATCH " + k);
                return k;
            }
        }
    }
}

相关问答

更多
  • 既然你最终需要一个id为2d的数组,并且在第二个数值中,我建议从合适的大小开始 In [535]: arr = np.zeros((2,10),int) In [536]: arr Out[536]: array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]) In [537]: arr[0,:]=np.arange(10) In [538]: arr Out[538]: array([[0, 1, 2, ...
  • 无需为您编写任何代码... 想想你的2D阵列需要多大。 认识到你需要遍历源数组的内容来获取每个值到目标数组中。 所以它看起来像... 创建一个适当大小的二维数组。 使用for循环来遍历你的1d数组。 在for循环中,您需要计算1d数组中每个值应该在2d数组中的位置。 尝试对你的计数器变量使用mod函数来“环绕”二维数组的索引。 我故意模糊,因为这是家庭作业。 尝试发布一些代码,以便我们可以看到你卡住的地方。 Without writing any code for you... Think about ho ...
  • argCopy是一个2D数组,也就是一个数组数组。 因此,元素argCopy[0]和argCopy[1]将保存默认大小为2的1D数组。由于args是1D数组,因此可以将argCopy [0]从大小为2的空数组重新分配给称为args的数组。 要访问2D数组中每个1D数组的各个元素,您不仅需要识别数组的索引,还要识别元素的索引。 例如, argCopy[0][0]将允许您访问第一个数组的第一个元素。 如果argCopy[0].length的概念argCopy[0].length你感到困惑,那么它就意味着第一个数 ...
  • 数组在C和C ++中是不可变的。 你无法重新分配它们。 你可以使用memcpy : memcpy(arrtemp, words[i], 16 * sizeof(int) ); 这将16 * sizeof(int)字节从words[i] arrtemp到arrtemp 。 Arrays are immutable in C and C++. You can't reassign them. You can use memcpy: memcpy(arrtemp, words[i], 16 * sizeof(i ...
  • 你正在寻找一个“拉链”功能 使用Underscore.js,压缩数组变得简单 http://underscorejs.org/#zip var A = [1,3,5]; var B = [2,4,6]; var zipped = _.zip(A,B); // => [[1,2], [3,4], [5,6]] You're looking for a "zip" function With Underscore.js, zipping arrays made easy http://underscorejs ...
  • 也许你想要这个索引i+n*j为1D数组。 int i,j,k, n=2, result[50]; for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { result[i+n*j] = c[i][j]; for (k = 0; k < n; k++) { result[i+n*j] += a[i][k] * b[k][j]; printf("%i ", result[i+ ...
  • 2D数组是数组的数组。 那么你为什么不尝试这个? int result[][] = {x,y}; 为了确保它非常简单并且有效,请测试以下内容: for(int i=0; i
  • 令人惊讶的是,您可以在数字和数组上使用RegExp: var answerArray = [1, 2, 2, 2, 1, 1]; var case1 = [ [1, 2, 3, 1, 1, 1], [1, 2, 2, 2, 1, 1], [1, 2, 3, 2, 1, 1], [1, 2, 2, 1, 1, 1] ]; var case2 = [1, 2, 3, 1, 1, 2]; var case3 = [ [1, 2, 3, 1, 2], [1, 3, 2 ...
  • *在numpy中进行逐元素乘法,例如将1d数组乘以另一个1d数组: In [52]: np.array([3,4,5]) * np.array([1,2,3]) Out[52]: array([ 3, 8, 15]) 当你将2d数组乘以1d数组时,对于2d数组的每一行都会发生同样的事情: In [53]: np.array([[3,4,5],[4,5,6]]) * np.array([1,2,3]) Out[53]: array([[ 3, 8, 15], [ 4, 10, 18]]) ...
  • 我假设如果你想要将“2D数组”作为一个D矩阵访问,你希望当你将索引递增1时,你可以访问数组中的下一个元素(当你运行时它会自动转到下一行)离开边缘)。 正确的方法是改变分配数组的方式。 我将尝试展示如何完成 - 这只是C,而不是C ++。 自从你使用malloc以来,它可能更合适。 我也在想你的代码中有一个非常严重的错误,因为你正在创建一个char *指针p ,但是期望在它中使用它 p[x][y]; 显然,你需要一个char ** 。 让我们尝试制作能够满足您需求的代码: int sizes[100]; / ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)