简体   繁体   English

为什么在calAverage方法中return语句不返回任何东西

[英]Why doesn't the return statement return anything in the calAverage Method

So, I have coded everything, but return statement isn't returning or printing out anything in the output.所以,我已经对所有内容进行了编码,但是 return 语句没有在输出中返回或打印任何内容。 The return statement is in my calAverage method. return 语句在我的 calAverage 方法中。 And the output should look like this https://gyazo.com/328bcfebfb08709edbc0e62a93ada7f8 but I have everything but the calAverage output.输出应该如下所示https://gyazo.com/328bcfebfb08709edbc0e62a93ada7f8但除了 calAverage 输出之外我什么都有。 I don't get what I did wrong.我不明白我做错了什么。 I know I have to call the method like this: sc.calAverage(a, b, c);我知道我必须像这样调用方法:sc.calAverage(a, b, c); and then assign the return value to a variable and the print it out but I don't know how to do it with the calAverage method since it has three arguments.然后将返回值分配给一个变量并将其打印出来,但我不知道如何使用 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);
        //
    }
}

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

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

You should change你应该改变

sc.calAverage(a, b, c)

to

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

in your main method if you want to print the value returned by the calAverage method.如果您想打印calAverage方法返回的值,请在您的 main 方法中。

Or print the average after calculating it in the method 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;
    }

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

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