各位程序员好。
我有以下代码:public void命令(({
boolean runSystem = true;
while (runSystem) {
Scanner sc = new Scanner(System.in);
//int userInput = sc.nextInt();
System.out.println("""
1. Division.
2. Multiplication.
3. Substraction.
4. Addition.
9. Exit program.
Enter command:
""");
try {
switch (sc.nextInt()) {
case 1:
System.out.println("""
You have selected division.
Please enter the first number you wish to divide, then press enter.
Please use comma (,) to seperate periods
""");
double argOneDivision = sc.nextDouble();
System.out.println("Please enter the second number");
double argTwoDivision = sc.nextDouble();
controller.divisionCalc(argOneDivision, argTwoDivision);
System.out.println("We are now dividing " + argOneDivision + " / " + argTwoDivision + " which equeals: ");
System.out.format("%.2f", (controller.divisionCalc(argOneDivision, argTwoDivision)));
System.out.println(" ");
break;
case 2:
System.out.println("""
You have selected multiplciation.
Please enter the first number you wish to divide, then press enter.
Please use comma (,) to seperate periods
""");
double argOneMultiplication = sc.nextDouble();
System.out.println("Please enter the second number");
double argTwoMultiplication = sc.nextDouble();
controller.multiplicationCalc(argOneMultiplication, argTwoMultiplication);
System.out.println("We are now multiplying " + argOneMultiplication + " * " + argTwoMultiplication + " which equeals: ");
System.out.format("%.2f", (controller.multiplicationCalc(argOneMultiplication, argTwoMultiplication)));
System.out.println(" ");
break;
case 3:
System.out.println("""
You have selected substraction.
Please enter the first number you wish to divide, then press enter.
Please use comma (,) to seperate periods.
To indicate it is a minus number, use - at the start of the number.
""");
double argOneSubstraction = sc.nextDouble();
System.out.println("Please enter the second number");
double argTwoSubstraction = sc.nextDouble();
controller.substractionCalc(argOneSubstraction, argTwoSubstraction);
System.out.println("We are now substracting " + argOneSubstraction + " - " + argTwoSubstraction + " which equeals: ");
System.out.format("%.2f", (controller.substractionCalc(argOneSubstraction, argTwoSubstraction)));
System.out.println(" ");
break;
case 4:
System.out.println("""
You have selected addition.
Please enter the first number you wish to divide, then press enter.
Please use comma (,) to seperate periods.
To indicate it is a minus number, use - at the start of the number.
""");
double argOneAddition = sc.nextDouble();
System.out.println("Please enter the second number");
double argTwoAddition = sc.nextDouble();
controller.additionCalc(argOneAddition, argTwoAddition);
System.out.println("We are now substracting " + argOneAddition + " + " + argTwoAddition + " which equeals: ");
System.out.format("%.2f", (controller.additionCalc(argOneAddition, argTwoAddition)));
System.out.println(" ");
break;
case 9:
System.out.println("Closing program");
runSystem = false;
break;
default:
System.out.println("Not a valid option");
controller.checkIfInt();
break;
}
} catch (InputMismatchException e) {
System.out.println("Invalid input. Please read instrucctions clearly.");
}
}
}
}
我希望它在用户通过终端输入命令时每五次输出一个字符串。如果它能在我的";计算器";同学们,因为我想确保我遵循GRASP原则。
但如果它太笨重,我可以忍受它在";UI";类,这也是该代码的来源。
我试过一些不同的循环,但出于愤怒把它们都删除了。
希望有人能帮忙。
由于您关心原理,我将解释每做n次,什么样的基本数学运算将解决的问题。
您想要使用模运算,它返回除法的(有符号(余数。Java(以及许多其他编程语言(中的模运算符是%
。
例如,如果你计算模5:
1 % 5 = 1 6 % 5 = 1
2 % 5 = 2 7 % 5 = 2
3 % 5 = 3 8 % 5 = 3
4 % 5 = 4 9 % 5 = 4
5 % 5 = 0 10 % 5 = 0 <== Every 5-th time modulo returns 0
因此,在Java程序中,如果你想每5次做一次,只需检查模5运算是否返回零。
public class Application {
public static void main(String[] args) {
for (var i = 1; i <= 17; i++) {
if(i % 5 == 0) System.out.println("I am a 5-th iteration");
System.out.println(i);
}
}
}
这将打印以下内容:
1
2
3
4
I am a 5-th iteration
5
6
7
8
9
I am a 5-th iteration
10
11
12
13
14
I am a 5-th iteration
15
16
17
使用这个想法应该不难实现你想要的。