Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/375.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 理解';无效';此场景中的类型错误?_Java - Fatal编程技术网

Java 理解';无效';此场景中的类型错误?

Java 理解';无效';此场景中的类型错误?,java,Java,我感觉如此接近,却又如此遥远 /** * A program that accepts and int input through 2 command line arguments then, * calculates and prints all the prime numbers up to that integer */ public class Primes { /** * Main method takes in 2 command line argument

我感觉如此接近,却又如此遥远

/**
 * A program that accepts and int input through 2 command line arguments then,
 * calculates and prints  all the prime numbers up to that integer
 */

public class Primes {


   /**
   * Main method takes in 2 command line arguments  and calls
    * necessary algorithm
    * 
    * @param args command line arguments
    */
  public static void main(String[]args) {

  int a = Integer.parseInt(args[0]);
  int n = Integer.parseInt(args[1]);

  for(;args.length < 2;) {

     if(a == 1){
        System.out.println(algorithmOne(n));
        /* }
           else if(a == 2) {
           //reference to method
           }
           else{ //reference to method
           }*/
     }


     System.err.println(timeTaken());
  }
 }


  /**Algorithm 1 method
   *
   *
   */
  public static boolean algorithmOne(int n) {

     for(int m = 2; m < n; m++) {
        if(n%i == 0)
           return false;
     }
     return true;
  }




  /**
   * Method works out time taken to perform an algorithm
   *
   * 
   */
  public static void timeTaken() {

     long startTime = System.currentTimeMillis();
     long time = 0;

     for(int i = 0; i < 1000; i++) {
        time += i;
     }

     long endTime = System.currentTimeMillis();
     System.out.println(endTime - startTime); //prints time taken
  }
}
/**
*通过2个命令行参数接受和int输入的程序,
*计算并打印该整数之前的所有素数
*/
公共类素数{
/**
*Main方法接受2个命令行参数和调用
*必要算法
* 
*@param args命令行参数
*/
公共静态void main(字符串[]args){
inta=Integer.parseInt(args[0]);
int n=Integer.parseInt(args[1]);
对于(;参数长度<2;){
如果(a==1){
System.out.println(algorithmOne(n));
/* }
如果(a==2),则为else{
//参考方法
}
else{//方法的引用
}*/
}
System.err.println(timetake());
}
}
/**算法1方法
*
*
*/
公共静态布尔算法(int n){
对于(int m=2;m
这是我到目前为止写的

我得到的错误是这里不允许使用“void”类型,我研究并了解到:我使用的方法不会在需要值的地方返回值,例如等号的右侧或另一个方法的参数

问题是,我看不出这在我的代码中的具体应用! 另外,我有一种感觉,在我修复这个错误之后,会出现更多的错误,所以任何预见都将不胜感激

感谢您的时间和知识。

您的电话:

 System.err.println(timeTaken());
当:

那么你希望打印什么
timetake
不返回任何值

您可以做的是在
timetake
上返回一个值:

  public static long timeTaken() {

     long startTime = System.currentTimeMillis();
     long time = 0;

     for(int i = 0; i < 1000; i++) {
        time += i;
     }

     long endTime = System.currentTimeMillis();
     long diff = endTime - startTime;
     System.out.println(diff ); //prints time taken
     return diff;
  }
公共静态长时间占用(){
long startTime=System.currentTimeMillis();
长时间=0;
对于(int i=0;i<1000;i++){
时间+=i;
}
long-endTime=System.currentTimeMillis();
长差异=结束时间-开始时间;
System.out.println(diff);//打印所用时间
返回差;
}

但是请注意,您要打印两次该值(在
timetake
及其返回值内)。

打印timetake()时会发生这种情况:


你试图打印一些无效的东西
println
(自然)没有接受void的重载。

您正在
System.out.println()中调用
timetake()
。这是不正确的,因为
System.out.println()
需要获取一个或多个参数,并且由于您的方法不返回任何内容,因此您收到了一个错误

只需调用
timetake()
(不带
System.out.println()
)。它将打印您在insde中计算的差值,因为您已经在方法中打印了它:

而不是:

System.out.println(timetaken());
使用:

您在这里一定遇到了错误:您正在调用
System.err.println(timetake())
并且您的方法不返回任何内容,即
void
。因此,您的方法返回类型必须存在。 或者你这样称呼这个方法

timeTaken()
方法timetake()不返回值,但尝试在以下行中使用返回值:

System.err.println(""+timeTaken());

要么让它返回一个字符串打印到控制台,要么只调用该方法。

您最好使用IDE用Java编写代码。eclipse、netbeans等。

将方法返回类型更改为
long
,并在方法末尾添加
return
语句。所以在
System.err.println(timetake())行中不会出现错误
在您的代码中,
timetake()
方法将
void
值发送到
System.err.println(..)
,而在JAVA中没有这样的规范/设计将
void
发送到任何方法参数。这就是为什么它在第
System.err.println(timetake())行抛出编译器错误

/**
*方法计算执行算法所需的时间
*
* 
*/
公共静态长时间占用(){
long startTime=System.currentTimeMillis();
长时间=0;
对于(int i=0;i<1000;i++){
时间+=i;
}
long-endTime=System.currentTimeMillis();
长总时间=结束时间-开始时间;
System.out.println(总时间);//打印所用时间
返回总时间;
}

错误消息对应于哪一行?我认为返回类型(void)很麻烦,所以我交换了很长时间。这听起来是一个可以接受的选择吗?是的。我还建议删除
内的打印时间
。我可以再次请求您的帮助吗?我在返回所需的时间内修改了打印内容,它编译得非常完美。然而,当我尝试在这样的终端中运行它时:java-jarprimes.jar117,它什么也不返回!你知道为什么吗?
timetaken();
System.err.println(timeTaken());
timeTaken()
System.err.println(""+timeTaken());
/**
   * Method works out time taken to perform an algorithm
   *
   * 
   */
  public static long timeTaken() {

     long startTime = System.currentTimeMillis();
     long time = 0;

     for(int i = 0; i < 1000; i++) {
        time += i;
     }

     long endTime = System.currentTimeMillis();
     long totalTime = endTime - startTime;
     System.out.println(totalTime); //prints time taken
     return totalTime;

  }