Я недавно перешел на калькулятор, который пользователь на этом веб-сайте предложил мне из-за моего калькулятора, откровенно говоря, сосать. Вот код для калькулятора:Что именно означает символ% в контексте вывода в java
import java.util.Scanner;
class RealCalc {
// Declare the constants
private static final String YES = "YES";
private static final String MULTIPLY = "Multiply";
private static final String DIVIDE = "Divide";
private static final String SUBTRACT = "Subtract";
private static final String ADD = "Add";
public static void main(String args[]) {
// Using one only Scanner
Scanner sc = new Scanner(System.in);
double fnum, snum;
System.out.println("Hello, my name is Cortana,"
+ " would you like to perform operations with me?");
// Use equalsIgnoreCase to make it case-insensitive
if(sc.nextLine().equalsIgnoreCase(YES)) {
System.out.println("Good choice!\n");
System.out.print("Enter your first number: ");
fnum = sc.nextDouble();
System.out.print("Enter your second number: ");
snum = sc.nextDouble();
// This is used to clear the '\n' character following the previous
// integer input
sc.nextLine();
// Call the function to perform arithmetic operations
performOperation(sc, fnum, snum);
}
System.out.println("Thank you!");
}
public static void performOperation(Scanner sc, double fnum, double snum) {
System.out.println("\n" + MULTIPLY + ", " + DIVIDE + ", " + SUBTRACT + ", or " + ADD);
System.out.println("What do you want me to do with these numbers?");
String operation = sc.nextLine();
System.out.println();
if(operation.equalsIgnoreCase(MULTIPLY)) {
System.out.printf("%8.3f * %8.3f = %8.3f%n%n", fnum, snum, fnum * snum);
}
else if(operation.equalsIgnoreCase(DIVIDE)) {
System.out.printf("%8.3f/%8.3f = %8.3f%n%n", fnum, snum, fnum/snum);
}
else if(operation.equalsIgnoreCase(SUBTRACT)) {
System.out.printf("%8.3f - %8.3f = %8.3f%n%n", fnum, snum, fnum - snum);
}
else if(operation.equalsIgnoreCase(ADD)) {
System.out.printf("%8.3f + %8.3f = %8.3f%n%n", fnum, snum, fnum + snum);
}
else {
System.out.println("Sorry, I do not recognize this operation.\n");
// Call this function again to re-prompt the user for a valid operation
performOperation(sc, fnum, snum);
}
}
}
Проблемы я имею с выходами. Всякий раз, когда пользователь вводит два числа, он сохраняет их в две разные переменные: fNum и sNum. Поэтому я подумал: почему вы не можете просто положить
System.out.println("Your answer is: " + (fNum * sNUM));
//Assuming it is multiplication
Итак, где все эти знаки% и 8.3f входят? любая помощь была бы оценена, спасибо
Вы могли понять это? Если ответы были полезными, пожалуйста, поддержите их или примите их. – deezy