java - 我的 showChar 方法有什么问题?简单程序方法介绍

标签 java methods char

//Ch5a程序

'我应该使用一种方法来显示用户输入的单词的某个字母。

我需要使用showChar。 我确实没有看到任何明显的错误,并且我已经处理了几个小时。”

import javax.swing.JOptionPane;
public class Ch5a {
    public static void main(String[] args){
        String inputString = JOptionPane.showInputDialog("What word would you like to analyze?");
        String inputNumberString = JOptionPane.showInputDialog("What letter would you like to see? (Eg: For the second letter of 'dog', input 2)");
        int inputNo;
        inputNo = Integer.parseInt(inputNumberString);
    /**
     At this point, i have an input number from the user(inputNo) and I have a word from the user(inputString).
     I then print the inputNo for testing.
     */
        System.out.println(inputNo);
    //time to call the method.
        char answer;
    //I declare the character answer.
        answer = showChar(inputString, inputNo);
    //i set it equal to the result of the method.
        System.out.println("The " + inputString +" number character in " + inputNo + " is" + answer);

}
    public static char showChar(String inputString, int inputNo){
     //local variable
        char result;
        result = showChar(inputString, inputNo); //user's chosen character
    //returning whatever i want in place of the method call(in this case, "result")
        return result;
    }
}

最佳答案

我想你想要这样的东西:

public static char showChar(String inputString, int inputNo){
    char result;
    result = inputString.charAt(inputNo -1);   // since index starts at 0
    return result;
}

关于java - 我的 showChar 方法有什么问题?简单程序方法介绍,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33574370/

相关文章:

java - 如何通过以编程方式提供 SELECTALL CHECKBOX 来检查多选警报框中的所有项目

java - 设置键盘焦点

ruby - 在 Ruby 中减少/注入(inject)运算符

Android Widget Onclick 按钮或方法或来自另一个 Activity 的方法?

java - 如何从包含搜索字符串的字符串中获取字符序列

java - MQTT 订阅数小时后未收到消息

java - 创建 .jar 文件 - 即使使用带有回车符的 manifest.txt 也会获取 "Could not find main class"

java - 鉴于我只有类的完全限定名称作为字符串,我如何知道一个类是否是另一个类的父类(super class)?

c - Malloc 一个字符 ***

c - c中有符号和无符号字符之间的区别