12328257 2021-05-17 12:58 采纳率: 33.3%
浏览 67
已采纳

java (File, Scanner, PrintStream)

import java.util.*;
import java.io.*;

public class YazInterpreter {
   public static void main(String[] args) throws FileNotFoundException {
      Scanner console = new Scanner(System.in);
      boolean go = true;
      intro();
      
      while (go) {
         System.out.print("(I)nterpret YazLang program, (V)iew output, (Q)uit? ");
         String ans = console.next();
    
         if (ans.equalsIgnoreCase("I")) {
            interpret(console);
            go = true;
         } else if (ans.equalsIgnoreCase("V")) {
            view(console);
            System.out.println();
            go = true;
         } else if (ans.equalsIgnoreCase("Q")) {
            go = false;
         }
      }
   }
   
   //This method prints out the introduction of this program.
   public static void intro() {
      System.out.println("Welcome to YazInterpreter!");
      System.out.println("You may interpret a YazLang program and output");
      System.out.println("the results to a file or view a previously");
      System.out.println("interpreted YazLang program.");
      System.out.println();
   }
   
   //This method allows user to input files' names.
   public static File fileName(Scanner console) throws FileNotFoundException {
      System.out.print("Input file name: ");
      String inputName = console.next();
      File file = new File(inputName);
      
      while(!file.exists()) {
         System.out.print("File not found. Try again: ");
         inputName = console.next();
         file = new File(inputName);
      }
      return file;
   }
   
   //This method allows people to view the content of YazLang program in the console.
   public static void view(Scanner console) throws FileNotFoundException {
      File inputName = fileName(console);
      Scanner input = new Scanner(new File(inputName.getName()));
      
      while(input.hasNextLine()) {
         String line = input.nextLine();
         System.out.print(line);
      }
      System.out.println();
   }
   
   //This method interprets.
   public static void interpret(Scanner console) throws FileNotFoundException {
      File file = fileName(console);
      
      System.out.print("Output file name: ");
      String outputName = console.next();
      PrintStream out = new PrintStream(outputName);
      Scanner inputFile = new Scanner(file);
      
      while (inputFile.hasNextLine()) {
         String row = inputFile.nextLine();
         command(console, row, out);
      }
      System.out.println("YazLang interpreted and output to a file!"); 
      System.out.println();
   }
   
   //This method determine which Command to use.
   public static void command(Scanner console, String row, PrintStream out) 
                      throws FileNotFoundException {
      Scanner input = new Scanner(row);
      String commandtype = input.next();
      if (commandtype.equalsIgnoreCase("convert")) {
         int temp = input.nextInt();
         String unit = input.next();
         String convertResult = convert(temp, unit);
         out.println(convertResult);
      } else if (commandtype.equalsIgnoreCase("repeat")) {
         String repeatResult = repeat(row);
         out.println(repeatResult);
      } else if (commandtype.equalsIgnoreCase("range")) {
         int start = input.nextInt();
         int end = input.nextInt();
         int incre = input.nextInt();
         String rangeResult = range(start, end, incre);
         out.println(rangeResult);
      }
   }
   
   
   //This method prints out the command CONVERT
   public static String convert(int temp, String unit) {
      String convertTemp = "";
      if (unit.equalsIgnoreCase("f")) {
         int temp1 = (int)((temp - 32) / 1.8);
         convertTemp = temp1 + "C";
      } else if (unit.equalsIgnoreCase("c")) {
         int temp1 = (int)(temp * 1.8) + 32;
         convertTemp = temp1 + "F";
      }
      return convertTemp;
   }
   
   //This method prints out the command REPEAT
   public static String repeat(String statement) 
                      throws FileNotFoundException {
      Scanner input = new Scanner(statement);
      String command = input.next();

      String statementNew = "";
      while(input.hasNext()) {
         String word = input.next();
         word = word.replace("_", " ");
         word = word.replaceFirst("\"", "");
         word = word.substring(0, word.length() - 1);

         int num = input.nextInt();
         
         for (int i =1; i <= num; i++) {
         statementNew = statementNew + word;
         }
      }
      return statementNew;
   }
   
   //This method prints out the command RANGE
   public static String range(int start, int end, int incre) {
      String numRange = "";
      while (start < end) {
         numRange = numRange + start + " ";
         start = start + incre;
      }
      return numRange;
   }  
}

 

 这两个问题是出在哪了

  • 写回答

3条回答 默认 最新

  • 关注

    String inputName = console.next();

    输入文件名称要有对应的路径

    打印出来看看,路径是否准确。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器