'我应该使用一种方法来显示用户输入的单词的某个字母。
我需要使用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;
}
}
答案 0 :(得分:3)
我想你想要这样的东西:
public static char showChar(String inputString, int inputNo){
char result;
result = inputString.charAt(inputNo -1); // since index starts at 0
return result;
}
答案 1 :(得分:1)
查看String.charAt()
方法。我想你想要更像的东西:
public static char showChar(String inputString, int inputNo){
char result;
result = inputString.charAt(inputNo - 1);
return result;
}
或简化:
public static char showChar(String inputString, int inputNo){
return inputString.charAt(inputNo - 1);
}
请参阅http://www.tutorialspoint.com/java/java_string_charat.htm了解详情
答案 2 :(得分:0)
using System;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication1
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
LineBreaker lb = new LineBreaker(4);
this.Controls.Add(lb);
}
}
public static class HtmlConstants
{
public const string Br = @"<br />";
}
public class LineBreaker : WebControl
{
public LineBreaker(int lineCount)
{
LineCount = lineCount;
}
public int LineCount { get; set; }
protected override void Render(HtmlTextWriter writer)
{
writer.Write(CreateLineBreaker(LineCount));
base.Render(writer);
}
public string CreateLineBreaker(int howManyLines)
{
string result = String.Empty;
StringBuilder sb = new StringBuilder();
if (howManyLines > 0)
{
for (int i = 0; i < howManyLines; i++)
{
sb.Append(HtmlConstants.Br);
}
result = sb.ToString();
}
return result;
}
}