2014-12-04 54 views
-5

int type = 0,num = 0,square,cube;如果必須轉到「else」語句,則重複「if」語句

Scanner input = new Scanner(System.in); 

    System.out.println("Press 1 to Square a Number\nPress 2 to Cube a Number "); 
    type = input.nextInt(); 

    if (type == 1) 
    { 
      do 
     { 
      System.out.println("Type a number to square "); 
      num = input.nextInt(); 
      square = num * num; 
      System.out.println(square); 
     } 
     while (num != 0); 
    } 

    else if (type == 2) 
    { 
     do 
     { 
      System.out.println("Type a number to cube "); 
      num = input.nextInt(); 
      cube = num * num * num; 
      System.out.println(cube); 
     } 
     while (num != 0); 
    } 

    else 
     System.out.println("Please type either 1 or 2"); 

     ----> here I want it to go back to the top and ask again. 
     ----> how do it do this part? 

閱讀右上方這個地方我把這些箭頭。這是我需要幫助的部分。

(不介意這一點。這件事情只是不斷地激勵我把更多的正常的話在它 所以這是我在做什麼)

} 
+0

你想要一個'while'循環 – fge 2014-12-04 23:50:11

+0

如果想ÿ你要繼續說更正常的話,這可能是有原因的。他們並不僅僅把最低限度的字數限制在那裏,所以不要讓你的帖子來避開它。 – 2014-12-04 23:53:22

+0

哇。感謝馬特,你是有幫助的...和人Aditya是對的 – 2014-12-05 01:31:57

回答

0

把整個代碼塊在一段時間環 與 while((type!=1 && type!=2)) { ... }

這將這樣的伎倆

+1

謝謝你。我把所有的東西都放在我把掃描儀變成「Do While」循環的那一行,而「while」部分就是你說的和它的工作原理。 – 2014-12-05 01:30:53

0
while ((type!=1) && (type!=2)) 
{ 
    if......