双变量输入
Double variable input
我目前正在学习 java。我正在尝试制作一个示例应用程序,当我们输入 4 个数字时,打印这些数字的平均值。
这是我的尝试:
package ave4numbers;
import java.util.Scanner;
public class Ave4Numbers {
double a,b,c,d;
double e = (a+b+c+d)/4;
Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
System.out.println("Enter your numbers ");
a = sc.nextDouble();
b = sc.nextDouble();
c = sc.nextDouble();
d = sc.nextDouble();
System.out.println(e);
}
}
但这不起作用。怎么了?
谢谢
你需要对这些变量进行初始化或者直接从input中获取,然后在print语句中进行加法和平均。
System.out.println((a+b+c+d)/4);
修改代码你应该有如下内容:
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter your numbers ");
double a = sc.nextDouble();
double b = sc.nextDouble();
double c = sc.nextDouble();
double d = sc.nextDouble();
System.out.println((a+b+c+d)/4);
}
您尝试在计算接受 a、b、c 和 d 的值之前打印 e
的值,改为执行此操作
package ave4numbers;
import java.util.Scanner;
public class Ave4Numbers {
double a,b,c,d;
Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
System.out.println("Enter your numbers ");
a = sc.nextDouble();
b = sc.nextDouble();
c = sc.nextDouble();
d = sc.nextDouble();
System.out.println((a+b+c+d)/4);
}
}
所有这些都是在 class 加载时发生的。 e 将为 0,因为 a、b、c 和 d 默认初始化为 0。
double a,b,c,d;
double e = (a+b+c+d)/4;
Scanner sc = new Scanner(System.in);
那么下面你没有在你的main方法中改变e的值,所以它仍然是0
System.out.println("Enter your numbers ");
a = sc.nextDouble();
b = sc.nextDouble();
c = sc.nextDouble();
d = sc.nextDouble();
System.out.println(e);
要修复,请执行
System.out.println("Enter your numbers ");
a = sc.nextDouble();
b = sc.nextDouble();
c = sc.nextDouble();
d = sc.nextDouble();
//set the value of e
e = (a+b+c+d)/4;
System.out.println(e);
除了在获得数字之前尝试进行数学运算的错误之外,您还需要在 main
方法中声明变量或将变量设为静态。
public static void main(String[] args) {
double a,b,c,d; //These don't have to be static, if they are inside your method.
Scanner sc = new Scanner(System.in);
System.out.println("Enter your numbers ");
a = sc.nextDouble();
b = sc.nextDouble();
c = sc.nextDouble();
d = sc.nextDouble();
double e = (a+b+c+d)/4; //Do math after we save the numbers
System.out.println(e);
}
或...
static double a,b,c,d; //If you leave these outside your method, it has to be static
static Scanner sc = new Scanner(System.in); //This as well
public static void main(String[] args) {
System.out.println("Enter your numbers ");
a = sc.nextDouble();
b = sc.nextDouble();
c = sc.nextDouble();
d = sc.nextDouble();
double e = (a+b+c+d)/4; //Do math after we save the numbers
System.out.println(e);
}
静态示例
public class Solution
{
public static void main(String[] args) {
//You can call static methods from another static method without an instance of the class.
//So you can do this if it is static. Notice I did not create an instance of Example.
Example.staticMethod();
System.out.println(Example.staticVariable);
//However these will NOT compile
//Example.nonStaticMethod
//System.out.println(Example.nonStaticVariable);
//If you want access to the nonStaticMethod you need an instance.
Example myExample = new Example();
myExample.nonStaticMethod(); //This WILL compile.
System.out.println(myExample.nonStaticVariable); //Will compile
}
}
class Example{
static String staticVariable = "";
public String nonStaticVariable = "";
public static void staticMethod(){
}
public void nonStaticMethod(){
}
}
我目前正在学习 java。我正在尝试制作一个示例应用程序,当我们输入 4 个数字时,打印这些数字的平均值。
这是我的尝试:
package ave4numbers;
import java.util.Scanner;
public class Ave4Numbers {
double a,b,c,d;
double e = (a+b+c+d)/4;
Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
System.out.println("Enter your numbers ");
a = sc.nextDouble();
b = sc.nextDouble();
c = sc.nextDouble();
d = sc.nextDouble();
System.out.println(e);
}
}
但这不起作用。怎么了? 谢谢
你需要对这些变量进行初始化或者直接从input中获取,然后在print语句中进行加法和平均。
System.out.println((a+b+c+d)/4);
修改代码你应该有如下内容:
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter your numbers ");
double a = sc.nextDouble();
double b = sc.nextDouble();
double c = sc.nextDouble();
double d = sc.nextDouble();
System.out.println((a+b+c+d)/4);
}
您尝试在计算接受 a、b、c 和 d 的值之前打印 e
的值,改为执行此操作
package ave4numbers;
import java.util.Scanner;
public class Ave4Numbers {
double a,b,c,d;
Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
System.out.println("Enter your numbers ");
a = sc.nextDouble();
b = sc.nextDouble();
c = sc.nextDouble();
d = sc.nextDouble();
System.out.println((a+b+c+d)/4);
}
}
所有这些都是在 class 加载时发生的。 e 将为 0,因为 a、b、c 和 d 默认初始化为 0。
double a,b,c,d;
double e = (a+b+c+d)/4;
Scanner sc = new Scanner(System.in);
那么下面你没有在你的main方法中改变e的值,所以它仍然是0
System.out.println("Enter your numbers ");
a = sc.nextDouble();
b = sc.nextDouble();
c = sc.nextDouble();
d = sc.nextDouble();
System.out.println(e);
要修复,请执行
System.out.println("Enter your numbers ");
a = sc.nextDouble();
b = sc.nextDouble();
c = sc.nextDouble();
d = sc.nextDouble();
//set the value of e
e = (a+b+c+d)/4;
System.out.println(e);
除了在获得数字之前尝试进行数学运算的错误之外,您还需要在 main
方法中声明变量或将变量设为静态。
public static void main(String[] args) {
double a,b,c,d; //These don't have to be static, if they are inside your method.
Scanner sc = new Scanner(System.in);
System.out.println("Enter your numbers ");
a = sc.nextDouble();
b = sc.nextDouble();
c = sc.nextDouble();
d = sc.nextDouble();
double e = (a+b+c+d)/4; //Do math after we save the numbers
System.out.println(e);
}
或...
static double a,b,c,d; //If you leave these outside your method, it has to be static
static Scanner sc = new Scanner(System.in); //This as well
public static void main(String[] args) {
System.out.println("Enter your numbers ");
a = sc.nextDouble();
b = sc.nextDouble();
c = sc.nextDouble();
d = sc.nextDouble();
double e = (a+b+c+d)/4; //Do math after we save the numbers
System.out.println(e);
}
静态示例
public class Solution
{
public static void main(String[] args) {
//You can call static methods from another static method without an instance of the class.
//So you can do this if it is static. Notice I did not create an instance of Example.
Example.staticMethod();
System.out.println(Example.staticVariable);
//However these will NOT compile
//Example.nonStaticMethod
//System.out.println(Example.nonStaticVariable);
//If you want access to the nonStaticMethod you need an instance.
Example myExample = new Example();
myExample.nonStaticMethod(); //This WILL compile.
System.out.println(myExample.nonStaticVariable); //Will compile
}
}
class Example{
static String staticVariable = "";
public String nonStaticVariable = "";
public static void staticMethod(){
}
public void nonStaticMethod(){
}
}