2017-01-25 46 views
1

我想要讓程序執行: 如果輸入的數據不被接受,請再次請求信息 (注意:可以再次請求所有信息,這是沒有必要的只要求重新輸入特定的信息,但如果你願意,你可以)。用於循環和封裝

對於程序來說,除了當它的分辨率時,它似乎運行良好,它要求另一個輸入,但如果輸入不正確,它只是接受。我需要它繼續運行,直到輸入正確的輸入。

import java.util.Scanner; 
public class encap 
{ 
    private static String userID; 
    private static String password; 
    private static String resolution; 
    private static int Ramsize; 
    private static int freespace; 
    private static int videocard; 

    //Get and Set methods 

    public static String getuserID() 
    { 
     Scanner input = new Scanner(System.in); 
     System.out.print("Please enter userID : "); 
     userID = input.next(); 
     return userID; 
    } 
    public static String getpassword() 
    { 
     Scanner input = new Scanner(System.in); 
     System.out.print("Please enter password : "); 
     password = input.next(); 
     return password; 
    } 
    public static String getresolution() 
    { 
     Scanner input = new Scanner(System.in); 
     System.out.print("Please enter video resolution: "); 
     resolution = input.next(); 
     if (resolution.equals("800x600") || resolution.equals("1024x768") || resolution.equals("1152x900")); 
     else 
     { 
      while(true) 
      { 
       System.out.println("Information invalid, Please fill again"); 
       String getresolution = input.next(); 
       if (resolution.equals("800x600") || resolution.equals("1024x768") || resolution.equals("1152x900")); 
       break; 
      } 
     } 
     return resolution; 
    } 

    public static int getRamsize() 
    { 
     Scanner input = new Scanner(System.in); 
     System.out.print("Please enter RAM size : "); 
     while(true) 
     { 
      if(input.hasNextInt()) 
      { 
       Ramsize = input.nextInt(); 
       break; 
      } 
      else 
      { 
      input.nextLine(); 
      System.out.println("Invalid Input! Integer required"); 
      System.out.print("Please enter RAM size : "); 
      } 
     } 
     return Ramsize; 
    } 
    public static int getfreespace() 
    { 
     Scanner input = new Scanner(System.in); 
     System.out.print("Please enter HD free space : "); 
     while(true) 
     { 
      if(input.hasNextInt()) 
      { 
       freespace = input.nextInt(); 
       break; 
      } 
      else 
      { 
       input.nextLine(); 
       System.out.println("Invalid Input! Integer required"); 
       System.out.print("Please enter HD free space : "); 

      } 
     } 
     return freespace; 
    } 

    public static int getvideocard() 
    { 
     Scanner input = new Scanner(System.in); 
     System.out.print("Please enter video card RAM size: "); 
     while(true) 
     { 
      if(input.hasNextInt()) 
      { 
       videocard = input.nextInt(); 
       break; 
      } 
      else 
      { 
       input.nextLine(); 
       System.out.println("Invalid Input! Integer required"); 
       System.out.print("Please enter video card RAM size: "); 
      } 
     } 
     return videocard; 
    } 

    public static void setuserID(String newuserID) 
    { 
     userID = newuserID; 
    } 
    public static void setpassword(String newpassword) 
    { 
     password = newpassword; 
    } 
    public static void setresolution(String newresolution) 
    { 
     resolution = newresolution; 
    } 

    public static void setRamsize (int newRamsize) 
    { 
     Ramsize = newRamsize; 
    } 

    public static void setfreespace (int newfreespace) 
    { 
     freespace = newfreespace; 
    } 

    public static void setvideocard (int newvideocard) 
    { 
     videocard = newvideocard; 
    } 
    public static void main(String[] args) 
    { 
    setuserID(getuserID()); 
    setpassword(getpassword()); 
    setresolution(getresolution()); 
    setRamsize(getRamsize()); 
    setfreespace(getfreespace()); 
    setvideocard(getvideocard()); 
    System.out.println("You have input the following information: " + "\nuserID: " + userID 
      + "\npassword: " + password + "\nVideo resolution: " + resolution + "\nRam Size: " 
      + Ramsize + "\nHD Free Space: " + freespace + "\nVideo Card Ram Size: " + videocard); 
    } 
} 
+1

爲什麼你的內部getresolution之後直接使用分號呢?如果輸入有效,你永遠不會做任何事情 –

+0

現在循環只是繼續運行,並要求輸入,即使我輸入800x600它不會中斷 – tubvajlis

+0

更不用說'if'條件中的分號問題,但是有' while(true)'在'else'的條件下會導致無限循環;) –

回答

0

的問題是,因爲你永遠不上您的有效的情況下內做任何事情,你正在使用的.next()的單個字符,而不是.nextLine(),它抓住了整個輸入進入行字符的結束後(返回字符)

這將詢問,直到輸入的輸入滿足您的條件。

public static String getresolution() 
{ 
    String resolution; 
    boolean validAnswer = false; 
    Scanner input = new Scanner(System.in); 
    HashSet<String> validResolutions = new HashSet<>(); 
    validResolutions.add("800x600"); 
    validResolutions.add("1024x768"); 
    validResolutions.add("1152x900"); 
    //add more resolutions if you want without having to create a bigger if check 
    //validResolutions.add("1400x1120"); 

    do { 
     System.out.print("Please enter video resolution: "); 
     resolution = input.nextLine().replaceAll(" ", "").replaceAll("\n", ""); 
     validAnswer = validResolutions.contains(resolution) ? true : false; 
     if(!validAnswer) 
      System.out.println("Incorrect resolution please try again"); 
    } while (!validAnswer); 

    return resolution; 
} 
+0

謝謝,那有效。 – tubvajlis

+0

這很好,如果它能解決問題,請隨時標記爲答案:)祝好運與您的其餘任務。 –