2014-10-25 2 views
-4

for 루프를 만드는 방법을 알아낼 수 없으므로 추가 (1)를 수행 한 후에 다시 메뉴로 돌아갑니다. 나는 이것을 위해서 for 루프를 사용할 필요가있다.main에서 FOR 루프를 사용하는 방법

공간을 절약하기 위해 나머지 코드는 잘라 냈습니다. 그래서 나는 단지 추가에 도움이 필요하고 나머지는 할 수 있습니다. 는 (이 사이트를 사용하지 않는다, 그래서 제대로 코드를 게시하는 방법을 잘 모릅니다.)

public static void main (String args[]) 
{ 
    Scanner userInput = new Scanner(System.in); 


    int add, add2, sub, sub2, multi, multi2, divi, divi2, rema, rema2, power, power2, Conf; 
    System.out.println("(1) Addition~"); 
    System.out.println("(2) Subtraction~"); 
    System.out.println("(3) Multiply~"); 
    System.out.println("(4) Divide~"); 
    System.out.println("(5) Remainder~"); 
    System.out.println("(6) Power~"); 
    System.out.println("(7) Quit~"); 
    Conf = userInput.nextInt(); 

    if(Conf == 1) 
    { 

     System.out.print("Enter first number to add: "); 
     add = userInput.nextInt(); 
     System.out.print("Enter second number to add: "); 
     add2 = userInput.nextInt(); 
     Ansr(); 
     Adder(add, add2); 

     Sp(); 
     } 


    else if(Conf == 7); 
    System.out.println("Exiting Program..."); 
     System.exit(0); 
     userInput.close(); 
    } 
} 
+1

솔직히 매우 당신이 실제로 원하는 것을 혼동하고있다. 뭐가 문제 야? 코드에서 무엇을하고 싶습니까? 뭐하는거야? –

+0

while while 루프가 더 적절하다고 생각합니다. 'while (Conf! = 7) '과 같은 것. 'Conf = 0'을 초기화하고 변수 선언과 초기화 후에 모든 것을이 루프 안에 넣습니다. –

+0

그리고'switch'는 여러개의'if-else' 구조체를 읽기가 쉬울 것입니다. –

답변

1

귀하의 질문은 당신이 또한에 대한 루프를 필요 왜 같은 좀 더 명확하게 필요합니다.

while 루프는 구문 적으로 더 나은 선택입니다.

이 시도 :

public static void main (String args[]) 
{ 
    Scanner userInput = new Scanner(System.in); 

    int add, add2, sub, sub2, multi, multi2, divi, divi2, rema, rema2, power, power2, Conf; 

    Conf = 0; 

    while(Conf != 7) 
    { 
     System.out.println("(1) Addition~"); 
     System.out.println("(2) Subtraction~"); 
     System.out.println("(3) Multiply~"); 
     System.out.println("(4) Divide~"); 
     System.out.println("(5) Remainder~"); 
     System.out.println("(6) Power~"); 
     System.out.println("(7) Quit~"); 

     Conf = userInput.nextInt(); 

     if(Conf == 1) 
     { 

      System.out.print("Enter first number to add: "); 
      add = userInput.nextInt(); 
      System.out.print("Enter second number to add: "); 
      add2 = userInput.nextInt(); 
      Ansr(); 
      Adder(add, add2); 

      ............ 
     } 
     else if() 
     { 
      // add additional else if for other operations 
     } 
    } 

    userInput.close(); 
} 
+0

** 첫 번째 **'Conf = userInput.nextInt();'는 첫 번째 숫자를 먹을 것입니다. 'Conf = 0'은 훨씬 깨끗합니다. –

+0

하단의'Conf = userInput.nextInt();를 제거하십시오. –

+0

@ PM77-1 죄송합니다! 그것을 지적 해 주셔서 감사합니다 – Chiseled

관련 문제