简体   繁体   English

如何在Java中调用方法

[英]How to call on a method in Java

import java.util.Scanner;
public class Question1 
{
    public static void main(String[] args) 
    {    

      Primary test = new Primary();
      test.Main();

    }
}


class Primary       
{
  double x;
  double y;
  double z;
  double Small;
  double Avg;
  int dad;
  final int userNumbers = 3;
  Scanner in = new Scanner(System.in);


  public void Main()
  {
      System.out.println("Please enter 3 numbers");
       x = in.nextInt();
       y = in.nextInt();
       z = in.nextInt();
       Primary test = new Primary();
       test.Smallest();
       test.Average();
       System.out.println("The average of the numbers is:" + Avg);
       System.out.println("The smallest of the numbers is:" + Small);
  }


  public void Smallest()
  {
      if(z < y && z < x)
          Small = z;
      if (x < z && x < y)
          Small = x;
      if (y < z && y < x)
          Small = y;  
  }

  public void Average()
  {
      Avg = x + y + z / userNumbers;   
  }
}

I have no clue what to do since everything ive tried either gives me an error or I'm just not doing it right. 我不知道该怎么办,因为我所尝试的一切都给了我一个错误,或者我只是做错了。 I'm not even sure I'm doing what the professor is asking me to do, and he never responds to his emails. 我什至不确定我是否正在按照教授要求我做的事情,而且他从不回复他的电子邮件。 If anyone could help me it would be greatly appreciated (And if I'm not doing what my professor is asking please let me know). 如果有人可以帮助我,将不胜感激(如果我没有按照教授的要求去做,请告诉我)。

Heres his instructions + The assignment on page 248 继承人的指示+第248页的作业

Around Page 248. Practice Exercise E5.1.You will create one class that has three methods: main(), smallest(), and average(). 在第248页周围。练习E5.1。您将创建一个包含三种方法的类:main(),smallest()和average()。 main() reads the test data values and prints the results. main()读取测试数据值并打印结果。 You may want to do this in a loop. 您可能需要循环执行此操作。 smallest() and average() do not read data values or print the results. minimum()和average()不会读取数据值或打印结果。 The main() method reads the data values and prints the results. main()方法读取数据值并打印结果。

Write the following methods and provide a program to test them. 编写以下方法并提供程序对其进行测试。 a. 一种。 double smallest(double x, double y, double z), returning the smallest of the arguments b. 最小的double(double x,double y,double z),返回最小的参数b。 double average(double x, double y, double z), returning the average of the arguments double average(double x,double y,double z),返回参数的平均值

Here try this....` 这里尝试这个。

let me know how that works 让我知道如何运作

import java.util.ArrayList;
import java.util.Scanner;
package javaapplication17;

public class Question1 {
    public static void main(String[] args) 
    {    
     double avg, small;
      Scanner in = new Scanner(System.in);

      System.out.println("Please enter 3 numbers");
       double x = in.nextInt();
       double y = in.nextInt();
       double z = in.nextInt();

       //Method calls -----------------------------------

       avg = average(x, y, z);
       small = smallest(x, y, z);

       //Method calls -----------------------------------


       System.out.println("The average of the numbers is: " + avg);
       System.out.println("The smallest of the numbers is: " + small);
      }


      public static double smallest(double x, double y, double z)
      {
          double small = 0;
       if(z <= y && z <= x)
           small = z;
       if (x <= z && x <= y)
           small = x;
       if (y <= z && y <= x)
           small = y;  

        return small;
      }

      public static double average(double x, double y, double z)
      {
          return (x + y + z) / 3.0;   
      }   
}

You do need to learn java to do this. 您确实需要学习Java才能做到这一点。 See https://en.wikiversity.org/wiki/Java_Tutorial/Hello_World ! 参见https://en.wikiversity.org/wiki/Java_Tutorial/Hello_World and others like it for hints. 和其他喜欢它的提示。

To answer the question given to you, you only need one class - you have two. 要回答给您的问题,您只需要一堂课-您有两堂课。 In that class, you need three methods. 在该类中,您需要三种方法。 Two of the methods return values - you have them returning a void. 其中两个方法返回值-您让它们返回一个void。 You shouldn't need any field variables in your class. 您在类中不需要任何字段变量。

Structure the class as in the tutorial link. 按照教程链接中的说明构造类。 (This isnt' a working answer - you still have to do the homework.) (这不是一个可行的答案-您仍然需要做家庭作业。)

public class Question1 {

    public static void main( String[] args ) {

        double x, y, z;

        // Add your a loop here to collect your x, y and z
        // data and process it
        while (true) {
           // Collect the data ...

           // Call the methods
           double smallest = smallest(x, y, z);
           double average = average(x, y, z);

           // Do something with the results
        }
    }

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

        double smallValue;

        // Add your logic and set smallValue

        return smallValue;

    }

    public static double average(double x, double y, double z) {

        // Calculate the average and return that value
        // as in the smallest method...

    }
}

To test the functions, write another program like the following: 要测试这些功能,请编写另一个程序,如下所示:

import Question1;

public class TestQuestion1 {

    public static void main( String[] args ) {

        // Add test code here. Something like 
        double result = Question1.smallest(1.0, 2.0, 3.0)
        //  Check that the result is 1.0

        //  Add other tests - output the results

}

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

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