以下是我能够开发的代码 - 但是我无法创建显示结果的代码。
import java.util.Scanner;
public class Student2 {
public static void main(String[] args) {
String name= "";
char[] array;
Scanner input= new Scanner(System.in);
System.out.print("Please enter a word: ");
name= input.nextLine();
array= tally(name);
input.close();
}
public static char[] tally(String name){
char[] charArray= name.toCharArray();
char[] arr= name.toCharArray();
for (int count=0; count<name.length(); count= count+1){
arr[count]= (char)(arr[count]);
System.out.println(name.charAt(count) +"\t"+(int)arr[count]);
}
return charArray;
}
}
答案 0 :(得分:0)
修改强>
你的方法:
public static void displayResults(String word) {
System.out.println(tally(word));
}
从您的主要方式调用您的方法:
public static void main(String[] args) {
String name= "";
char[] array;
Scanner input= new Scanner(System.in);
System.out.print("Please enter a word: ");
name= input.nextLine();
array= tally(name);
/*Below is where I called your method:*/
displayResults("Test");
你的主要继续:
input.close();
}
答案 1 :(得分:0)
这是你可以做到的另一种方式。
public static void main(String[] args) {
String name= "";
char[] array;
Scanner input= new Scanner(System.in);
System.out.print("Please enter a word: ");
name= input.nextLine();
array= tally(name);
input.close();
//add print statement that uses Arrays.toString()
System.out.println(Arrays.toString(array));
}
输出:
Please enter a word: bob
b 98
o 111
b 98
[b, o, b]
答案 2 :(得分:0)
也许我没有发现你的问题,但只是阅读你的代码,你可能想要在字符串中显示字符并同时显示ASCII号,所以代码可以这样写:
import java.util.Scanner;
public class Student2{
public static void main(String[] args) {
Scanner input= new Scanner(System.in);
System.out.print("Please enter a word: ");
String name= input.nextLine();
tally(name);
input.close();
}
public static void tally(String name){
for(char ch:name.toCharArray()){
System.out.println(ch+"\t"+(int)ch);
}
}
}