【问题标题】:how to run multiple java files then changing one to c++?如何运行多个 java 文件然后将一个更改为 c++?
【发布时间】:2020-08-04 19:26:05
【问题描述】:

我是 java 新手,我得到了 3 个文件,但我不知道如何编译它们以使它们一起工作。

这里是文件:

GetBurnRate.java

import java.io.*;

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

    //Send welcome message
    System.out.println("#Welcome to Lunar Lander");

    try{
      //Begin reading from System input
      BufferedReader inputReader =
      new BufferedReader(new InputStreamReader(System.in));
      //Set initial burn rate to 0
      int burnRate = 0;
      do{
        //Prompt user
        System.out.println("#Enter burn rate or <0 to quit:");
        //Read user response
        try{
          String burnRateString = inputReader.readLine();
          burnRate = Integer.parseInt(burnRateString);
          //Send user-supplied burn rate to next filter
          System.out.println("%" + burnRate);
        } catch(NumberFormatException nfe){
          System.out.println("#Invalid burn rate.");
        }
      }
      while(burnRate >= 0);
      inputReader.close();
    } catch(IOException ioe){
      ioe.printStackTrace();
    }
  }
}

CalcNewValues.java

    import java.io.*;

    public class CalcNewValues{

      public static void main(String[] args){
        //Initialize values
        final int GRAVITY = 2;
        int altitude = 1000;
        int fuel = 500;
        int velocity = 70;
        int time = 0;

        try{
          BufferedReader inputReader = new
          BufferedReader(new InputStreamReader(System.in));

          //Print initial values
          System.out.println("%a" + altitude);
          System.out.println("%f" + fuel);
          System.out.println("%v" + velocity);
          System.out.println("%t" + time);

          String inputLine = null;
          do{
            inputLine = inputReader.readLine();
            if((inputLine != null) &&
               (inputLine.length() > 0)){

              if(inputLine.startsWith("#")){
                //This is a status line of text, and
                //should be passed down the pipeline
                System.out.println(inputLine);
              }
              else if(inputLine.startsWith("%")){
                //This is an input burn rate
                try{
                  int burnRate =
                    Integer.parseInt(inputLine.substring(1));
                  if(altitude <= 0){
                    System.out.println("#The game is over.");
                  }
                  else if(burnRate > fuel){
                    System.out.println("#Sorry, you don't" +
                                       "have that much fuel.");
                  }
                  else{
                    //Calculate new application state
                    time = time + 1;
                    altitude = altitude - velocity;
                    velocity = ((velocity + GRAVITY) * 10 -
                                burnRate * 2) / 10;
                    fuel = fuel - burnRate;
                    if(altitude <= 0){
                      altitude = 0;
                      if(velocity <= 5){
                        System.out.println("#You have" +
                                           "landed safely.");
                      }
                      else{
                        System.out.println("#You have" +
                                           "crashed.");
                      }
                    }
                  }
                  //Print new values
                  System.out.println("%a" + altitude);
                  System.out.println("%f" + fuel);
                  System.out.println("%v" + velocity);
                  System.out.println("%t" + time);
                }
                catch(NumberFormatException nfe){
                }
              }
            }
          }
          while((inputLine != null) && (altitude > 0));
          inputReader.close();
        }
        catch(IOException ioe){
          ioe.printStackTrace();
        }
      }
    }

DisplayValues.java

import java.io.*;
public class DisplayValues{
  public static void main(String[] args){
    try{
      BufferedReader inputReader = new
      BufferedReader(new InputStreamReader(System.in));
      String inputLine = null;
      do{
        inputLine = inputReader.readLine();
        if((inputLine != null) &&
           (inputLine.length() > 0)){
          if(inputLine.startsWith("#")){


  //This is a status line of text, and
        //should be passed down the pipeline with
        //the pound-sign stripped off
        System.out.println(inputLine.substring(1));
      }
      else if(inputLine.startsWith("%")){
        //This is a value to display
        if(inputLine.length() > 1){
          try{
            char valueType = inputLine.charAt(1);
            int value =
            Integer.parseInt(inputLine.substring(2));
            switch(valueType){
              case 'a':
                System.out.println("Altitude: " +
                                   value);
                break;
              case 'f':
                System.out.println("Fuel remaining: " +
                                   value);
                break;
              case 'v':
                System.out.println("Current Velocity: "
                                   + value);
                break;
              case 't':
                System.out.println("Time elapsed: " +
                                   value);
                break;
            }
          }
          catch(NumberFormatException nfe){
          }
        }
      }
    }
  }
  while(inputLine != null);
  inputReader.close();
}
catch(IOException ioe){
  ioe.printStackTrace();
}
  }
}

然后我应该更改 GetCalcNewValues.java 并将其转换为 C++ 并让它像这样运行:

|GetBurnRate.java | GetCalcNewValues.cpp | DisplayValues.java

我只需要知道如何正确编译这些,我就可以做翻译。

【问题讨论】:

  • How do I run a Java program from the command line on Windows? 或来自网络上百万个链接的任何其他内容。与 C++ 类似,Java 代码必须先编译,然后您必须使用java 程序来运行它。请注意,您的两个文件都有public static void main() 函数,因此它们将被编译成两个单独的程序。
  • 我怀疑有人会免费为您将代码从 java 翻译成 c++。这不是 StackOverflow 的意义所在。

标签: java c++ architecture


【解决方案1】:

要使用主文件 GetBurnRate.java 编译 java 程序,在控制台中键入以下内容(您需要安装 java 编译器)

javac GetBurnRate.java

要编译 c++ 程序,您需要安装 c++ 编译器,例如 gcc 或 clang。

对于 gcc,在控制台中输入以下内容

g++ GetCalcNewValues.cpp -o GetCalcNewValues.exe

铿锵声

clang++ GetCalcNewValues.cpp -o GetCalcNewValues.exe

如果您不使用 windows,则编译后的 c++ 程序的文件扩展名可能不是 exe。

运行程序,将第一次的输出作为下一次使用的输入

java GetBurnRate | GetCalcNewValues | java DisplayValues

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多