我第一次尝试学习方法,但是在尝试编译时出现错误

我正在尝试找出设置方法代码的正确方法,但不断出现错误。我已经确保我有正确的牙套应该在哪里。所有的代码都在课堂上,所以我真的很困惑。这可能是一个简单的修复,但我看不到它。


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

  }


杨魅力
浏览 115回答 2
2回答

MMMHUHU

稍微调整一下你的代码,它就是这样工作的......&nbsp;public class NumbersFunctions {&nbsp;public static void main(String[] args) {&nbsp; &nbsp;Scanner keyboard = new Scanner(System.in);&nbsp; &nbsp;double x, y, z;&nbsp; &nbsp;System.out.println("Please input 3 numbers");&nbsp; &nbsp;x = keyboard.nextDouble();&nbsp; &nbsp;y = keyboard.nextDouble();&nbsp; &nbsp;z = keyboard.nextDouble();&nbsp; &nbsp;smallest(x, y, z);}&nbsp;&nbsp;public static void smallest(double x, double y, double z){&nbsp; &nbsp;if(x < y && x < z){&nbsp; &nbsp; &nbsp;System.out.println(x);&nbsp; &nbsp;}else if(y < z){&nbsp; &nbsp; &nbsp;System.out.println(y);&nbsp; &nbsp;}&nbsp;}}

慕盖茨4494581

这是正确的写法。一个类中有 2 个方法。main() 将调用 minimum() 方法。import java.util.Scanner;&nbsp;public class NumbersFunctions{&nbsp; public static void main(String[] args)&nbsp;&nbsp; {&nbsp; &nbsp; Scanner keyboard = new Scanner(System.in);&nbsp; &nbsp; double x, y, z;&nbsp; &nbsp; System.out.println("Please input 3 numbers");&nbsp; &nbsp; x = keyboard.nextDouble();&nbsp; &nbsp; y = keyboard.nextDouble();&nbsp; &nbsp; z = keyboard.nextDouble();&nbsp; &nbsp; smallest(x, y, z);&nbsp; &nbsp; //System.out.println(smallest(x, y, z));&nbsp; }&nbsp; public static void smallest(double x, double y, double z)// public Double smallest(double x, double y, double z)&nbsp; {&nbsp; &nbsp; if(x < y && x < z){&nbsp; &nbsp; &nbsp; System.out.println(x);&nbsp; &nbsp; &nbsp; //return(x);&nbsp; &nbsp; }&nbsp; &nbsp; else if(y < z){&nbsp; &nbsp; &nbsp; System.out.println(y);&nbsp; &nbsp; &nbsp; //return(y);&nbsp; }&nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; System.out.println(z);&nbsp; &nbsp; &nbsp; //return(z);}&nbsp; &nbsp;}&nbsp;
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java