用不同的Java方法访问变量
我正在学习方法,并得到了一个练习。我有点不确定如何处理这个问题 我们得到的问题是:用不同的Java方法访问变量,java,methods,Java,Methods,我正在学习方法,并得到了一个练习。我有点不确定如何处理这个问题 我们得到的问题是: Modify the above program so the conversion is done in a method. 这是到目前为止我的代码,我的问题是当我运行代码时,直到我输入字母时,它停止 //Exercise 3 Brian Sheet 5 //修改上述程序,以便在方法中完成转换 import java.util.Scanner; public class Exercise3
Modify the above program so the conversion is done in a method.
这是到目前为止我的代码,我的问题是当我运行代码时,直到我输入字母时,它停止
//Exercise 3 Brian Sheet 5
//修改上述程序,以便在方法中完成转换
import java.util.Scanner;
public class Exercise3 {
public static void main(String[] args) {
double temp;
String c = "c";
String f = "f";
String a;
Scanner input = new Scanner(System.in);
System.out.println("Please enter the temperature: ");
temp = input.nextDouble();
input = new Scanner(System.in);
System.out
.println("Please enter whether you wish to convert to Celsius or Fahrenheit(c or f)");
a = input.nextLine();
if (a.equals(c)) {
celsiusEq(temp);
} else {
Fahren(temp);
}
}
private static double celsiusEq(double celsius) {
double temp;
celsius = (temp - 32) * 5 / 9;
return celsius;
}
private static double Fahren(double fahrenheit) {
double temp;
fahrenheit = temp * 9 / 5 + 32;
return fahrenheit;
}
}
我不知道我做错了什么,可能很简单。如果有人能帮助我,我将非常感激,因为我已经看了30分钟了 在这里,您需要交换temp和CECLICES变量才能正常工作
private static double celsiusEq(double celsius) {
double temp; //here is the problem
celsius = (temp - 32) * 5 / 9;
return celsius;
}
private static double Fahren(double fahrenheit) {
double temp; //here is the problem
fahrenheit = temp * 9 / 5 + 32;
return fahrenheit;
}
在这里,您需要交换温度和华氏度变量才能正常工作
private static double celsiusEq(double celsius) {
double temp; //here is the problem
celsius = (temp - 32) * 5 / 9;
return celsius;
}
private static double Fahren(double fahrenheit) {
double temp; //here is the problem
fahrenheit = temp * 9 / 5 + 32;
return fahrenheit;
}
此处更正
private static double celsiusEq(double temp){
double celsius;
celsius = (temp - 32) * 5 / 9;
return celsius;
}
private static double Fahren(double temp){
double fahrenheit;
fahrenheit = temp * 9 / 5 + 32;
return fahrenheit;
}
更新请求
returntype functionname(argumenttype argument2 ,argumenttype argument2,....argumenttype argumentN ){
// local variable declaration
variableype variable1;
variableype variable2;
----------------------
variableype variableN;
Your calculation
return your value based on the return type;
}
请参阅此处的更多详细信息
在这里,您需要交换温度和摄氏度变量才能正常工作
private static double celsiusEq(double celsius) {
double temp; //here is the problem
celsius = (temp - 32) * 5 / 9;
return celsius;
}
private static double Fahren(double fahrenheit) {
double temp; //here is the problem
fahrenheit = temp * 9 / 5 + 32;
return fahrenheit;
}
在这里,您需要交换温度和华氏度变量才能正常工作
private static double celsiusEq(double celsius) {
double temp; //here is the problem
celsius = (temp - 32) * 5 / 9;
return celsius;
}
private static double Fahren(double fahrenheit) {
double temp; //here is the problem
fahrenheit = temp * 9 / 5 + 32;
return fahrenheit;
}
此处更正
private static double celsiusEq(double temp){
double celsius;
celsius = (temp - 32) * 5 / 9;
return celsius;
}
private static double Fahren(double temp){
double fahrenheit;
fahrenheit = temp * 9 / 5 + 32;
return fahrenheit;
}
更新请求
returntype functionname(argumenttype argument2 ,argumenttype argument2,....argumenttype argumentN ){
// local variable declaration
variableype variable1;
variableype variable2;
----------------------
variableype variableN;
Your calculation
return your value based on the return type;
}
请参阅此处的更多详细信息
尝试以下代码
import java.util.Scanner;
public class Exercise3 {
public static void main(String[] args) {
double temp;
String c = "c";
String f = "f";
String a;
Scanner input = new Scanner(System.in);
System.out.println("Please enter the temperature: ");
temp = input.nextDouble();
input = new Scanner(System.in);
System.out.println("Please enter whether you wish to convert to Celsius or Fahrenheit(c or f)");
a = input.nextLine();
if(a.equals(c)){
System.out.println(celsiusEq(temp));
}else{
Fahren(temp);
}
}
private static double celsiusEq(double celsius){
double temp = 0;
celsius = (temp - 32) * 5 / 9;
return celsius;
}
private static double Fahren(double fahrenheit){
double temp = 0;
fahrenheit = temp * 9 / 5 + 32;
return fahrenheit;
}
}
输出
Please enter the temperature:
250
Please enter whether you wish to convert to Celsius or Fahrenheit(c or f)
f
32.0
请尝试以下代码
import java.util.Scanner;
public class Exercise3 {
public static void main(String[] args) {
double temp;
String c = "c";
String f = "f";
String a;
Scanner input = new Scanner(System.in);
System.out.println("Please enter the temperature: ");
temp = input.nextDouble();
input = new Scanner(System.in);
System.out.println("Please enter whether you wish to convert to Celsius or Fahrenheit(c or f)");
a = input.nextLine();
if(a.equals(c)){
System.out.println(celsiusEq(temp));
}else{
Fahren(temp);
}
}
private static double celsiusEq(double celsius){
double temp = 0;
celsius = (temp - 32) * 5 / 9;
return celsius;
}
private static double Fahren(double fahrenheit){
double temp = 0;
fahrenheit = temp * 9 / 5 + 32;
return fahrenheit;
}
}
输出
Please enter the temperature:
250
Please enter whether you wish to convert to Celsius or Fahrenheit(c or f)
f
32.0
代码中存在编译时错误: 如果不初始化局部变量,就不能使用它。 在celsiusEq内部,Fahren方法初始化温度变量,如:
double temp = 0;
那么它应该会起作用
仅供参考,请阅读以下文章
代码中存在编译时错误: 如果不初始化局部变量,就不能使用它。 在celsiusEq内部,Fahren方法初始化温度变量,如:
double temp = 0;
那么它应该会起作用
仅供参考,请阅读以下文章
实际上,这很简单。代码如下: 摄氏到华氏
private static double celsiusToFahrenheit(double celsius)
{
double fahrenheit;
fahrenheit = ((celsius * 9) / 5) + 32;
return fahrenheit;
}
华氏到摄氏度
private static double fahrenheitToCelsius(double fahrenheit)
{
double celsius;
celsius = ((fahrenheit - 32) * 5) / 9;
return celsius;
}
执行数字操作时始终使用括号。这是一个很好的编程习惯
以下是您的代码的错误:
private static double celsiusEq(double celsius)
{
double temp; //TEMP HAS NO VALUE
celsius = (temp - 32) * 5 / 9; //STILL YOU ARE USING IT
return celsius;
}
private static double Fahren(double fahrenheit)
{
double temp; //TEMP HAS NO VALUE
fahrenheit = temp * 9 / 5 + 32; //STILL YOU ARE USING IT
return fahrenheit;
}
你没有使用摄氏度和华氏度变量,而是使用了温度变量。其实很简单。代码如下: 摄氏到华氏
private static double celsiusToFahrenheit(double celsius)
{
double fahrenheit;
fahrenheit = ((celsius * 9) / 5) + 32;
return fahrenheit;
}
华氏到摄氏度
private static double fahrenheitToCelsius(double fahrenheit)
{
double celsius;
celsius = ((fahrenheit - 32) * 5) / 9;
return celsius;
}
执行数字操作时始终使用括号。这是一个很好的编程习惯
以下是您的代码的错误:
private static double celsiusEq(double celsius)
{
double temp; //TEMP HAS NO VALUE
celsius = (temp - 32) * 5 / 9; //STILL YOU ARE USING IT
return celsius;
}
private static double Fahren(double fahrenheit)
{
double temp; //TEMP HAS NO VALUE
fahrenheit = temp * 9 / 5 + 32; //STILL YOU ARE USING IT
return fahrenheit;
}
您使用的不是摄氏度和华氏度变量,而是温度变量。这是您的代码:
private static double celsiusEq(double temp){
double celsius;
celsius = (temp - 32) * 5 / 9;
return celsius;
}
private static double Fahren(double temp){
double fahrenheit;
fahrenheit = temp * 9 / 5 + 32;
return fahrenheit;
}
需要进行以下几项更正:
1.)既然您将temp
作为参数传递,为什么不在函数中使用它呢
private static double Fahren(double temp){
double fahrenheit;
fahrenheit = temp * 9 / 5 + 32;
return fahrenheit;
}
2.)尝试将此类方法公开
,而不是私有
。提供更容易的访问和操作
3.)由于函数返回一个双精度
,因此需要捕获结果以打印/修改它。像这样:
double answerInCelcius = celsiusEq(temp);
System.out.println("Answer in Celsius is :" +answerInCelsius);
希望有帮助:)这是您的代码:
private static double celsiusEq(double temp){
double celsius;
celsius = (temp - 32) * 5 / 9;
return celsius;
}
private static double Fahren(double temp){
double fahrenheit;
fahrenheit = temp * 9 / 5 + 32;
return fahrenheit;
}
需要进行以下几项更正:
1.)既然您将temp
作为参数传递,为什么不在函数中使用它呢
private static double Fahren(double temp){
double fahrenheit;
fahrenheit = temp * 9 / 5 + 32;
return fahrenheit;
}
2.)尝试将此类方法公开
,而不是私有
。提供更容易的访问和操作
3.)由于函数返回一个双精度
,因此需要捕获结果以打印/修改它。像这样:
double answerInCelcius = celsiusEq(temp);
System.out.println("Answer in Celsius is :" +answerInCelsius);
希望有帮助:)您需要将方法更改为
private static double celsiusEq(double temp) {
double celsius;
celsius = (temp - 32) * 5 / 9;
return celsius;
}
private static double Fahren(double temp) {
double fahrenheit;
fahrenheit = temp * 9 / 5 + 32;
return fahrenheit;
}
您的变量名已互换。您需要将方法更改为
private static double celsiusEq(double temp) {
double celsius;
celsius = (temp - 32) * 5 / 9;
return celsius;
}
private static double Fahren(double temp) {
double fahrenheit;
fahrenheit = temp * 9 / 5 + 32;
return fahrenheit;
}
您的变量名被互换了。我将执行以下操作:
import java.util.Scanner;
public class Exercise3 {
public static void main(String[] args) {
double temp;
String a;
System.out.println("Please enter the temperature: ");
Scanner input = new Scanner(System.in);
temp = input.nextDouble();
System.out.println("Please enter whether you wish to convert to Celsius or Fahrenheit(c or f)");
a = input.nextLine();
switch(a)
{
case "c":
temp = toCelsius(temp);
System.out.println("Temp in Celsius: " + temp);
break;
case "f":
temp = toFahren(temp);
System.out.println("Temp in Fahren: " + temp);
break;
default:
System.out.println("Invalid Entry");
}
}
private static double toCelsius (double fahren){
return (fahren - 32) * 5 / 9;
}
private static double toFahren(double celsius){
return celsius * 9 / 5 + 32;
}
}
但是嘿!很多其他人把我揍了一顿。我会做如下事情:
import java.util.Scanner;
public class Exercise3 {
public static void main(String[] args) {
double temp;
String a;
System.out.println("Please enter the temperature: ");
Scanner input = new Scanner(System.in);
temp = input.nextDouble();
System.out.println("Please enter whether you wish to convert to Celsius or Fahrenheit(c or f)");
a = input.nextLine();
switch(a)
{
case "c":
temp = toCelsius(temp);
System.out.println("Temp in Celsius: " + temp);
break;
case "f":
temp = toFahren(temp);
System.out.println("Temp in Fahren: " + temp);
break;
default:
System.out.println("Invalid Entry");
}
}
private static double toCelsius (double fahren){
return (fahren - 32) * 5 / 9;
}
private static double toFahren(double celsius){
return celsius * 9 / 5 + 32;
}
}
private static double celsiusEq(double temp) {
return (temp - 32) * 5 / 9;
}
private static double Fahren(double temp) {
return temp * 9 / 5 + 32;
}
但是嘿!很多人把我揍了一顿
private static double celsiusEq(double temp) {
return (temp - 32) * 5 / 9;
}
private static double Fahren(double temp) {
return temp * 9 / 5 + 32;
}
别忘了打印一个类似System.out.println(celsiusEq(temp));:-)的结果
别忘了打印一个类似System.out.println(celsiusEq(temp));:-)的结果 您的方法只是混淆了side方法中的temp和cellics变量。所以我更改了代码,下面是代码的更新版本
public class Exercise3 {
public static void main(String[] args) {
double temp;
String c = "c";
String f = "f";
String a;
Scanner input = new Scanner(System.in);
System.out.println("Please enter the temperature: ");
temp = input.nextDouble();
input = new Scanner(System.in);
System.out
.println("Please enter whether you wish to convert to Celsius or Fahrenheit(c or f)");
a = input.nextLine();
if (a.equals(c)) {
celsiusEq(temp);
} else {
Fahren(temp);
}
}
private static double celsiusEq(double temp ) {
double celsius;
celsius = (temp - 32) * 5 / 9;
System.out.println("AFTER CONVERTION " + celsius);
return celsius;
}
private static double Fahren(double temp ) {
double fahrenheit;
fahrenheit = temp * 9 / 5 + 32;
System.out.println("AFTER CONVERTION " + fahrenheit);
return fahrenheit;
}
}
您的方法只是混淆了side方法中的temp和cercil变量。所以我更改了代码,下面是代码的更新版本
public class Exercise3 {
public static void main(String[] args) {
double temp;
String c = "c";
String f = "f";
String a;
Scanner input = new Scanner(System.in);
System.out.println("Please enter the temperature: ");
temp = input.nextDouble();
input = new Scanner(System.in);
System.out
.println("Please enter whether you wish to convert to Celsius or Fahrenheit(c or f)");
a = input.nextLine();
if (a.equals(c)) {
celsiusEq(temp);
} else {
Fahren(temp);
}
}
private static double celsiusEq(double temp ) {
double celsius;
celsius = (temp - 32) * 5 / 9;
System.out.println("AFTER CONVERTION " + celsius);
return celsius;
}
private static double Fahren(double temp ) {
double fahrenheit;
fahrenheit = temp * 9 / 5 + 32;
System.out.println("AFTER CONVERTION " + fahrenheit);
return fahrenheit;
}
}
我希望这能帮助你:你把
return
和System.out.println
搞混了。第一种方法允许调用方使用被调用方法返回的内容。第二个打印到屏幕上。您的代码调用一个方法,该方法返回一个double,但对返回的doube值完全不做任何操作。我希望这能帮助您:您将return
与System.out.println
混淆了。第一种方法允许调用方使用被调用方法返回的内容。第二个打印到屏幕上。您的代码调用一个方法,该方法返回一个double,但对返回的doube值完全不做任何操作。因此,要在方法中使用变量,需要在括号中声明它?这是一个参数。它是在调用方法时传入的一个值。因此,要在方法中使用变量,需要在括号中声明它?这是一个参数。它是在调用方法时传入的值。