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

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

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

  1. import java.util.Scanner;
  2. public class SecretCode
  3. {
  4. //no instance variables
  5. public SecretCode(){
  6. }
  7. public double calAverage(int a, int b, int c){
  8. double average = 0.0;
  9. //your work - step 2
  10. average = (a + b + c) / 3.0;
  11. return average;
  12. }
  13. public void decodeMe(String s1){
  14. //your work here step 4
  15. //This method will take in a String and then process the string to produce output withe the following rules:
  16. // The first 5 characters are needed but must be uppercase()
  17. // The first integer will decrease by 121
  18. // The last number only takes the last 2 decimals
  19. // Print out 3 lines of data as followed:
  20. // XXXXX
  21. // XXX
  22. // XX
  23. String s = "Delta 230 ATA 23.75";
  24. s1 = s.substring(0, 5);
  25. String s2 = s1.toUpperCase();
  26. int wholeNumber = Integer.parseInt(s.substring(6, 9));
  27. int finalNumber = wholeNumber - 121;
  28. int lastNumber = Integer.parseInt(s.substring(17,19));
  29. System.out.println(s2 + "\n" + finalNumber + "\n" + lastNumber);
  30. }
  31. public static void main(String args[]){
  32. int a, b, c;
  33. String s;
  34. SecretCode sc = new SecretCode();
  35. Scanner myObj = new Scanner(System.in);
  36. System.out.println("Enter 3 numbers separated by space ");
  37. //your work step 3
  38. // receive 3 integer values and call calAverage() method
  39. // print out the average
  40. a = myObj.nextInt();
  41. b = myObj.nextInt();
  42. c = myObj.nextInt();
  43. sc.calAverage(a, b, c);
  44. //
  45. Scanner myObj1 = new Scanner(System.in);
  46. System.out.println("Enter a secret code below ");
  47. //Step enter the code: Delta 230 ATA 23.75
  48. s = myObj1.nextLine();
  49. sc.decodeMe(s);
  50. //
  51. }
  52. }
o4hqfura

o4hqfura1#

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

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

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

  1. public double calAverage(int a, int b, int c) {
  2. double average = 0.0;
  3. //your work - step 2
  4. average = (a + b + c) / 3.0;
  5. System.out.println(average);
  6. return average;
  7. }
gwbalxhn

gwbalxhn2#

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

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

相关问题