Java 我收到以下消息:错误:无法将b解析为变量
这是我的代码。有人能帮我弄清楚吗?我收到一个编译器错误消息: 发现2个错误: 文件:C:\Users\Keith\Desktop\Prog Files\hw4chrismancher.java[行:13] 错误:无法将解析为变量 文件:C:\Users\Keith\Desktop\Prog Files\hw4chrismancher.java[行:19] 错误:无法将b解析为变量Java 我收到以下消息:错误:无法将b解析为变量,java,java.util.scanner,Java,Java.util.scanner,这是我的代码。有人能帮我弄清楚吗?我收到一个编译器错误消息: 发现2个错误: 文件:C:\Users\Keith\Desktop\Prog Files\hw4chrismancher.java[行:13] 错误:无法将解析为变量 文件:C:\Users\Keith\Desktop\Prog Files\hw4chrismancher.java[行:19] 错误:无法将b解析为变量 import java.util.Scanner; public class HW4ChrisMuncher {
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;
}
}
import java.util.Scanner;
公共级HW4Chrismancher
{
公共静态void main(字符串[]args)
{
扫描仪输入=新扫描仪(System.in);
System.out.println(“输入一个数字”);
double userNum=input.nextDouble();
a=用户数;
扫描仪输入2=新扫描仪(System.in);
System.out.println(“输入一个数字(无小数!)”;
double userNum2=input2.nextDouble();
b=用户num2;
双a;
int b;
双c=最小值(a,b);
双d=最大值(a,b);
双e=abs(a);
双f=功率(a,b);
System.out.println(“11和8的最小值为”+c);
System.out.println(“11和8的最大值为”+d);
System.out.println(“11.5的绝对值为”+e);
System.out.println(“11.5乘以8的幂等于”+f);
}
//返回两个数字中的最小值
公共静态双最小值(双n1,内部n2)
{
双分钟;
如果(n1>n2)
min=n2;
其他的
min=n1;
返回最小值;
}
//返回两个数字之间的最大值
公共静态双最大值(双n1,整数n2)
{
双峰;
如果(n1>n2)
max=n1;
其他的
max=n2;
返回最大值;
}
//返回两个数字的绝对值
公共静态双abs(双n1)
{
如果(n1<0)
返回-n1;
其他的
返回n1;
}
公共静态双电源(双n1,int n2)
{
双f=1;
对于(int i=0;i
在声明之前,您试图使用a
和b
。将a
和b
的声明(即,行双a;
和int b;
)移动到首次使用它们之前。您正在执行:
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"
i、 在处理变量之前,必须先声明变量