【问题标题】:Why doesn't the return statement return anything in the calAverage Method为什么在 calAverage 方法中 return 语句不返回任何内容
【发布时间】:2020-12-24 01:55:54
【问题描述】:

所以,我已经编写了所有代码,但是 return 语句没有返回或打印输出中的任何内容。 return 语句在我的 calAverage 方法中。输出应该看起来像这样https://gyazo.com/328bcfebfb08709edbc0e62a93ada7f8,但除了 calAverage 输出之外,我什么都有。我不明白我做错了什么。我知道我必须调用这样的方法: 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);
        //
    }
}

【问题讨论】:

  • 你没有使用返回值。
  • 但我想使用没有 System.out.println() 的 return 语句;
  • System.out.println(sc.calAverage(a, b, c));?
  • 我的意思是没有 System.out.println();
  • 双平均 = sc.calAverage(a, b, c);

标签: java printing return output


【解决方案1】:

你应该改变

sc.calAverage(a, b, c)

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

如果您想打印calAverage 方法返回的值,请在您的 main 方法中。

或者在方法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;
    }

【讨论】:

    【解决方案2】:

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

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

    【讨论】: