2014-10-29 41 views
-3

我需要在這問一個循環,詢問玩家是否想再次玩。如果用戶說是的重新啓動遊戲。如果他們說不結束比賽。我一起遇到麻煩。請幫忙!循環出現問題

import java.util.Scanner; 
public class HW4ChrisMuncher 
    { 
    public static void main(String[] args) 
    { 
    Scanner input = new Scanner(System.in); 

    double a; 
    int b; 

    System.out.println("Enter a number"); 
    double userNum = input.nextDouble(); 
    a = userNum; 

    Scanner input2 = new Scanner(System.in); 

    System.out.println("Enter a number (no decimals!)"); 
    int userNum2 = input2.nextInt(); 
    b = userNum2;   



    double c = min(a, b); 
    double d = max(a, b); 
    double e = abs(a); 
    double f = pow(a, b);  



    System.out.println("The minimum value of " + a + " and " + b + " is " + c);   
    System.out.println("The maximum value of " + a + " and " + b + " is " + d); 
    System.out.println("The absolute value of "+ a+ " is " + e); 
    System.out.println(+ a + " to the power of " + b + " is " + f); 

    } 

    // Returns the minimum of two numbers 
    public static double min(double n1, int n2) 
    { 
    double min; 
     if (n1 > n2) 
     min = n2; 
     else 
     min = n1; 

     return min; 
    } 

    // Return the max between two numbers 
    public static double max(double n1, int n2) 
    { 
    double max; 
     if (n1 > n2) 
     max = n1; 
     else 
     max = n2; 

     return max; 
    } 


    //Returns the absolute value of the two numbers 
    public static double abs(double n1) 
    { 
    if (n1 < 0) 
     return -n1; 
    else 
     return n1; 

    } 


    public static double pow(double n1, int n2) 
    { 
    double f = 1; 
     for (int i =0; i< n2; i++) 
      { 
      f = f * n1; 
      } 
     return f; 

    } 


} 
+1

你的問題是什麼? – Qix 2014-10-29 00:52:49

+0

我需要幫助把這個循環放在這個 – 2014-10-29 00:53:33

+0

什麼循環?您沒有提供任何描述性信息,說明您面臨的問題。你有什麼嘗試?你收到什麼錯誤?你不明白什麼? – Qix 2014-10-29 00:54:20

回答

0

很簡單!

只要抓住一個while循環,做這樣的事情:

String s = "yes"; 
while(s.equals("yes") || s.equals("y")) { 
    //whatever you want repeated 
    System.out.println("Would you like to repeat?"); 
    s = scan.nextLine(); 
} 

想想吧。如果這個人輸入「是」,那麼while循環仍然是真的,所以它會從頭開始。否則,如果該人輸入除「是」之外的任何內容,則while循環條件將爲假,並且循環將被終止。

我希望有幫助。祝你好運:)