使用showChar,ERROR实际和形式参数列表的长度不同

时间:2017-05-11 03:45:48

标签: java arrays methods java.util.scanner

这是在标题中给出错误的行

showChar();

这是我的方法:

public static char showChar(String best, int pos) {
return best.charAt(pos);
}

这是我的全部代码:

import java.util.Scanner;

public class StringCode
{
   static String input1, input2, s, best;
   static int pos;

   public static void main(String []args)
   {

      Scanner scan = new Scanner(System.in);
      System.out.print("Please input your first word: ");
      input1 = scan.nextLine();

      System.out.print("Please input your second word: ");
      input2 = scan.nextLine();

      compareWords();
      if (input1.length() > input2.length()) {
         System.out.println("Number of characters in " + input1 + " is " + input1.length());
         best = input1;
         }
      else {
         System.out.println("Number of characters in " + input2 + " is " + input2.length());
         best = input2;
         }

      System.out.print("Enter position noting first character is at 0: ");
      pos = scan.nextInt();
      showChar();
      System.out.println("Character at position " + pos + " in " + best + " is: " + s);
      }

   public static void compareWords() 
      {
      if (input1.length() > input2.length()) {
         System.out.println(input1 + " is greater than " + input2);
         }
      else if (input1.compareTo(input2)==0) {
         System.out.println("The first input is equal to second input");
         }
      else {
         System.out.println(input1 + " is not greater than " + input2);
         }
      }

   public static char showChar(String best, int pos) {
      return best.charAt(pos);
      }
}

如果你有任何提示或更好的写作方式,这将有很大帮助 谢谢你,祝你有个愉快的一天

1 个答案:

答案 0 :(得分:0)

在调用已声明为接受字符串showChar();和int best的{​​{1}}时,不包含参数。您的代码应如下所示:

pos

我还建议您在showChar(best, pos); 方法结束时使用scan.close();,以便不再使用扫描仪。