java - 运行已编译的 Java 程序就表示完成

标签 java compilation

我制作了一个接受用户输入的java程序,但是当完成编译后在JGrasp中运行它时,它只是说“操作完成”并且不允许我输入任何内容或显示我在代码中添加的任何打印文本.

/*
 * The file name of your program, Assign_1.java
 *  
 * TCSS 143 – January 10th, 2015
 * Assignment 1  
 */

 import java.util.Scanner;

 /**
 * This program allows 5 town populations to be entered then converted
 * to stars based on each 1000.
 *
 * @author Thejai Riem
 * @version 1/10/15
 */

 public class Assign_1 {

   /**
   * For town1-5 variable for population # that changes later
   */

   public static int town1;
   public static int town2;
   public static int town3;
   public static int town4;
   public static int town5;

   public static void main(String[] theArgs) {
   }

   /**
   * Gets population from user input of the 5 towns.
   *
   * @param theArgs is used for user input to scanner
   */

   public static void getPopulation(String[] theArgs) {
      Scanner population = new Scanner(System.in); // Keyboard input
      System.out.println("Enter the population of town 1: ");
      town1 = population.nextInt(); // Asks input for town population
      System.out.println("Enter the population of town 2: ");
      town2 = population.nextInt(); // Changes town(#) to user input
      System.out.println("Enter the population of town 3: ");
      town3 = population.nextInt();
      System.out.println("Enter the population of town 4: ");
      town4 = population.nextInt();
      System.out.println("Enter the population of town 5: ");
      town5 = population.nextInt();
   } 

   /**
   * Draws population one * for 1000 people
   *
   * @param theArgs is used for string output
   */

   public static void drawPopulationBar(String[] theArgs) {
      System.out.println();
      System.out.println("POPULATION GRAPH:");
      System.out.println("Town 1: " + town1 / 1000);
      System.out.println("Town 2: " + town2 / 1000);
      System.out.println("Town 3: " + town3 / 1000);
      System.out.println("Town 4: " + town4 / 1000);
      System.out.println("Town 5: " + town5 / 1000);
   } 
 }

最佳答案

它不要求输入的原因是您永远不会路由到代码的该部分。请注意,所有 Java 应用程序都是通过调用 main 方法来运行的。请注意,您的内容是空的。我想你想要的可能是这样的:

import java.util.Scanner;

public class Assign_1 {

   /**
   * For town1-5 variable for population # that changes later
   */

   public static int town1;
   public static int town2;
   public static int town3;
   public static int town4;
   public static int town5;

   public static void main(String[] theArgs) {
       getPopulation(theArgs);
       drawPopulationBar(theArgs);
   }

   /**
   * Gets population from user input of the 5 towns.
   *
   * @param theArgs is used for user input to scanner
   */

   public static void getPopulation(String[] theArgs) {
      Scanner population = new Scanner(System.in); // Keyboard input
      System.out.println("Enter the population of town 1: ");
      town1 = population.nextInt(); // Asks input for town population
      System.out.println("Enter the population of town 2: ");
      town2 = population.nextInt(); // Changes town(#) to user input
      System.out.println("Enter the population of town 3: ");
      town3 = population.nextInt();
      System.out.println("Enter the population of town 4: ");
      town4 = population.nextInt();
      System.out.println("Enter the population of town 5: ");
      town5 = population.nextInt();
   } 

   /**
   * Draws population one * for 1000 people
   *
   * @param theArgs is used for string output
   */

   public static void drawPopulationBar(String[] theArgs) {
      System.out.println();
      System.out.println("POPULATION GRAPH:");
      System.out.println("Town 1: " + town1 / 1000);
      System.out.println("Town 2: " + town2 / 1000);
      System.out.println("Town 3: " + town3 / 1000);
      System.out.println("Town 4: " + town4 / 1000);
      System.out.println("Town 5: " + town5 / 1000);
   } 
 }

我还发现您实际上并未使用任何命令行参数(任何命令行参数都会填充到主方法调用中的字符串数组参数中)。因此,您可以从其他两个方法中删除这些参数(但您必须将其保留在 main 方法中,即使它没有被使用,因为它是 java 期望的签名的一部分)。

关于java - 运行已编译的 Java 程序就表示完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27883181/

相关文章:

Android Studio - 出现错误 : java. util.zip.ZipException:只有 DEFLATED 条目可以有 EXT 描述符

C 预处理无法在 #error 后立即停止

c++ - 如何在不要求客户安装的情况下编译PCAP程序

java - 在 Java 中运行 python 或其他语言代码

c++ - 不使用 boost 绑定(bind)

java - git addfilepattern 无法读取我的目录路径

java - 为什么 Activity 管理器显示启动 Intent 消息?

java - Spring MVC 随着页面方向的改变而改变语言环境

java - 如何使用java下载网页并保存图像文件

java - curl 错误 : could not resolve host, java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:-1