Java调用Char方法来收集从A到W之间的字符
我正在写一个代码来显示从a到w的字母,每行只有10个字母。我的任务是调用方法“publicstaticvoidprintchars(charc1,charc2)”,但我不确定如何调用这个方法。我的代码仍然非常混乱,我正试图根据教科书中给出的示例来解开并理解它:Java调用Char方法来收集从A到W之间的字符,java,methods,char,Java,Methods,Char,我正在写一个代码来显示从a到w的字母,每行只有10个字母。我的任务是调用方法“publicstaticvoidprintchars(charc1,charc2)”,但我不确定如何调用这个方法。我的代码仍然非常混乱,我正试图根据教科书中给出的示例来解开并理解它: import java.util.Scanner; public class CallingTheCharMethods { public static void main(String[] args) { System.out.p
import java.util.Scanner;
public class CallingTheCharMethods {
public static void main(String[] args) {
System.out.println("Printing all letters between a and w:");
}
final int NUMBER_OF_CHARS = 23;
final int CHARS_PER_LINE = 10;
}
for (int i = 0; i < NUMBER_OF_CHARS; i++) {
char ch = public static void printChars(char c1, char c2);
if ((i + 1) % CHARS_PER_LINE == 0)
System.out.println(ch);
else
System.out.print(ch);
}
import java.util.Scanner;
公共类调用echarmethods{
公共静态void main(字符串[]args){
System.out.println(“打印a和w之间的所有字母:”);
}
字符的最终整数=23;
每行最终整数字符=10;
}
for(int i=0;i<字符数;i++){
char ch=公共静态无效打印字符(字符c1,字符c2);
如果((i+1)%CHARS\u每行==0)
系统输出println(ch);
其他的
系统输出打印(ch);
}
再一次,我为混乱、愚蠢的代码感到抱歉。我正在尽我所能去理解这一点 关于使用类似
public static void printChars(char c1, char c2)
它可以用不同的参数从main调用
e、 g
此方法中的代码类似于
for (char startChar = c1; startChar <= c2; startChar++) {
print startChar
}
for(char startChar=c1;startChar拥有类似
public static void printChars(char c1, char c2)
它可以用不同的参数从main调用
e、 g
此方法中的代码类似于
for (char startChar = c1; startChar <= c2; startChar++) {
print startChar
}
for(char startChar=c1;startCharpublic类调用方法{
publicstaticvoidmain(字符串argv[]){//main方法;运行代码时执行的第一个方法
System.out.println(“打印a和w之间的所有字母:”);
printChars('a','w');//使用参数a和w调用printChars方法
}
私有静态void printChars(char c1,char c2){//printChars方法的定义:接受两个char c1和c2作为参数
int charsPerLine=1;
对于(char firstChar=c1;firstChar调用方法的公共类{
publicstaticvoidmain(字符串argv[]){//main方法;运行代码时执行的第一个方法
System.out.println(“打印a和w之间的所有字母:”);
printChars('a','w');//使用参数a和w调用printChars方法
}
私有静态void printChars(char c1,char c2){//printChars方法的定义:接受两个char c1和c2作为参数
int charsPerLine=1;
对于(char firstChar=c1;firstChar可能会将c1
和c2
和start
重命名为firstChar
和lastChar
和ch
和c2
,我同意,但根据任务,它应该是c1
和c2
,谢谢你的帮助!所以我正在重新编写代码,以便更好地使用。)在显示system.out.print“打印所有字母…”部分代码后,我不知道如何调用printChar方法。我编写了:public static void printChars('a','w'))但是我遇到了一个错误。我该如何解决这个问题呢?要调用printChars
只需在System.out.println(“打印a和w之间的所有字母:”);
像printChars('a','w')之后调用它即可
-你犯了什么错误?也许可以将c1
和c2
和start
重命名为firstChar
和lastChar
和ch
和c2
,谢谢你的帮助!所以我要把我的代码重做给b请仔细阅读您为我提供的提示和信息。在显示system.out.print“打印所有字母…”部分代码后,我不知道如何调用printChar方法。我编写了:public static void printChars('a','w'))但是我遇到了一个错误。我如何着手解决这个问题呢?要调用printChars
只需在System.out.println(“打印a和w之间的所有字母:”);
像printChars('a','w');
之后调用它-您遇到了什么错误?
public class CallingTheCharMethods {
public static void main(String argv[]) { // your main method; the first method which is excuted when your run your code
System.out.println("Printing all letters between a and w:");
printChars('a', 'w'); // calling the printChars method with the parameters a and w
}
private static void printChars(char c1, char c2) { // definition of the printChars method : accept two chars c1 and c2 as parameter
int charsPerLine = 1;
for (char firstChar = c1; firstChar <= c2; firstChar++,charsPerLine++){ // start from c1 as first charachter and print until c2
if(charsPerLine%10 == 0){ // count already printed chars and keep the value in the var charsPerLine
System.out.println(firstChar); // if charsPerLine equals n*10 start a new line
} //(println() prints on a new line)
else{
System.out.print(firstChar); // (print() prints on the current line)
}
}
}
}