【问题标题】:Why cant first and last be used? [closed]为什么不能使用第一个和最后一个? [关闭]
【发布时间】:2016-10-18 12:05:04
【问题描述】:

大家好,由于私有字符串“first”和“last”未被“使用”,我无法运行。

知道为什么会导致问题吗?谢谢!

public class Bananas{

    private String first;
    private String last;
    private static int members = 0;

    public Bananas(String fn, String ln){
        first = fn;
        last = ln;
        members++;

        System.out.printf("Constructor for %s %s, members in the club: %d\n", members);
    }
}

单独的类

public class clasone {

    public static void main(String[] args){
        Bananas member1 = new Bananas ("Ted","O'Shea");
        Bananas member2 = new Bananas ("John","Wayne");
        Bananas member3 = new Bananas ("Hope","Go");
    }
}

【问题讨论】:

  • 您能分享一下您收到的错误消息吗?
  • 这可能只是一个警告,而不是编译错误。该代码没有用,因为无法从您的课程中获取信息。
  • 错误“线程“主”java.util.MissingFormatArgumentException 中 1 个异常的构造器:java.io.PrintStream 的 java.util.Formatter.format(未知源)中的格式说明符“%s”。格式(未知来源)在 java.io.PrintStream.printf(未知来源)在 Main.(Main.java:13) 在 clasone.main(clasone.java:4) "
  • 这与未使用字符串无关...请编辑您的问题以包含实际错误!
  • 但它没有使用第一个或最后一个字符串?

标签: java static printf


【解决方案1】:

这不是编译错误,而是运行时错误。正如它所说,您的 printf 格式不正确 - 它需要三个参数(两个字符串和一个 int),而您只传递 onw (members)。从上下文来看,我假设您也打算在那里传递 firstlast

System.out.printf("Constructor for %s %s, members in the club: %d\n", 
                   first, last, members);
// -- Here  -------^------^

【讨论】:

  • 谢谢你的菜鸟问题!
【解决方案2】:

线程“main”java.util.MissingFormatArgumentException 中 1 个异常的构造函数:格式说明符“%s”

这是一个运行时错误,而不是编译时错误。这意味着您的格式中有三个值,但您只提供了一个。

【讨论】:

    【解决方案3】:

    你的错误在这一行:

     System.out.printf("Constructor for %s %s, members in the club: %d\n", members);
    

    像这样改变它:

    System.out.printf("Constructor for %s %s, members in the club: %d\n", first, last, members);
    

    留言the private strings "first" and "last" not being "used."|是警告而不是错误。

    错误"Contructor for 1 Exception in thread "main" java.util.MissingFormatArgumentException: 是运行时错误而不是编译错误。这与 printf 方法中缺少参数有关,女巫需要 3 个参数 String, String, Number,因为在您的消息中您有 %s %s %d

    【讨论】:

      【解决方案4】:

      您收到此错误是因为您的String 格式中有没有对应值的占位符,实际上您有两次%s 和一次%d,这意味着它希望将两个参数转换为String 和一个整数或长整数。

      试试这个:

      System.out.printf(
          "Constructor for %s %s, members in the club: %d\n", first, last, members
      );
      

      有关Formatter here 的更多详细信息。

      注意:您可以将 String 格式中的 \n 替换为 %n 以获得与下一个相同的结果:

      System.out.printf(
          "Constructor for %s %s, members in the club: %d%n", first, last, members
      );
      

      【讨论】:

        【解决方案5】:

        问题出在下面一行:

        System.out.printf("Constructor for %s %s, members in the club: %d\n", members);
        

        因为您在 printf 语句中使用了两个用于 String 的格式说明符和一个用于 int 的格式说明符,因此您必须为各自的格式说明符传递三个值,如下所示:

        System.out.printf("Constructor for %s %s, members in the club: %d\n", first,last,members);
        

        如果你只想在 printf 语句中使用成员,或者删除格式说明符,所以像这样改变:

        System.out.printf("Constructor for the members in the club: %d\n", members);
        

        【讨论】:

          猜你喜欢
          • 2020-07-05
          • 2013-12-23
          • 1970-01-01
          • 2021-05-26
          • 2021-03-21
          • 2023-03-12
          • 2020-12-18
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多