gpt4 book ai didi

java - 我收到此消息 : Error: b cannot be resolved to a variable

转载 作者:行者123 更新时间:2023-12-02 05:20:43 25 4
gpt4 key购买 nike

这是我的代码。有人可以帮我解决这个问题吗?我收到编译器错误消息:

发现 2 个错误:文件:C:\Users\Keith\Desktop\Prog Files\HW4ChrisMuncher.java [行:13]错误:a 无法解析为变量文件:C:\Users\Keith\Desktop\Prog Files\HW4ChrisMuncher.java [行:19]错误:b 无法解析为变量

import java.util.Scanner;
public class HW4ChrisMuncher
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);

System.out.println("Enter a number");
double userNum = input.nextDouble();
a = userNum;

Scanner input2 = new Scanner(System.in);

System.out.println("Enter a number (no decimals!)");
double userNum2 = input2.nextDouble();
b = userNum2;



double a;
int b;
double c = min(a, b);
double d = max(a, b);
double e = abs(a);
double f = pow(a, b);



System.out.println("Minimum Value of 11 and 8 is " + c );
System.out.println("Maximum Value of 11 and 8 is " +d );
System.out.println("The absolute value of 11.5 is " +e );
System.out.println("11.5 to the power of 8 is " +f );

}

// Returns the minimum of two numbers
public static double min(double n1, int n2)
{
double min;
if (n1 > n2)
min = n2;
else
min = n1;

return min;
}

// Return the max between two numbers
public static double max(double n1, int n2)
{
double max;
if (n1 > n2)
max = n1;
else
max = n2;

return max;
}


//Returns the absolute value of the two numbers
public static double abs(double n1)
{
if (n1 < 0)
return -n1;
else
return n1;

}


public static double pow(double n1, int n2)
{
double f = 1;
for (int i =0; i< n2; i++)
{
f = f * n1;
}
return f;

}


}

最佳答案

你正在做:

a = userNum;  //compiler: "WTF is a?? I dunno... Exception!!!!!!" 
b = userNum2; //compiler: "WTF is b?? Exception!!!!"

//...THEN:

double a; //compiler: "I didn't read this far, I stopped at the first exception."
int b;

你需要做:

double a;  //compiler: "okay, a is going to refer to a double" 
int b; //compiler: "okay, b is going to refer to an int"

//...THEN:

a = userNum; //compiler: "cool, a refers to THAT double"
b = userNum2; //compiler: "cool, b refers THAT int"

即在对变量进行任何操作之前,您必须声明您的变量。

关于java - 我收到此消息 : Error: b cannot be resolved to a variable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26538582/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com