我正在尝试第一次学习方法,但是在尝试编译时遇到了一个错误

时间:2019-04-23 20:30:27

标签: java

我正在尝试找出设置方法代码的正确方法,但始终会出错。我已经确定我在正确的位置有正确的牙套。而且所有代码都在类内部,所以我真的很困惑。这可能是一个简单的修复程序,但我看不到。

import java.util.Scanner;

public class NumbersFunctions {
    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);

        double x, y, z;
        System.out.println("Please input 3 numbers");
        x = keyboard.nextDouble();
        y = keyboard.nextDouble();
        z = keyboard.nextDouble();

        public double smallest(double x, double y, double z)
        {
            if (x < y && x < z) {
                System.out.println(x);
            } else if (y < z) {
                System.out.println(y);
            }
        }
    }
}

这是我得到的错误:

  Numbers.java:10: error: illegal start of expression
  public double smallest(double x, double y, double z) {
  ^
  Numbers.java:19: error: class, interface, or enum expected
  }

2 个答案:

答案 0 :(得分:1)

只需稍微调整一下代码,它就是这样工作的...

 public class NumbersFunctions {

 public static void main(String[] args) {
   Scanner keyboard = new Scanner(System.in);

   double x, y, z;
   System.out.println("Please input 3 numbers");
   x = keyboard.nextDouble();
   y = keyboard.nextDouble();
   z = keyboard.nextDouble();

   smallest(x, y, z);

}  

public static void smallest(double x, double y, double z)
{

   if(x < y && x < z){
     System.out.println(x);
   }else if(y < z){
     System.out.println(y);
   }
 }
}

答案 1 :(得分:0)

这是编写它的正确方法。一个类中有2个方法。 main()将调用minimum()方法。

import java.util.Scanner; 
public class NumbersFunctions
{

  public static void main(String[] args) 
  {

    Scanner keyboard = new Scanner(System.in);

    double x, y, z;
    System.out.println("Please input 3 numbers");
    x = keyboard.nextDouble();
    y = keyboard.nextDouble();
    z = keyboard.nextDouble();

    smallest(x, y, z);
    //System.out.println(smallest(x, y, z));

  }

  public static void smallest(double x, double y, double z)
// public Double smallest(double x, double y, double z)
  {

    if(x < y && x < z){
      System.out.println(x);
      //return(x);
    }
    else if(y < z){
      System.out.println(y);
      //return(y);
  }
    else {
      System.out.println(z);
      //return(z);

}   
}