繁体   English   中英

使用带有命令提示符的Java.util.scanner

[英]Using Java.util.scanner with 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运行它。 如果你看这里你会看到51.0是java 7,事实上它不受支持意味着你的类路径上的java更旧。

尝试在命令行上运行java -version来验证并修复你的PATH

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM