询问用户输入数字并返回该索引的字符?

时间:2017-01-19 22:13:32

标签: java eclipse indexing char

我对如何完成此事感到难过。脚本需要向用户询问一个句子,告诉他们长度,返回该索引处的字符,而不是要求他们输入一个字符并给出它出现的第一个位置。我只是想弄清楚如何使用数字输入来找到返回该索引处的字符。 (我知道它可能是一个简单的答案)。其他一切都有效。

public class Sentence
{
    Scanner scan = new Scanner (System.in);
    int sentlength;
    int letterenter;
    int lowerinput;
    int letterloc;
    String enterletter;

public void sentence()
{
    System.out.print("Please enter a sentence");
    String originalsent = scan.nextLine();
    sentlength=originalsent.length();

    System.out.println("The sentence is "+sentlength+" charecters long");   
    System.out.println("Please enter a number less than the length of the sentence");
    lowerinput = scan.nextInt();

    System.out.println("Please enter a charecter");
    enterletter = scan.next();
    letterloc = originalsent.indexOf(""+enterletter+"");
    System.out.println(""+letterloc+"");
}

public static void main(String[] args)
{
    Sentence worksheet= new Sentence();
    worksheet.sentence();

}

}

2 个答案:

答案 0 :(得分:0)

这很容易:

System.out.println(originalsent.charAt(lowerinput));

答案 1 :(得分:0)

我相信你正在从你的问题中寻找类似的东西

 With Application.Caller
    CallerRow = .Row
 End With
 findUnique = Evaluate("=Index(" & indexRange.Address & ",AGGREGATE(15,6,Row(" & matchRange.Address & _
    ")/(" & matchRange.Address & "=" & matchVal & "),Row(" & CallerRow & ":" & CallerRow & ")))")

运行时输出以下内容

    System.out.print("Please enter a sentence: ");
    String originalsent = scan.nextLine();
    sentlength=originalsent.length();

    System.out.println("The sentence is "+ sentlength +" characters long");   
    System.out.println("Please enter a number less than the length of the sentence: ");
    lowerinput = scan.nextInt();
    System.out.println("The character at index " + lowerinput + " is " + originalsent.charAt(lowerinput));
    System.out.println("Please enter a character: ");
    enterletter = scan.next();
    System.out.println("The first index " + enterletter + " shows up is at " + originalsent.indexOf(enterletter));