我制作了一个接受用户输入的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);
}
}发布于 2015-01-11 09:53:11
它不要求输入的原因是您永远不会路由到代码的该部分。注意,所有的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期望的签名的一部分)。
发布于 2015-01-11 09:54:18
我认为这是因为你没有填写程序的main方法。
https://stackoverflow.com/questions/27883181
复制相似问题