英文:
Java program that loops until sentinel value entered
问题
总之,我正试图整理我的代码。程序提示用户输入(a)dd、(s)ubtract、(m)ultiply、(d)ivide或(q)uit中的一个选项 - 每个字母与一个if语句相关联。 "q"将是退出程序的哨兵值。用户需要输入两个数字,然后由上述操作之一执行这些数字。我的问题是,我无法解决为什么会在"Would you like to (a)dd, (s)ubtract, (m)ultiply, (d)ivide or (q)uit?"旁边提示"Enter first number"。我可能忽视了最简单的事情。我希望我的解释已经足够清楚。此外,非常欢迎提出有关提高代码可读性的任何建议。
目标是询问用户如何执行他们的两个数字。一旦选择,他们将输入两个数字 - 紧接着是它们的和。一旦呈现出总和,会再次询问他们要选择哪种操作。
import java.util.Scanner;
public class ScratchCode {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String operationLetter;
int firstNumber;
int secondNumber;
System.out.println("Hello! Welcome to the world's worst calculator.\n");
do {
System.out.println("Would you like to (a)dd, (s)ubtract, (m)ultiply, (d)ivide, or (q)uit?");
operationLetter = input.nextLine();
System.out.println("Enter first number.");
firstNumber = input.nextInt();
System.out.println("Enter second number.");
secondNumber = input.nextInt();
if (operationLetter.equalsIgnoreCase("a")) {
int sum = firstNumber + secondNumber;
System.out.println("Great. " + firstNumber + " + " + secondNumber + " = " + sum);
}
else if (operationLetter.equalsIgnoreCase("s")) {
int sum = firstNumber - secondNumber;
System.out.println("Great. " + firstNumber + " - " + secondNumber + " = " + sum);
}
else if (operationLetter.equalsIgnoreCase("m")) {
int sum = firstNumber * secondNumber;
System.out.println("Great. " + firstNumber + " * " + secondNumber + " = " + sum);
}
else if (operationLetter.equalsIgnoreCase("d")) {
int sum = firstNumber / secondNumber;
System.out.println("Great. " + firstNumber + " / " + secondNumber + " = " + sum);
}
}
while(!operationLetter.equalsIgnoreCase("q"));
System.out.println("Goodbye !");
}
}
英文:
To summarize, I am attempting to clean my code up. The program is prompting a user to enter (a)dd, (s)ubtract, (m)ultiply, (d)ivide, or (q)uit - each letter is associated with an if statement. "q" will be the sentinel value exiting the program. User will need to enter two numbers which would then be executed by one of the above operations. My problem is that I cannot seem to solve why "Enter first number" is being prompted alongside "Would you like to (a)dd, (s)ubtract, (m)ultiply, (d)ivide, or (q)uit?". I am probably over looking the simplest thing. I hope my explanation was clear enough. Also, any suggestions on improving code readability would be greatly appreciated.
The goal was to ask the user how they would like to execute their two numbers. Once selected, they would then enter their two numbers - immediately followed by the sum. Once the sum is presented, they are asked again which operation to choice.
import java.util.Scanner;
public class ScratchCode {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String operationLetter;
int firstNumber;
int secondNumber;
System.out.println("Hello! Welcome to the world's worst calculator.\n");
do {
System.out.println("Would you like to (a)dd, (s)ubtract, (m)ultiply, (d)ivide, or (q)uit?");
operationLetter = input.nextLine();
System.out.println("Enter first number.");
firstNumber = input.nextInt();
System.out.println("Enter second number.");
secondNumber = input.nextInt();
if (operationLetter.equalsIgnoreCase("a")) {
int sum = firstNumber + secondNumber;
System.out.println("Great. " + firstNumber + " + " + secondNumber + " = " + sum);
}
else if (operationLetter.equalsIgnoreCase("s")) {
int sum = firstNumber - secondNumber;
System.out.println("Great. " + firstNumber + " - " + secondNumber + " = " + sum);
}
else if (operationLetter.equalsIgnoreCase("m")) {
int sum = firstNumber * secondNumber;
System.out.println("Great. " + firstNumber + " * " + secondNumber + " = " + sum);
}
else if (operationLetter.equalsIgnoreCase("d")) {
int sum = firstNumber / secondNumber;
System.out.println("Great. " + firstNumber + " / " + secondNumber + " = " + sum);
}
}
while(!operationLetter.equalsIgnoreCase("q"));
System.out.println("Goodbye !");
}
}
答案1
得分: 1
请将您的哨兵值输入更改为以下内容:
System.out.println("您想要进行 (a)dd、(s)ubtract、(m)ultiply、(d)ivide 还是 (q)uit?");
operationLetter = input.next();
问题出在 input.nextLine();
上。
英文:
Please change the input for your sentinel values as below:
System.out.println("Would you like to (a)dd, (s)ubtract, (m)ultiply, (d)ivide, or (q)uit?");
operationLetter = input.next();
The problem was with input.nextLine();
答案2
得分: 0
以下是翻译好的代码部分:
private void bestCalcEver() {
Scanner input = new Scanner(System.in);
String operationLetter;
int firstNumber;
int secondNumber;
System.out.println("你好!欢迎来到世界上最糟糕的计算器。\n");
do {
System.out.println("你想要进行加法(a)、减法(s)、乘法(m)、除法(d)还是退出(q)?");
operationLetter = input.nextLine();
System.out.println("输入第一个数字。");
firstNumber = input.nextInt();
System.out.println("输入第二个数字。");
secondNumber = input.nextInt();
switch (operationLetter.toLowerCase()) {
case "a":
printResult(firstNumber, secondNumber, firstNumber + secondNumber, "+");
break;
case "s":
printResult(firstNumber, secondNumber, firstNumber - secondNumber, "-");
break;
case "m":
printResult(firstNumber, secondNumber, firstNumber * secondNumber, "*");
break;
case "d":
if (secondNumber == 0) {
System.out.println("糟糕,我不能除以0,抱歉 =(");
} else {
printResult(firstNumber, secondNumber, firstNumber / secondNumber, "/");
}
break;
case "q":
System.out.println("再见!");
break;
default:
System.out.println("无效的操作符,抱歉。");
break;
}
} while (!operationLetter.equalsIgnoreCase("q"));
}
private void printResult(int firstNumber, int secondNumber, int result, String operator) {
System.out.println("太棒了。" + firstNumber + operator + secondNumber + " = " + result);
}
英文:
Not an answer just trying to improve a bit your code, first create a method if you se some code being repeated, second that seems a perfect usage of a switch case, here is my attempt to demonstrate that:
private void bestCalcEver() {
Scanner input = new Scanner(System.in);
String operationLetter;
int firstNumber;
int secondNumber;
System.out.println("Hello! Welcome to the world's worst calculator.\n");
do {
System.out.println("Would you like to (a)dd, (s)ubtract, (m)ultiply, (d)ivide, or (q)uit?");
operationLetter = input.nextLine();
System.out.println("Enter first number.");
firstNumber = input.nextInt();
System.out.println("Enter second number.");
secondNumber = input.nextInt();
switch (operationLetter.toLowerCase()) {
case "a":
printResult(firstNumber, secondNumber, firstNumber + secondNumber, "+");
break;
case "s":
printResult(firstNumber, secondNumber, firstNumber - secondNumber, "-");
break;
case "m":
printResult(firstNumber, secondNumber, firstNumber * secondNumber, "*");
break;
case "d":
if (secondNumber == 0) {
System.out.println("Oops, I can't divide by 0 sorry =(");
} else {
printResult(firstNumber, secondNumber, firstNumber / secondNumber, "/");
}
break;
case "q":
System.out.println("Goodbye !");
break;
default:
System.out.println("Invalid operator, sorry");
break;
}
} while(!operationLetter.equalsIgnoreCase("q"));
}
private void printResult(int firstNumber, int secondNumber, int result, String operator) {
System.out.println("Great. " + firstNumber + operator + secondNumber + " = " + result);
}
答案3
得分: 0
问题解决:
1. 需要清理缓冲区,因为存在一些未使用的数据需要清除,导致调用`input.nextLine();`会自动从缓冲区中获取数据,而在您的情况下,这不是预期的行为,所以需要清理一下。
2. 第二,哨兵的放置位置不正确,所以我稍微修改了循环,这样您可以进行比较。
3. 输入验证以及其他一些小的改进也可以进行,但我暂时跳过了。
代码:
class ScratchCode {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Hello! 欢迎使用世界上最糟糕的计算器。\n");
System.out.println("你想要做加法(a),减法(s),乘法(m),除法(d),还是退出(q)?");
String operationLetter = input.nextLine();
while (!operationLetter.equalsIgnoreCase("q")) {
System.out.println("输入第一个数字。");
int firstNumber = input.nextInt();
System.out.println("输入第二个数字。");
int secondNumber = input.nextInt();
if (operationLetter.equalsIgnoreCase("a")) {
int sum = firstNumber + secondNumber;
System.out.println("好的。" + firstNumber + " + " + secondNumber + " = " + sum);
} else if (operationLetter.equalsIgnoreCase("s")) {
int sum = firstNumber - secondNumber;
System.out.println("好的。" + firstNumber + " - " + secondNumber + " = " + sum);
} else if (operationLetter.equalsIgnoreCase("m")) {
int sum = firstNumber * secondNumber;
System.out.println("好的。" + firstNumber + " * " + secondNumber + " = " + sum);
} else if (operationLetter.equalsIgnoreCase("d")) {
int sum = firstNumber / secondNumber;
System.out.println("好的。" + firstNumber + " / " + secondNumber + " = " + sum);
}
// 清理缓冲区
input.nextLine();
System.out.println("你想要做加法(a),减法(s),乘法(m),除法(d),还是退出(q)?");
operationLetter = input.nextLine();
}
System.out.println("再见!");
}
}
英文:
Issues to Solve:
- The Buffer needs to cleaned up, it has some unused data that needs to cleaned, due to which call to
input.nextLine();
is automatically get's it from buffer, which in your case is not the expected behavior, so need to clean that up. - Second the placement of sentinel is not where it should be, so I changed the loop little bit that way you can compare
- Input validation, other minor improvements could be done, which I also skipped
Code:
class ScratchCode {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Hello! Welcome to the world's worst calculator.\n");
System.out.println("Would you like to (a)dd, (s)ubtract, (m)ultiply, (d)ivide, or (q)uit?");
String operationLetter = input.nextLine();
while (!operationLetter.equalsIgnoreCase("q")) {
System.out.println("Enter first number.");
int firstNumber = input.nextInt();
System.out.println("Enter second number.");
int secondNumber = input.nextInt();
if (operationLetter.equalsIgnoreCase("a")) {
int sum = firstNumber + secondNumber;
System.out.println("Great. " + firstNumber + " + " + secondNumber + " = " + sum);
} else if (operationLetter.equalsIgnoreCase("s")) {
int sum = firstNumber - secondNumber;
System.out.println("Great. " + firstNumber + " - " + secondNumber + " = " + sum);
} else if (operationLetter.equalsIgnoreCase("m")) {
int sum = firstNumber * secondNumber;
System.out.println("Great. " + firstNumber + " * " + secondNumber + " = " + sum);
} else if (operationLetter.equalsIgnoreCase("d")) {
int sum = firstNumber / secondNumber;
System.out.println("Great. " + firstNumber + " / " + secondNumber + " = " + sum);
}
// Buffer Cleanup
input.nextLine();
System.out.println("Would you like to (a)dd, (s)ubtract, (m)ultiply, (d)ivide, or (q)uit?");
operationLetter = input.nextLine();
}
System.out.println("Goodbye !");
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论