所以,今天,我接受了一个学校项目,我真的需要帮助。
我需要找到这个数组中每个元素的第一个和最后一个字母,然后打印出来。对于你们所有的java人,你能告诉我为了做到这一点我需要什么样的函数,方法或代码吗?
当我编译并运行它时出现的问题之一是它所说的charAt(a);它告诉我它无法承担那里的角色,无论出于何种原因。
所以,如果你可以帮助我解决这个问题,我将不胜感激,因为这是今晚到期的,我无法理解。
非常感谢。
/**
* Write a description of class Pokemon here.
*
* @author (your name)
* @version (a version number or a date)
*/
import java.util.Scanner;
public class Pokemon
{
public static void main (String [ ] args)
{
Scanner scan = new Scanner(System.in);
String array [ ] = new String [ 10 ];
array [ 0 ] = "Charizard";
array [ 1 ] = "Pikachu";
array [ 2 ] = "Jigglypuff";
array [ 3 ] = "Timburr";
array [ 4 ] = "Conkeldurr";
array [ 5 ] = "Gurdurr";
System.out.println("------------------------========================------------------------");
System.out.println(" ");
System.out.println(" .\"-,.__");
System.out.println(" `. `. ,");
System.out.println(" .--' .._,'\"-' `.");
System.out.println(" . .' `'");
System.out.println(" `. / ,'");
System.out.println(" ` '--. ,-\"'");
System.out.println(" `\"` | \\");
System.out.println(" -. \\, |");
System.out.println(" `--Y.' ___.");
System.out.println(" \\ L._, \\");
System.out.println(" _., `. < <\\ _");
System.out.println(" ,' ' `, `. | \\ ( `");
System.out.println(" ../, `. ` | .\\`. \\ \\_");
System.out.println(" ,' ,.. . _.,' ||\\l ) '\".");
System.out.println(" , ,' \\ ,'.-.`-._,' | . _._`.");
System.out.println(" ,' / \\ \\ `' ' `--/ | \\ / / ..\\");
System.out.println(" .' / \\ . |\\__ - _ ,'` ` / / `.`.");
System.out.println(" | ' .. `-...-\" | `-' / / . `.");
System.out.println(" | / |L__ | | / / `. `.");
System.out.println(" , / . . | | / / ` `");
System.out.println(" / / ,. ,`._ `-_ | | _ ,-' / ` \\");
System.out.println(" / . \"`_/. `-_ \\_,. ,' +-' `-' _, ..,-. \\`.");
System.out.println(" ' .-f ,' ` '. \\__.---' _ .' ' \\ \\");
System.out.println("' / `.' l .' / \\.. ,_|/ `. ,'` L`");
System.out.println("|' _.-\"\"` `. \\ _,' ` \\ `.___`.'\"`-. , | | | \\");
System.out.println("|| ,' `. `. ' _,...._ ` | `/ ' | ' .|");
System.out.println("|| ,' `. ;.,.---' ,' `. `.. `-' .-' /_ .' ;_ ||");
System.out.println("|| ' V / / ` | ` ,' ,' '. ! `. ||");
System.out.println("||/ _,-------7 ' . | `-' l / `||");
System.out.println(" | ,' .- ,' || | .-. `. .' ||");
System.out.println(" `' ,' `\".' | | `. '. -.' `'");
System.out.println(" / ,' | |,' \\-.._,.'/'");
System.out.println(" . / . . \\ .''");
System.out.println(" .`. | `. / :_,'.'");
System.out.println(" \\ `...\\ _ ,'-. .' /_.-'");
System.out.println(" `-.__ `, `' . _.>----''. _ __ /");
System.out.println(" .' /\"' | \"' '_");
System.out.println(" /_|.-'\\ ,\\\". '.'`__'-( \\");
System.out.println(" / ,\\\"'\\\"\\,' `/ `-.|\"");
System.out.println(" ");
System.out.println("------------------------========Charizard========------------------------");
System.out.println(" ");
System.out.println(" ");
System.out.println("Items in array: ");
System.out.println(" ");
System.out.println("0 - " + array [ 0 ]);
System.out.println("1 - " + array [ 1 ]);
System.out.println("2 - " + array [ 2 ]);
System.out.println("3 - " + array [ 3 ]);
System.out.println("4 - " + array [ 4 ]);
System.out.println("5 - " + array [ 5 ]);
System.out.println(" ");
System.out.println(" ");
int a = (array [ 0 ]).length();
int b = (array [ 1 ]).length();
int c = (array [ 2 ]).length();
int d = (array [ 3 ]).length();
int e = (array [ 4 ]).length();
int f = (array [ 5 ]).length();
char g = (array [ 0 ]).charAt(0);
char h = (array [ 1 ]).charAt(0);
char i = (array [ 2 ]).charAt(0);
char j = (array [ 3 ]).charAt(0);
char k = (array [ 4 ]).charAt(0);
char l = (array [ 5 ]).charAt(0);
char m = (array [ 0 ]).charAt(a);
char n = (array [ 1 ]).charAt(b);
char o = (array [ 2 ]).charAt(c);
char p = (array [ 3 ]).charAt(d);
char q = (array [ 4 ]).charAt(e);
char r = (array [ 5 ]).charAt(f);
System.out.println(array [ 0 ] + " - First letter of element: " + g + " Second letter of element: " + m);
System.out.println(array [ 1 ] + " - First letter of element: " + h + " Second letter of element: " + n);
System.out.println(array [ 2 ] + " - First letter of element: " + i + " Second letter of element: " + o);
System.out.println(array [ 3 ] + " - First letter of element: " + j + " Second letter of element: " + p);
System.out.println(array [ 4 ] + " - First letter of element: " + k + " Second letter of element: " + q);
System.out.println(array [ 5 ] + " - First letter of element: " + l + " Second letter of element: " + r);
}
}
答案 0 :(得分:2)
你必须使用,
char m = (array [ 0 ]).charAt(a-1);
char n = (array [ 1 ]).charAt(b-1);
char o = (array [ 2 ]).charAt(c-1);
char p = (array [ 3 ]).charAt(d-1);
char q = (array [ 4 ]).charAt(e-1);
char r = (array [ 5 ]).charAt(f-1);
因为索引从0开始,而最后一个字母的索引是(length-1)
但最好为此
使用for循环for(int v=0;v<6;v++){
System.out.println(array [ v ] + " - First letter of element: " + array[v].charAt(0) + " Second letter of element: " + array[v].charAt(array[v].length()-1));
}
答案 1 :(得分:1)
基于零的索引
char m = (array [ 0 ]).charAt(a-1);
答案 2 :(得分:0)
这可以帮到你:
public class CalculateActionListener {
//Here's the actual ActionListener
private ActionListener calculateListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "WORKING!");
}
};
//Getter for my ActionListener
public ActionListener getCalculateListener() {
return calculateListener;
}
}