【问题标题】:Console output to file控制台输出到文件
【发布时间】:2013-11-27 23:58:59
【问题描述】:

基本上,我正在读取一个包含 9 个商店单位的文件,然后是他们的一堆代码,这些代码从中进行了一堆计算,无论如何我遇到了一个问题,我只得到了一些输出。这是我的代码:

    }
    if(total > reccomended_max){
        PrintStream out = new PrintStream(new FileOutputStream("output.txt"));
        System.setOut(out);
        System.out.println("The total staff wages for " + Unitnum + " is £" +total + ", therefore it is higher than the RM");
    }else{
        System.out.println("The total staff wages for " + Unitnum + " is £" +total + ", therefore it is lower than the RM");
    }

“reccomended_max”是用户输入的数字,然后基本上如果店铺单位的“总”工资大于“recommended_max”我需要它:

将店铺单位数据打印到输出文件

Else > 如果小于.. orint 到控制台.. 当我尝试为 reccomended_max 输入随机数时,我得到:

Please enter the recommended maximum(RM) staff cost: 
900
The total staff wages for Unit One is £824.0, therefore it is lower than the RM
The total staff wages for Unit Two is £0.0, therefore it is lower than the RM
The total staff wages for Unit Three is £504.0, therefore it is lower than the RM

为了安慰^^。并输出文件: 第九单元的员工总工资为 935.0 英镑,因此高于 RM

似乎无论输入是什么,我只在输出文件中显示单元九?任何人都知道为什么会这样,或者我的代码太简短以至于不知道为什么?

【问题讨论】:

  • 您应该在 if 语句之外初始化输出流。 else部分没有用到。

标签: java loops console output


【解决方案1】:

尝试将PrintStream out = new PrintStream(new FileOutputStream("output.txt")); 在 if 语句之外。想想也许这就是问题所在,但我还是个初学者,所以不要强迫我!

【讨论】:

    猜你喜欢
    • 2018-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-27
    • 2021-10-21
    • 2010-09-30
    • 2012-07-04
    • 1970-01-01
    相关资源
    最近更新 更多