java - 在命令提示符下使用 Java.util.scanner

标签 java java.util.scanner command-prompt

我试图让这个程序仅在命令行之外运行(在本例中为命令提示符),但是当我创建 .jar 时,我不断收到此错误:

Expception in thread "main" java.lang.UnsupportedClassVersionError: Exponentiation: Unsupported major.minor version 51.0
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClassCond(Unknown Source)
    at java.lang.ClassLoader.defineClass(Unknown Source)
    at java.security.SecureClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.access$000(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class: Exponentiation. Program will exit.

程序是:

//import scanner for user input
import java.util.Scanner;

public class Exponentiation
{

static long values[]; //array of values to be computed
static long output[]; //array of computed values
static int numberExp; //number times the experiment is run
static int currentExp = 1; //current time the experiment is running

//main method
public static void main(String [] args)
{
    //run initial setup and random generating
    setup();

    //run the interative and recursive components until the user specified number of experiments
    for (currentExp = 1; currentExp <= numberExp; currentExp++)
    {
        iterative();
        recursive();
    }

} //close main method

//runs the trail iteratively
public static void iterative()
{
    //create variables for time
    long initialTime;
    long endTime;
    long diffTime;

    //record initial time
    initialTime = System.nanoTime();

    //for every value exponetiate
    for (int counter = 1; counter <= values[0]; counter++)
    {
        //raise every value by its opposing value in the array
        output[counter] = values[counter]^values[(int)values[0] + 1 - counter];
    }

    //recourd end time
    endTime = System.nanoTime();

    //calculate the difference
    diffTime = endTime - initialTime;

    //output time values
    System.out.println();
    System.out.println("Experiment " + currentExp + " Iterative:");
    System.out.println(initialTime);
    System.out.println(endTime);
    System.out.println(diffTime);
} //close iterative method

//runs the trial recursively
public static void recursive()
{
    //create variables for time
    long initialTime;
    long endTime;
    long diffTime;

    //record initial time
    initialTime = System.nanoTime();

    //for every value exponetiate
    for (int counter = 1; counter <= values[0]; counter++)
    {
        //raise every value by its opposing value in the array using the recursive method
        output[counter] = recursiveExpo(values[counter],values[(int)values[0] + 1 - counter]);
    }

    //recourd end time
    endTime = System.nanoTime();

    //calculate the difference
    diffTime = endTime - initialTime;

    //output time values
    System.out.println();
    System.out.println("Experiment " + currentExp + " Recursive:");
    System.out.println(initialTime);
    System.out.println(endTime);
    System.out.println(diffTime);
}  //close recursive method

public static long recursiveExpo(long base, long expo) {
    if (expo == 0) {
        return 1;
    } else {
        return base * recursiveExpo(base, expo - 1);
    }
} //close recursiveExpo

//setup method - runs all setup isolated from the main program
public static void setup()
{

    int lowRange; //lowest possible random value
    int highRange; //highest possible random value
    int numOfTrials; //number of trials/random numbers to be generated

    //create scanner for user input
    Scanner input = new Scanner( System.in );

    //runs the random number generator until a good assortment is found
    for (boolean goodRandom = false; goodRandom == false;)
    {

        //clear values[], just in case
        values = null;

        System.out.println("This is the program for Iterative Exponetiation");
        System.out.println("Enter your prefered range of numbers");

        System.out.println("Lowest possible number");
        lowRange = input.nextInt(); //store min random number

        System.out.println("Highest possible number");
        highRange = input.nextInt(); //store max random number

        System.out.println("Number of trials");
        numOfTrials = input.nextInt(); //store number of trials

        System.out.println("How many times should the experiment be run?");
        numberExp = input.nextInt(); //store number of experiments

        //create array of the correct size and store the size in the 0th element
        values = new long[numOfTrials+1];
        values[0] = numOfTrials;

        //make the output array the same lenght
        output = new long[numOfTrials+1];

        //generate random values for each element until the numOfTrials is reached
        for (int counter = 1; counter <= numOfTrials; counter++)
        {
            values[counter] = (int)(Math.random()*(highRange)) + lowRange;
        }

        //create output and concatinate numbers
        for (int counter = 0; counter <= values[0]; counter++)
        {
            //add a line break every tenth number
            //and output the values[]
            if ((int)(counter/10) == (((double)counter)/10))
            {
                System.out.println(values[counter] + " ");
            } else {
                System.out.print(values[counter] + " ");
            }

        }

        System.out.println("Enter 1 to use these values or 0 for new ones");

        //regenerate or not?
        if (input.nextInt() == 1)
        {
            goodRandom = true; //continue on the actual test
        }

    } //close number-generator loop

} //close setup method

} //close Expo class

我认为我的扫描仪出了问题,但我不确定是什么。

最佳答案

当您使用 jdk 7+ 编译此代码时,您实际上是使用 java <=6 运行它。 如果你看here您会看到 51.0 是 java 7,事实上它不受支持意味着您的类路径上的 java 较旧。

尝试在命令行上运行java -version来验证并修复您的路径

关于java - 在命令提示符下使用 Java.util.scanner,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17819417/

相关文章:

java - 使用 SecureRandom 生成安全随机数

java - 在套接字上设置超时时出现 NoSuchElementException

java - 如何使用scanner hasNext()循环一行键盘文本并验证用户输入的整数和字符串

java - Openshift 的预编译 ROOT.war 未部署

java - 具有动态方案的 Jpa

java - 将字符串从控制台输入传递到 TCP 套接字

java - 如何制作在命令提示符下运行的 .jar 文件

vba - 如何使用命令提示符列出目录中的文件名但排除第一个 3 个字符

c# - 在C#中发送cmd命令并读取结果

java - 库依赖不适用于模块