英文:
Java: nextDouble get numbers from user message doesn't get printed
问题
import java.util.Scanner;
public class Test3 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double total = 0;
do {
System.out.print("Enter dog sizes or END to end: ");
while (scanner.hasNextDouble()) {
total += scanner.nextDouble();
}
scanner.nextLine();
String q = scanner.nextLine();
if (q.equals("END")) {
break;
}
} while (true);
System.out.println("Total dog sizes: " + total);
}
}
英文:
My output is:
Enter dog sizes or END to end: 1
2
3
4
END
Total dog sizes: 10.0
Why I need it to be
Enter dog sizes or END to end: 1
Enter dog sizes or END to end: 2 3
Enter dog sizes or END to end: 4
Enter dog sizes or END to end: END
Total dog sizes: 10.0
import java.util.Scanner;
public class Test3 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double total = 0;
do {
System.out.print("Enter dog sizes or END to end: ");
while (scanner.hasNextDouble()) {
total += scanner.nextDouble();
}
scanner.nextLine();
String q = scanner.nextLine();
if (q.equals("END")) {
break;
}
} while (true);
System.out.println("Total dog sizes: " + total);
}
}
答案1
得分: 1
消息之所以没有被简单地打印出来,是因为在您的代码中在读取下一个双精度数后您没有将其打印出来。您可以在while循环中加入打印语句,以重新打印像您所描述的消息:
while (scanner.hasNextDouble()) {
total += scanner.nextDouble();
System.out.print("输入狗的大小或输入 END 结束:");
}
英文:
The message is not being printed simply because you don't print it after reading the next double in your code. You can enter the print statement in your while loop to re-print the message like you describe:
while (scanner.hasNextDouble()) {
total += scanner.nextDouble();
System.out.print("Enter dog sizes or END to end: ");
}
答案2
得分: 1
Your parent do..while loop only gets executed once, so the message will only get printed once. If you want it printed on every line then you can do this:
Scanner scanner = new Scanner(System.in);
double total = 0;
do {
System.out.print("Enter dog sizes or END to end: ");
String q = scanner.nextLine();
try {
String[] str = q.split("\\s+");
for(String s: str) {
total += Double.parseDouble(s);
}
} catch(NumberFormatException e) { //invalid double
if(q.toUpperCase().equals("END")) {
break; //stop if END or end is entered
}
continue; //skip over other invalid doubles
}
} while(true);
System.out.println("Total dog sizes: " + total);
英文:
EDIT:
Your parent do..while loop only gets executed once, so the message will only get printed once. If you want it printed on every line then you can do this:
Scanner scanner = new Scanner(System.in);
double total = 0;
do {
System.out.print("Enter dog sizes or END to end: ");
String q = scanner.nextLine();
try {
String[] str = q.split("\\s+");
for(String s: str) {
total += Double.parseDouble(s);
}
}catch(NumberFormatException e) { //invalid double
if(q.toUpperCase().equals("END")) {
break; //stop if END or end is entered
}
continue; //skip over other invalid doubles
}
} while(true);
System.out.println("Total dog sizes: " + total);
I read a whole line, and then split the numbers at white spaces.
答案3
得分: 1
也许你应该这样做(在代码中阅读注释):
Scanner scanner = new Scanner(System.in);
double total = 0;
String dogSizeStrg = "";
while (dogSizeStrg.isEmpty()) {
System.out.print("输入狗狗大小或输入 END 结束:");
dogSizeStrg = scanner.nextLine().trim();
if (dogSizeStrg.equalsIgnoreCase("end")) {
break;
}
// 输入的数值是有符号还是无符号整数或浮点数?
if (dogSizeStrg.matches("-?\\d+(\\.\\d+)?")) {
// 是的,是数值。
total += Double.valueOf(dogSizeStrg); // 转换为 double 并加到总和中。
} else {
// 不是数值,通知用户并让其重试。
System.err.println("无效的数值输入!(" +
dogSizeStrg + ")请重试...");
}
dogSizeStrg = ""; // 重置以重新循环...
}
System.out.println("狗狗大小总和:" + total);
在这里,使用了 Scanner#nextLine() 方法,结合 String#matches() 方法和一个 正则表达式 (RegEx) 来验证是否输入了有符号或无符号整数或浮点数值。
英文:
Perhaps you should do it this way (read the comments in code):
Scanner scanner = new Scanner(System.in);
double total = 0;
String dogSizeStrg = "";
while (dogSizeStrg.isEmpty()) {
System.out.print("Enter dog sizes or END to end: ");
dogSizeStrg = scanner.nextLine().trim();
if (dogSizeStrg.equalsIgnoreCase("end")) {
break;
}
// Is the entered numerical value a signed or
// unsigned Integer or floating point number?
if (dogSizeStrg.matches("-?\\d+(\\.\\d+)?")) {
// Yes...it is.
total+= Double.valueOf(dogSizeStrg); // Convert to double and add to total.
}
else {
// No... it isn't so inform User and let
// him/her try again.
System.err.println("Invalid numerical value supplied! (" +
dogSizeStrg + ") Try Again...");
}
dogSizeStrg = ""; // Reset to loop again...
}
System.out.println("Total dog sizes: " + total);
The Scanner#nextLine() method is used here along with the String#matches() method and a Regular Expression (RegEx) to validate that a signed or unsigned Integer or floating point value was supplied.
答案4
得分: 1
public class Test3
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
double total = 0;
String q = "";
do
{
System.out.print("Enter dog sizes or END to end: ");
if(scanner.hasNextDouble())
{
String s[]= scanner.nextLine().split(" ");
for(int i =0 ;i < s.length;i++)
total+= Double.parseDouble(s[i]);
}
else
{
q = scanner.nextLine();
}
} while (!q.equals("END"));
scanner.close();
System.out.println("Total dog sizes: " + total);
}
}
英文:
You have input separated by space, so you have to use nextLine().split(“ ”)
as shown below and get your desired output:
public class Test3
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
double total = 0;
String q = "";
do
{
System.out.print("Enter dog sizes or END to end: ");
if(scanner.hasNextDouble())
{
String s[]= scanner.nextLine().split(" ");
for(int i =0 ;i < s.length;i++)
total+= Double.parseDouble(s[i]);
}
else
{
q = scanner.nextLine();
}
} while (!q.equals("END"));
scanner.close();
System.out.println("Total dog sizes: " + total);
}
}
答案5
得分: 1
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double total = 0;
do {
System.out.print("输入狗的大小或输入END结束:");
String q = scanner.nextLine();
// 使用输入字符串创建一个新的Scanner
Scanner doubles = new Scanner(q);
while (doubles.hasNextDouble()) {
total += doubles.nextDouble();
}
if (q.equals("END")) {
break;
}
} while (true);
System.out.println("狗的总大小:" + total);
}
}
// 示例运行:
// 输入狗的大小或输入END结束:1
// 输入狗的大小或输入END结束:2 3
// 输入狗的大小或输入END结束:4
// 输入狗的大小或输入END结束:5
// 输入狗的大小或输入END结束:END
// 狗的总大小:15.0
// 此外,注意当您在无限循环中使用条件来中断时,可以简单地使用 `while (true) {//...}`,而不是 `do {//...} while(true);`。
// 例如,下面的代码与上面的代码具有相同的行为。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double total = 0;
while (true) {
System.out.print("输入狗的大小或输入END结束:");
String q = scanner.nextLine();
// 使用输入字符串创建一个新的Scanner
Scanner doubles = new Scanner(q);
while (doubles.hasNextDouble()) {
total += doubles.nextDouble();
}
if (q.equals("END")) {
break;
}
}
System.out.println("狗的总大小:" + total);
}
}
英文:
Do it as follows:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double total = 0;
do {
System.out.print("Enter dog sizes or END to end: ");
String q = scanner.nextLine();
// Create a new Scanner with the input string
Scanner doubles = new Scanner(q);
while (doubles.hasNextDouble()) {
total += doubles.nextDouble();
}
if (q.equals("END")) {
break;
}
} while (true);
System.out.println("Total dog sizes: " + total);
}
}
A sample run:
Enter dog sizes or END to end: 1
Enter dog sizes or END to end: 2 3
Enter dog sizes or END to end: 4
Enter dog sizes or END to end: 5
Enter dog sizes or END to end: END
Total dog sizes: 15.0
Also, note that when you use a condition inside an infinite loop to break it, you can simply use while (true) {//...}
instead of do {//...} while(true);
e.g. the following code will behave in the same way as the above one.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double total = 0;
while (true) {
System.out.print("Enter dog sizes or END to end: ");
String q = scanner.nextLine();
// Create a new Scanner with the input string
Scanner doubles = new Scanner(q);
while (doubles.hasNextDouble()) {
total += doubles.nextDouble();
}
if (q.equals("END")) {
break;
}
}
System.out.println("Total dog sizes: " + total);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论