Java 为什么';return语句在calAverage方法中不返回任何内容
所以,我已经对所有内容进行了编码,但是return语句没有返回或打印输出中的任何内容。return语句在我的calAverage方法中。输出应该是这样的,但是除了calAverage输出,我什么都有。我不明白我做错了什么。我知道我必须这样调用这个方法:sc.calAverage(a,b,c);然后将返回值赋给一个变量并打印出来,但我不知道如何使用calAverage方法,因为它有三个参数Java 为什么';return语句在calAverage方法中不返回任何内容,java,printing,return,output,Java,Printing,Return,Output,所以,我已经对所有内容进行了编码,但是return语句没有返回或打印输出中的任何内容。return语句在我的calAverage方法中。输出应该是这样的,但是除了calAverage输出,我什么都有。我不明白我做错了什么。我知道我必须这样调用这个方法:sc.calAverage(a,b,c);然后将返回值赋给一个变量并打印出来,但我不知道如何使用calAverage方法,因为它有三个参数 import java.util.Scanner; public class SecretCode {
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.calaverage(5,3,2);
您应该更改
sc.calAverage(a、b、c)
到
如果要打印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;
}
您不使用返回的值,但我想使用不带System.out.println()的return语句<代码>系统输出打印LN(sc.calAverage(a、b、c))代码>?我的坏意思是没有系统。out.println();双倍平均值=sc.平均值(a、b、c);
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;
}