java—为什么return语句在calaverage方法中不返回任何内容

6yjfywim  于 2021-07-05  发布在  Java
关注(0)|答案(2)|浏览(353)

所以,我已经对所有内容进行了编码,但是return语句没有返回或打印输出中的任何内容。return语句在我的calaverage方法中。输出应该是这样的https://gyazo.com/328bcfebfb08709edbc0e62a93ada7f8 但除了平均产量我什么都有。我不明白我做错了什么。我知道我必须这样调用方法:sc.calaverage(a,b,c);然后将返回值赋给一个变量并打印出来,但我不知道如何使用calaverage方法,因为它有三个参数。

import java.util.Scanner;
public class SecretCode
{
    //no instance variables
    public SecretCode(){
    }

    public double calAverage(int a, int b, int c){
        double average = 0.0;
        //your work - step 2

        average = (a + b + c) / 3.0;
        return average;
    }
    public void decodeMe(String s1){
        //your work here step 4
        //This method will take in a String and then process the string to produce output withe the following rules:
        // The first 5 characters are needed but must be uppercase()
        // The first integer will decrease by 121
        // The last number only takes the last 2 decimals
        // Print out 3 lines of data as followed:
        // XXXXX
        // XXX
        // XX

        String s = "Delta 230 ATA 23.75";
        s1 = s.substring(0, 5);
        String s2 = s1.toUpperCase();

        int wholeNumber = Integer.parseInt(s.substring(6, 9));
        int finalNumber = wholeNumber - 121; 

        int lastNumber = Integer.parseInt(s.substring(17,19));

        System.out.println(s2 + "\n" + finalNumber + "\n" + lastNumber);

    }

    public static void main(String args[]){
        int a, b, c;
        String s;
        SecretCode sc = new SecretCode();
        Scanner myObj = new Scanner(System.in);
        System.out.println("Enter 3 numbers separated by space ");
        //your work step 3
        // receive 3 integer values and call calAverage() method 
        // print out the average 

        a = myObj.nextInt();
        b = myObj.nextInt();
        c = myObj.nextInt();
        sc.calAverage(a, b, c);

        //
        Scanner myObj1 = new Scanner(System.in);
        System.out.println("Enter a secret code below ");
        //Step enter the code: Delta 230 ATA 23.75
        s = myObj1.nextLine();
        sc.decodeMe(s);
        //
    }
}
o4hqfura

o4hqfura1#

你应该改变 sc.calAverage(a, b, c)

double avg = sc.calAverage(a, b, c)
System.out.println(avg);

如果要打印 calAverage 方法。
或在方法中计算后打印平均值 calAverage .

public double calAverage(int a, int b, int c) {
        double average = 0.0;
        //your work - step 2

        average = (a + b + c) / 3.0;
        System.out.println(average);
        return average;
    }
gwbalxhn

gwbalxhn2#

将函数的响应保存在变量中:

double averageValue = sc.calcAverage(5, 3, 2);

相关问题