未知格式转换异常运行时错误

时间:2018-10-01 02:32:58

标签: java runtime-error

我编写了一个程序,用户输入3个4位十六进制数字,然后使用String方法按大小对它们进行排序。我遇到了一个我不熟悉的错误。系统会编译,但会运行并显示错误。 我将发布我的源代码和错误

import java.util.Scanner;

public class HexNumbers {
  public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.print("Enter three 4-digit hexadecimal numbers: ");
    String numbers = input.nextLine();
    String[] result = numbers.split("\\s+");
    String a = result[0];
    String b = result[1];
    String c = result[2];
    String A = a.toUpperCase();
    String B = b.toUpperCase();
    String C = c.toUpperCase();
    int comp1 = C.compareTo(A);
    int comp2 = C.compareTo(B);
    int comp3 = B.compareTo(A);
    boolean first = comp2 > comp1 && comp1 > 0 && comp2 > 0;
    boolean second = comp1 <0 && comp2 < 0 && comp2 > comp1;
    boolean third = comp1 > 0 && comp2 > 0 && comp1 == comp2;
    boolean fourth = comp1 < 0 && comp2 < 0 && comp1 == comp2;

    if(first = true){
      System.out.printf("Maximum: %/s /n Median: %/s /n Minimum: %/s /n", C, A, B);
    }
    else if (second = true) {
      System.out.printf("Maximum: %/s /n Median: %/s /n Minimum: %/s /n", A, B, C);
    }
    else if (third = true) {
      if (comp3 > 0){
        System.out.printf("Maximum: %/s /n Median: %/s /n Minimum: %/s /n", C, B, A);
      }
      else{
        System.out.printf("Maximum: %/s /n Median: %/s /n Minimum: %/s /n", C, A, B);
      }
    }
    else if (fourth = true) {
      if (comp3 > 0){
        System.out.printf("Maximum: %/s /n Median: %/s /n Minimum: %/s /n", B, A , C);
      }
      else{
        System.out.printf("Maximum: %/s /n Median: %/s /n Minimum: %/s /n", A, B, C);
      }
    }
  }
}

这是用户输入字符串后出现的错误:

java.util.UnknownFormatConversionException: Conversion = '/'
at java.util.Formatter.checkText(Unknown Source)
at java.util.Formatter.parse(Unknown Source)
at java.util.Formatter.format(Unknown Source)
at java.io.PrintStream.format(Unknown Source)
at java.io.PrintStream.printf(Unknown Source)
at HexNumbers.main(HexNumbers.java:33)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:267)
  

1 个答案:

答案 0 :(得分:0)

如果您想使用printf打印新行,则应使用%n而不是\n docs

A new line character appropriate to the platform running the application. You should always use %n, rather than \n. docs