나는 가르치는 목적을위한 간단한 프로그램을 썼고, 당신이 선택한 계산에 대한 이름과 해답을 인쇄하는 부분을 제외한 모든 것은 작동합니다. if 문은 앞으로 진행하기 전에 한 걸음 뒤로 물러나는 것처럼 두 번 실행됩니다.두번 인쇄하기
"계속 하시겠습니까?"라는 메시지가 인쇄되지만 Y/N을 입력하라는 메시지 대신 AGAIN 계산 결과를 출력하고 나머지는 계속 진행할 것인지 묻습니다 두 번째로 실제로 입력을 기다립니다.
미리 감사드립니다.
import java.util.Scanner;
public class Numbers
{
public static void main(String[] args)
{
Scanner reader = new Scanner(System.in);
boolean cont;
String name, yorn;
double num1, num2, answer;
int choice, iterator = 0;
System.out.print("What is your name? ");
name = reader.nextLine();
System.out.print("Please enter a number: ");
num1 = reader.nextDouble();
if(num1%2 != 0)
{
System.out.println(name + ", the number uou entered, " + num1 + ", is odd");
}
else
{
System.out.println(name + ", the number uou entered, " + num1 + ", is even");
}
System.out.print("Please enter a second number: ");
num2 = reader.nextDouble();
if(num2%2 != 0)
{
System.out.println(name + ", the second number you entered, " + num2 + ", is odd");
}
else
{
System.out.println(name + ", the second number you entered, " + num2 + ", is even");
}
System.out.println("1. Add");
System.out.println("2. Subtract");
System.out.println("3. Multiply");
System.out.println("4. Divide");
System.out.print("Please enter the number for the operation you would like to perform on your numbers: ");
choice = reader.nextInt();
cont = true;
while(cont)
{
while(choice != 1 && choice != 2 && choice != 3 && choice != 4)
{
System.out.print("The number entered is not one of the options. Please choose one of the operations: ");
choice = reader.nextInt();
}
if(choice == 1)
{
answer = num1 + num2;
System.out.println(name +" the sum of " + num1 + " and " + num2 + " is: " + answer);
}
else if(choice == 2)
{
answer = num1 - num2;
System.out.println(name +" the difference between " + num1 + " and " + num2 + " is: " + answer);
}
else if(choice == 3)
{
answer = num1 * num2;
System.out.println(name +" the product of " + num1 + " and " + num2 + " is: " + answer);
}
else //if(choice == 4)
{
answer = num1/num2;
System.out.println(name +" the quotient of " + num1 + " and " + num2 + " is: " + answer);
}
System.out.print("Would you like to do anything else (Y/N)? ");
yorn = reader.nextLine();
if(yorn.equals("Y"))
{
System.out.println("1. Add");
System.out.println("2. Subtract");
System.out.println("3. Multiply");
System.out.println("4. Divide");
System.out.print("Please enter the number for the operation you would like to perform on your numbers: ");
choice = reader.nextInt();
}
else if (yorn.equals("N"))
{
System.out.println("Thank you for using this prgram. Have a good day");
cont = false;
}
}
}
}
을 if'이 PLZ 구체적으로 어떤' – exexzian
어떤 경우 블록 어느 루프? 어떤 텍스트 줄을 두 번 보았습니까? – clay
수정 사항을 확인하십시오. 죄송 합니다만 충분히 명확하지 못해서 죄송합니다. 귀하의 답변을 주셔서 감사합니다 – PracticeFirst