简体   繁体   English

如何为 java 中的给定输入文件生成 output 文件

[英]How to generate output file for given input file in java

I am facing trouble to submit a code in a coding platform.我在编码平台中提交代码时遇到了麻烦。

Problem Statement问题陈述

A local musician is putting on a concert to raise money for charity.一位当地音乐家正在举办一场音乐会,为慈善事业筹集资金。 The concert will be held in the town hall, a spacious venue perfectly suited for such an event.音乐会将在市政厅举行,宽敞的场地非常适合举办此类活动。

  • There are r rows of seats, each containing exactly s seats.r排座位,每排正好包含s个座位。
  • At most one person can sit on a single seat (that is, two people cannot share a seat).最多一个人可以坐在一个座位上(即两个人不能共用一个座位)。

There is a problem - the concert may have been overbooked, This means that if everybody who bought tickets comes to the concert.有一个问题 - 音乐会可能已经超额预订,这意味着如果每个买票的人都来参加音乐会。 some of them might have to stand.他们中的一些人可能不得不站立。

Now the musician has approached you, not for advice, but for the answer to the following question:现在这位音乐家找你,不是为了建议,而是为了回答以下问题:

if everybody who bought tickets arrives and tries to find a seat, how many people will end up sitting, and how many people will be standing?如果每个买票的人都来找座位,最终会有多少人坐着,有多少人站着?

This is my program:这是我的程序:

  package Prerequisite;
  import java.io.FileInputStream;
  import java.io.FileOutputStream;
  import java.io.PrintStream;
  import java.util.Scanner;

  public class Solution {
  static private  final String INPUT ="sitin.txt";   
  static private  final String OUTPUT ="sitout.txt";   
  public static void main(String[] args) {
    FileInputStream instream = null;  
    PrintStream outstream = null;  
      try  {  
          instream =  new  FileInputStream (INPUT);  
          outstream =  new  PrintStream ( new  FileOutputStream (OUTPUT));  
          System.setIn (instream);  
          System.setOut (outstream);  
      }  catch  (Exception e) {  
          System.err.println ( "Error Occurred." );  
      }
     Scanner sc=new Scanner(System.in);
     int r=sc.nextInt();
    int s=sc.nextInt();
    int ticket=sc.nextInt();
    if((r*s)>=ticket)
   {
   System.out.println(ticket+" "+0);
    }
  else
  {
    System.out.println(r*s+" "+(ticket-r*s));
  }
   }
     }

Input 7 12 100输入 7 12 100

Output 84 16 Output 84 16

When I was a 16 years, I started programming with pseudo-code: structograms , a kind of visualization of logical flow.当我 16 岁时,我开始使用伪代码进行编程: 结构图,一种逻辑流的可视化。 This may help to solve the decomposition by divide & conquer.这可能有助于通过分而治之的方式解决分解问题。

But first of all start defining your given inputs & expected output - call it your User-Interface (here: file-based)但首先开始定义您给定的输入预期的 output - 将其称为您的用户界面(此处:基于文件)

Requirements: input/output interface要求:输入/输出接口

Supposed given Input假设给定的输入

You are given a file with a line containing 3 records delimited by a space-character:您将获得一个文件,其中包含由空格字符分隔的 3 条记录的行:

7 12 100

Above 3 records represent numbers which are read into variables as follows:以上3条记录代表读入变量的数字,如下:

  • 7 rows in the concert hall音乐厅7 rows
  • 12 seats in each row of the hall大厅每排12 seats
  • 100 tickets are sold in total for the concert演唱会共售出100 tickets

Expected Output预计 Output

The program should write its output to a file.程序应将其 output 写入文件。 So that this file contains a single line with 2 records, ie numbers delimited by space (similar to given input format).所以这个文件包含一行有 2 条记录,即由空格分隔的数字(类似于给定的输入格式)。 First the resulting total count of occupied seats .首先是占用seats总计数 Second the remaining count of left over visitors that can't be seated and are standing .其次是剩下的不能坐下和standing的游客的数量。 For example:例如:

84 16

Decomposition into Pseudo code分解成伪代码

I can't draw a structogram here.我不能在这里画结构图。 And for your task amd language (Java), I advise to start with a couple of comments .对于您的任务 amd 语言(Java),我建议您从几条评论开始。 Just write down like a movie-plot with separate scenes (input, processing, output) what should happen.只需像电影情节一样用不同的场景(输入、处理、输出)写下应该发生的事情。 Like this:像这样:

// (1) read parameters (3 variables) from input (file):
   // open given input-file (sitin.txt)
   // read first line from file
   // close file
   String content = Files.readString(path, StandardCharsets.US_ASCII);
   // split the read line by delimiter space into 3 strings (e.g. array)
   // convert the 3 strings into number variables (e.g. rows, seats, tickets

//  (2) calculate maximum seats (capacity) and ticket-category distribution (seated VS standing):
  // do the math ..
  // return the results (e.g. an object with two fields/variables: totalSeated, totalStanding)

// (3) write results to output:
   // open given output-file (sitout.txt)
   // write 3 variables delimited by space to the file
   // close file

You now have divided the problem into 3 blocks.您现在已将问题分为 3 个块。 Each can be solved separately.每个都可以单独解决。 Thus you can structure the program into 3 methods.因此,您可以将程序构造成 3 种方法。

Why decomposition?为什么要分解?

It might be your key to solve any problem.它可能是您解决任何问题的关键。 It's famously known and widely-practiced by following patterns/principles: separation of concerns , single responsibility principle or simply divide & conquer .它通过以下模式/原则而广为人知并广泛实践:关注点分离单一责任原则或简单地分治

It has many benefits: isolated development and isolated testing.它有很多好处:孤立的开发和孤立的测试。 In this case: you can ask 3 separate questions on stackoverflow在这种情况下:您可以在 stackoverflow 上提出 3 个单独的问题

For (1) I have already found an answer: File-Reading , I already inserted between comments above.对于 (1) 我已经找到了答案: File-Reading ,我已经在上面的评论之间插入了。

Which method is it you have tried to code?您尝试编写哪种方法? Where do you need our help: 1, 2 or 3?您在哪里需要我们的帮助:1、2 或 3?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM