【问题标题】:Why is system.err.printf glitching my output? [duplicate]为什么 system.err.printf 会干扰我的输出? [复制]
【发布时间】:2019-02-02 01:39:19
【问题描述】:

我在 System.err.flush();和 System.out.flush();但是问题仍然存在。似乎这些 printf 语句是以随机的非正统顺序输出的。

当 (absoluteFinalGrade % 1) 范围从 0.45 到 0.49 时,我希望输出以红色打印。这是因为我得到了数百行输出,我希望重要的输出脱颖而出。但是,system.err.printf 似乎以非正统的方式干扰了我的输出。是否可以使用不同的方法使我的文字脱颖而出?

import java.util.Scanner;

public class FinalChemistryGradeCalculator {
    public static void main (String [] args) {

            Scanner input = new Scanner (System.in);
            Scanner input2 = new Scanner (System.in);

            double termGrade;
            double examinationGrade;
            double examinationMarkScore;
            double examinationMarkTotal;
            double lowestTestGrade;
            double lowestTestMarkScore;
            double lowestTestMarkTotal;
            double initialFinalGrade;
            double absoluteFinalGrade;

            System.out.printf("Input the term grade.%n");
            termGrade = input.nextDouble();
            System.out.printf("%nThe term grade is %.2f.%n", termGrade);

        System.out.printf("%nInput the lowest test mark score.");
        lowestTestMarkScore = input.nextDouble();
        System.out.printf("%nInput the lowest test mark total.");
        lowestTestMarkTotal = input.nextDouble();
        lowestTestGrade = (lowestTestMarkScore/lowestTestMarkTotal) * 100;
        System.out.printf("%nThe lowest test grade is %.2f.%n", lowestTestGrade);

        System.out.printf("%nInput the examination mark total.");   
        examinationMarkTotal = input.nextDouble();

        for (examinationMarkScore = 0; examinationMarkScore <= examinationMarkTotal; examinationMarkScore += 0.5) {
            examinationGrade = (examinationMarkScore/examinationMarkTotal) * 100;
            initialFinalGrade = (termGrade * 0.7) + (examinationGrade * 0.3);
            if (lowestTestGrade < examinationGrade) {
                absoluteFinalGrade = initialFinalGrade - (lowestTestGrade * 0.05) + (examinationGrade * 0.05);
            }
            else {
                absoluteFinalGrade = initialFinalGrade;
            }
            if (absoluteFinalGrade % 1 >= 0.45 && absoluteFinalGrade % 1 <= 0.49) {
                System.err.printf("%n%n--------------------------------------------------------------------------------------------------------------");
                System.err.printf("%nTerm Grade: %.2f ||||| Examination Grade: %.2f (%.1f/%.1f) ||||| Lowest Test Grade: %.2f (%.1f/%.1f)", termGrade, examinationGrade, examinationMarkScore, examinationMarkTotal, lowestTestGrade, lowestTestMarkScore, lowestTestMarkTotal);
                System.err.printf("%nInitial Final Grade: %.2f ||||| Absolute Final Grade: %.2f", initialFinalGrade, absoluteFinalGrade);
                System.err.printf("%n--------------------------------------------------------------------------------------------------------------");
                System.err.flush();
            }
            else {
                System.out.printf("%n%n--------------------------------------------------------------------------------------------------------------");
                System.out.printf("%nTerm Grade: %.2f ||||| Examination Grade: %.2f (%.1f/%.1f) ||||| Lowest Test Grade: %.2f (%.1f/%.1f)", termGrade, examinationGrade, examinationMarkScore, examinationMarkTotal, lowestTestGrade, lowestTestMarkScore, lowestTestMarkTotal);
                System.out.printf("%nInitial Final Grade: %.2f ||||| Absolute Final Grade: %.2f", initialFinalGrade, absoluteFinalGrade);
                System.out.printf("%n--------------------------------------------------------------------------------------------------------------");
                System.out.flush();
            }
        }           
    }
}

【问题讨论】:

  • 请解释一下以非正统的方式使我的输出出错
  • 基本上,它在极少数情况下会以混乱的顺序打印出 System.err 和 System.out。
  • 编辑您的问题正文,关注重复项,而不是标题。永远要清楚,“故障”可能意味着一百万件事。此外,证明flush 解决方案不适用于minimal reproducible example
  • “好的,这个问题不是重复的,因为其他问题上提供的解决方案只解决了 println,而不是 printf。” - (当前)链接的所有问题解决你的问题。 “println”与“printf”的区别不相关。
  • 好的,我在刚刚对问题所做的编辑中提供了一个完整且可验证的示例。我不能举一个最小的例子,因为一切对于运行程序都是必不可少的。如果我说这是可验证的时候你不相信我,你可以自己使用 Eclipse 来确定问题是否仍然存在。

标签: java eclipse


【解决方案1】:

你可以使用jansi:

System.out.println( ansi().eraseScreen().render("@|red Hello|@ @|green
 World|@") );

此示例引用自Jansi Github README

请注意,这可能不适用于 Eclipse 或任何 IDE 包装器。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2018-01-19
  • 2016-05-16
  • 1970-01-01
  • 2011-11-11
  • 2011-03-14
  • 2013-01-07
  • 2011-11-26
  • 2021-01-06
相关资源
最近更新 更多