Java的新手。我的教授将此评论归还给我 “ 3)程序未正确实现新循环。循环的目的是允许用户输入四个数字并进行数学运算。然后,程序应询问用户是否要继续,是否继续,该程序应允许再输入四个数字,该过程应继续进行,直到用户明确表示不希望停止。 “
对于如何询问用户是否要继续然后允许再次输入四个数字,我感到困惑。
输入正确的错误消息时也遇到问题。
import java.util.Scanner;
public class Loops {
public static void main(String[] args) {
// 1. Declare two integer variables and two double variables
int iNumber;
int iNumber2;
double iDecimal;
double iDecimal2;
int count;
char yesNo = 'y'; // When a char variable is declared, the value needs to be a char not a string. Therefore, it needs a single quote instead of a double quote.
// 2. Instantiate a Scanner object
Scanner input = new Scanner(System.in);
// 3. Place the entry of the number into a loop that requires the user to enter the values at least one time.
//int i = 1;
// NOTE – Steps 4 through 8 are EXACTLY the same as the CE-Decision exercise. You may reuse that code if you would like.
// 4. Using print, display two lines that allows the user to enter in the two integer values on the same line as the prompt.
System.out.print("Input integer value 1: ");
iNumber = input.nextInt();
System.out.print("Input integer value 2: ");
iNumber2 = input.nextInt();
// 5. Using print, display two lines that allows the user to enter in the two double values on the same line as the prompt.
System.out.print("Input double value 1: ");
iDecimal = input.nextDouble();
System.out.print("Input double value 2: ");
iDecimal2 = input.nextDouble();
// 6. Using multiple printf statements, display the result of adding, subtracting, multiplying, dividing and moding the two integer values.
if (iNumber > iNumber2)
{
System.out.println("\nNumber 1 is greater than Number 2: ");
//+ instead of , for Printlns. , for printf. To add, use parenthesis.
System.out.println("\nInput integer value 1: "+ iNumber);
System.out.println("Input integer value 2: "+ iNumber2);
System.out.println("Input double value 1: "+ iDecimal);
System.out.println("Input double value 2: "+ iDecimal2);
System.out.println("\nInteger output: ");
System.out.printf("%s%d%s%d%s%d%n", "Adding ", iNumber, " and ", iNumber2, " = ", iNumber + iNumber2);
System.out.printf("%s%d%s%d%s%d%n", "Subtracting ", iNumber, " and ", iNumber2, " = ", iNumber - iNumber2);
System.out.printf("%s%d%s%d%s%d%n", "Multiplying ", iNumber, " and ", iNumber2, " = ", iNumber * iNumber2);
System.out.printf("%s%d%s%d%s%d%n", "Dividing ", iNumber, " and ", iNumber2, " = ", iNumber / iNumber2);
System.out.printf("%s%d%s%d%s%d%n", "Moding ", iNumber, " and ", iNumber2, " = ", iNumber % iNumber2);
System.out.println("\nDouble output: ");
System.out.printf("%s%.2f%s%.2f%s%.2f%n", "Adding ", iDecimal, " and ", iDecimal2, " = ", iDecimal + iDecimal2);
System.out.printf("%s%.2f%s%.2f%s%.2f%n", "Subtracting ", iDecimal, " and ", iDecimal2, " = ", iDecimal - iDecimal2);
System.out.printf("%s%.2f%s%.2f%s%.2f%n", "Multiplying ", iDecimal, " and ", iDecimal2, " = ", iDecimal * iDecimal2);
System.out.printf("%s%.2f%s%.2f%s%.2f%n", "Dividing ", iDecimal, " and ", iDecimal2, " = ", iDecimal / iDecimal2);
}
else if (iNumber < iNumber2)
{
System.out.println("\nNumber 2 is greater than Number 1:");
//+ instead of , for Printlns. , for printf. To add, use parenthesis.
System.out.println("\nInput integer value 1: " + iNumber);
System.out.println("Input integer value 2: " + iNumber2);
System.out.println("Input double value 1: " + iDecimal);
System.out.println("Input double value 2: " + iDecimal2);
System.out.println("\nInteger output: ");
System.out.printf("%s%d%s%d%s%d%n", "Adding ", iNumber, " and ", iNumber2, " = ", iNumber + iNumber2);
System.out.printf("%s%d%s%d%s%d%n", "Subtracting ", iNumber, " and ", iNumber2, " = ", iNumber - iNumber2);
System.out.printf("%s%d%s%d%s%d%n", "Multiplying ", iNumber, " and ", iNumber2, " = ", iNumber * iNumber2);
System.out.printf("%s%d%s%d%s%d%n", "Division ", iNumber, " and ", iNumber2, " = ", iNumber / iNumber2);
System.out.printf("%s%d%s%d%s%d%n\n", "Moding ", iNumber, " and ", iNumber2, " = ", iNumber % iNumber2);
System.out.println("Double output: ");
System.out.printf("%s%.2f%s%.2f%s%.2f%n", "Adding ", iDecimal, " and ", iDecimal2, " = ", iDecimal + iDecimal2);
System.out.printf("%s%.2f%s%.2f%s%.2f%n", "Subtracting ", iDecimal, " and ", iDecimal2, " = ", iDecimal - iDecimal2);
System.out.printf("%s%.2f%s%.2f%s%.2f%n", "Multiplying ", iDecimal, " and ", iDecimal2, " = ", iDecimal * iDecimal2);
System.out.printf("%s%.2f%s%.2f%s%.2f%n", "Dividing ", iDecimal, " and ", iDecimal2, " = ", iDecimal / iDecimal2);
}
else if (iNumber2 == iNumber)
{
System.out.println("\nBoth numbers are equal");
//+ instead of , for Printlns. , for printf. To add, use parenthesis.
System.out.println("\nInput integer value 1: "+ iNumber);
System.out.println("Input integer value 2: "+ iNumber2);
System.out.println("Input double value 1: "+ iDecimal);
System.out.println("Input double value 2: "+ iDecimal2);
System.out.println("\nInteger output: ");
System.out.printf("%s%d%s%d%s%d%n", "Adding ", iNumber, " and ", iNumber2, " = ", iNumber + iNumber2);
System.out.printf("%s%d%s%d%s%d%n", "Subtracting ", iNumber2, " and ", iNumber, " = ", iNumber2 - iNumber);
System.out.printf("%s%d%s%d%s%d%n", "Multiplying ", iNumber, " and ", iNumber2, " = ", iNumber * iNumber2);
System.out.printf("%s%d%s%d%s%d%n", "Division ", iNumber, " and ", iNumber2, " = ", iNumber / iNumber2);
System.out.printf("%s%d%s%d%s%d%n\n", "Moding ", iNumber, " and ", iNumber2, " = ", iNumber % iNumber2);
System.out.println("Double output: ");
System.out.printf("%s%.2f%s%.2f%s%.2f%n", "Adding ", iDecimal, " and ", iDecimal2, " = ", iDecimal + iDecimal2);
System.out.printf("%s%.2f%s%.2f%s%.2f%n", "Subtracting ", iDecimal2, " and ", iDecimal, " = ", iDecimal2 - iDecimal);
System.out.printf("%s%.2f%s%.2f%s%.2f%n", "Multiplying ", iDecimal, " and ", iDecimal2, " = ", iDecimal * iDecimal2);
System.out.printf("%s%.2f%s%.2f%s%.2f%n", "Dividing ", iDecimal, " and ", iDecimal2, " = ", iDecimal / iDecimal2);
}
else if (iNumber2 == 0)
{
System.out.println("\nNumber 2 equals 0:");
//+ instead of , for Printlns. , for printf. To add, use parenthesis.
System.out.println("\nInput integer value 1: "+ iNumber);
System.out.println("Input integer value 2: "+ iNumber2);
System.out.println("Input double value 1: "+ iDecimal);
System.out.println("Input double value 2: "+ iDecimal2);
System.out.println("\nInteger output: ");
System.out.printf("%s%d%s%d%s%d%n", "Adding ", iNumber, " and ", iNumber2, " = ", iNumber + iNumber2);
System.out.printf("%s%d%s%d%n", "Subtracting ", iNumber, " = ", iNumber - iNumber2);
System.out.printf("%s%d%s%d%n", "Multiplying ", iNumber, " = ", iNumber * iNumber2);
if (iNumber2 == 0 || iDecimal2 == 0){
System.out.println("Error: You cannot divide and mod by zero!!!");
}
else {
System.out.printf("%s%d%s%d%s%d%n", "Division ", iNumber, " and ", iNumber2, " = ", iNumber / iNumber2);
System.out.printf("%s%d%s%d%s%d%n\n", "Moding ", iNumber, " and ", iNumber2, " = ", iNumber % iNumber2);
}
System.out.println("Double output: ");
System.out.printf("%s%.2f%s%.2f%s%.2f%n", "Adding ", iDecimal, " and ", iDecimal2, " = ", iDecimal + iDecimal2);
System.out.printf("%s%.2f%s%.2f%n", "Subtracting ", iDecimal, " = ", iDecimal - iDecimal2);
System.out.printf("%s%.2f%s%.2f%n", "Squaring ", iDecimal, " = ", iDecimal * iDecimal2);
System.out.printf("%s%.2f%s%.2f%s%.2f%n", "Dividing ", iDecimal, " and ", iDecimal2, " = ", iDecimal / iDecimal2);
}
// a. Test the two numbers, subtract the smaller number from the larger number. Display the output with the numbers in the proper order. If the numbers are equal, display a message that subtracting a number from itself gives you zero.
// b. If the two numbers are equal, the program should display the message as “Squaring” a number instead of multiplying two numbers.
// c. If the second number is zero, the program should display an error message saying that you cannot divide or mod by zero.
// 7. Using multiple printf statements, display the result of adding, subtracting, multiplying, and dividing the two double values.
// a. Test the two numbers, subtract the smaller number from the larger number. Display the output with the numbers in the proper order. If the numbers are equal, display a message that subtracting a number from itself gives you zero.
// b. If the two numbers are equal, the program should display the message as “Squaring” a number instead of multiplying two numbers.
// c. If the second number is zero, the program should display an error message saying that you cannot divide by zero.
// 8. Make sure that there is a title before each of the two outputs and there are blank lines between the input section and the two output sections.
// 9. Ask the user if they wish to enter in another set of numbers.
count = input.nextInt();
do {
System.out.println("Do you wish to enter another set of numbers?: (y/n)");
yesNo = input.next().charAt(0);
// a. The only valid entries are : y, Y, n or N (single letters)
if (yesNo != 'Y' && yesNo != 'y' && yesNo != 'N' && yesNo != 'n') {
System.out.println("Error = you must enter Y or N. Please retry. "); // Error message should be inside the if statement block or else the error message would always appear.
}
} while (yesNo != 'Y' && yesNo != 'y' && yesNo != 'N' && yesNo != 'n');
}
}
System.out.print("Do you wish to enter another set of numbers?: (y/n)");
String answer = scan.nextLine();
// b. Use the Java construct input.next().charAt(0) to get the character value
// c. Place the input into a loop that tests the entry for validity
// i. If the entry is valid, end the loop
// ii. If the entry is invalid, display an error message and ask the user to reenter their response
// 10. If the user indicated that they wish to enter new values, continue the loop and allow the user to reenter a new set of values.
// a. The user should be allowed to reenter values as many times as they like. Only the entry of ‘n’ or ‘N’ should cause the loop to end.
// 11. If the user indicated that they do not wish to enter any more values, end the loop and display a thank you/goodbye message.
// 12. Comment your code.
// 3)程序未正确实现新循环。循环的目的是允许用户输入四个数字并进行数学运算。
//然后程序应询问用户是否要继续,如果要继续,则程序应允许再输入四个数字。
//应该继续,直到用户明确说不希望停止。
答案 0 :(得分:0)
这个问题很难理解。您应该尝试编写更整洁的问题,以便更多的人可以为您提供帮助。给出问题所在代码的一小部分,然后询问“为什么不正确?”。
关于您的问题,我认为您应该有另一个while循环,其中应包括您实现的逻辑。类似于while(yesNo == 'Y' || yesNo == 'y')
。因此,虽然这是事实,但您从用户那里读取了4个输入。在我看来,您甚至都不关心用户输入的是“ n”还是“ y”,您的do-while循环仅检查输入的有效性。