在 I/O 中使用 Double.parseDouble( )
Using Double.parseDouble( ) within I/O
我正在尝试编写一个程序,从用户那里获取一系列的五个双精度字符串值,然后尝试使用 Double.parseDouble()
方法将这些字符串转换为双精度字符串。我遇到的麻烦是我必须在下一个“Please enter a number
”弹出之前输入两个值。这是我得到的输出:
Please enter a number 1: 4
3
Please enter a number 2: 5
4
Please enter a number 3: 3
4
Please enter a number 4: 5
6
Please enter a number 5: 4
2
The average is: 3.8
这是我的代码:
import java.io.*;
import java.util.Scanner;
public class theman {
public static void main(String[] args) throws IOException{
// Variables
int counter;
int userEntries = 5;
double sum = 0;
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
// Creating a new Scanner
Scanner scanner = new Scanner(System.in);
// Loop for the five entries
for (counter = 1; counter <= userEntries; counter++) {
String input = "";
System.out.print("Please enter a number " + counter + ": ");
input = br.readLine();
double number = Double.parseDouble(input);
sum += scanner.nextDouble();
}
// Print statements
System.out.println("The average is: " + sum / userEntries);
} // End of method header
} // End of class header
解决此问题的最佳方法是什么?我假设这是因为 sum += scanner.nextDouble();
但我需要它递增所以我真的不能删除它。
你已经有了双倍的,所以没有必要使用 Scanner.nextDouble()
。 nextDouble()
方法将输入的下一个标记扫描为双精度。你应该做的是 sum += number
.
这是更新后的代码 -
import java.io.*;
import java.util.Scanner;
public class theman {
public static void main(String[] args) throws IOException{
// Variables
int counter;
int userEntries = 5;
double sum = 0;
// Creating a new Scanner
Scanner scanner = new Scanner(System.in);
// Loop for the five entries
for (counter = 1; counter <= userEntries; counter++) {
System.out.print("Please enter a number " + counter + ": ");
sum += scanner.nextDouble();
}
// Print statements
System.out.println("The average is: " + sum / userEntries);
} // End of method header
} // End of class header
final int userEntries = 2;
Scanner in = new Scanner(System.in);
double sum = 0.0;
for (int counter = 1; counter <= userEntries; ++counter) {
System.out.printf("Please enter a number %d: ", counter);
sum += in.nextDouble();
}
System.out.printf("The average is: %.2f%n", sum / userEntries);
我正在尝试编写一个程序,从用户那里获取一系列的五个双精度字符串值,然后尝试使用 Double.parseDouble()
方法将这些字符串转换为双精度字符串。我遇到的麻烦是我必须在下一个“Please enter a number
”弹出之前输入两个值。这是我得到的输出:
Please enter a number 1: 4
3
Please enter a number 2: 5
4
Please enter a number 3: 3
4
Please enter a number 4: 5
6
Please enter a number 5: 4
2
The average is: 3.8
这是我的代码:
import java.io.*;
import java.util.Scanner;
public class theman {
public static void main(String[] args) throws IOException{
// Variables
int counter;
int userEntries = 5;
double sum = 0;
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
// Creating a new Scanner
Scanner scanner = new Scanner(System.in);
// Loop for the five entries
for (counter = 1; counter <= userEntries; counter++) {
String input = "";
System.out.print("Please enter a number " + counter + ": ");
input = br.readLine();
double number = Double.parseDouble(input);
sum += scanner.nextDouble();
}
// Print statements
System.out.println("The average is: " + sum / userEntries);
} // End of method header
} // End of class header
解决此问题的最佳方法是什么?我假设这是因为 sum += scanner.nextDouble();
但我需要它递增所以我真的不能删除它。
你已经有了双倍的,所以没有必要使用 Scanner.nextDouble()
。 nextDouble()
方法将输入的下一个标记扫描为双精度。你应该做的是 sum += number
.
这是更新后的代码 -
import java.io.*;
import java.util.Scanner;
public class theman {
public static void main(String[] args) throws IOException{
// Variables
int counter;
int userEntries = 5;
double sum = 0;
// Creating a new Scanner
Scanner scanner = new Scanner(System.in);
// Loop for the five entries
for (counter = 1; counter <= userEntries; counter++) {
System.out.print("Please enter a number " + counter + ": ");
sum += scanner.nextDouble();
}
// Print statements
System.out.println("The average is: " + sum / userEntries);
} // End of method header
} // End of class header
final int userEntries = 2;
Scanner in = new Scanner(System.in);
double sum = 0.0;
for (int counter = 1; counter <= userEntries; ++counter) {
System.out.printf("Please enter a number %d: ", counter);
sum += in.nextDouble();
}
System.out.printf("The average is: %.2f%n", sum / userEntries);