提问人:Nicholas Chill 提问时间:10/19/2022 更新时间:10/19/2022 访问量:38
如果存在空格,为什么还要单独检查用户输入字符串?
Why is the user input string being checked individually if there is whitespace?
问:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
String characters;
Scanner scan = new Scanner(System.in);
boolean quit = false;
do {
System.out.println("USD to SEK converter");
System.out.println("Enter 'quit' to exit the program");
System.out.print("Enter USD amount: ");
if (scan.hasNextDouble()) {
double usd = scan.nextDouble();
double sek = usd * 11.2;
System.out.println(sek);
}else if(scan.hasNextLine()) {
characters = scan.nextLine();
System.out.println(characters);
if (characters.equals("quit")) {
quit = true;
}
}
}while (!quit);
}
}
我正在制作一个 USD 到 SEK 的转换器,当我输入 1 + 时,我首先得到 1 USD 到 SEK 的转换,然后得到 +。为什么程序首先检查 1,然后检查 +,而不是同时检查两者?如果我输入 1+,我得到 1+。没有转换 1。如果我输入 1+2,我得到 1+2,当我输入 1 + 2 时,我首先得到 1 美元到 SEK 的转换,然后是 + 2。
答: 暂无答案
评论
next
hasNext
nextLine
hasNextLine
hasNextDouble
nextDouble
"1 +"
1
nextLine()
"quit"
Double.parseDouble