2011-05-22 64 views
0

我做了一個名爲「Skill」的對象和主程序。 技能nextLine()不能正常工作..(Java)

import java.util.*; 
class Skill 
{ 
    static Scanner reader = new Scanner(System.in); 
    public int iStar; 
    public int iColor; //0-White, 1-Red, 2-Orange, 3-Grenn, 4-DeepSkyBlue, 5-Blue. 
    public String sName; 
    public String sText; 
    public String sLink; 
    public String sRank; 
    Skill() 
    { 
     System.out.println ("What is the skill name?"); 
     this.sName = reader.nextLine(); 
     System.out.println ("Insert the icon's link please"); 
     this.sLink = reader.nextLine(); 
     System.out.println ("Which rank is recommended to this skill?"); 
     this.sRank = reader.nextLine(); 
     System.out.println ("Explanation about the skill"); 
     this.sText = reader.nextLine(); 
     System.out.println ("Text color?" + "\n" + "1-Red, 2-Orange, 3-Green, 4-DeepSkyBlue, 5-Blue."); 
     this.iColor = reader.nextInt(); 
     System.out.println ("How many stars would you like? (0-2)"); 
     this.iStar = reader.nextInt(); 
    } 
    void FixMe () 
    { 
     int iNum = 0; 
     System.out.println ("Which one of the data would you like to change?" + "\n" + "0-non of them, 1-Star, 2-Color, 3-Name, 4-Text, 5-Link, 6-Rank."); 
     do 
     { 
      switch (reader.nextInt()) 
      { 
       case 0: iNum = 0; 
           break; 
       case 1: this.iStar = reader.nextInt(); 
           iNum = 0; 
           break; 
       case 2: this.iColor = reader.nextInt(); 
           iNum = 0; 
           break; 
       case 3: this.sName = reader.nextLine(); 
           iNum = 0; 
           break; 
       case 4: this.sText = reader.nextLine(); 
           iNum = 0; 
           break; 
       case 5: this.sLink = reader.nextLine(); 
           iNum = 0; 
           break; 
       case 6: this.sRank = reader.nextLine(); 
           iNum = 0; 
           break; 
       default: System.out.println ("Error, Please insert again the number of the data (!) ."); 
            iNum = 1; 
            break; 
      } 
     } 
     while (iNum == 1); 
    } 
    String ImageIt () 
    { 
     String sPro = "[IMG]" + this.sLink + "[/IMG]"; 
     return sPro; 
    } 
    String BoldIt () 
    { 
     String sPro = "[B]Rank " + this.sRank + "[/B]"; 
     return sPro; 
    } 
    String UnderscoreIt () 
    { 
     String sPro = "[U]" + this.sName + "[/U]"; 
     return sPro; 
    } 
    String ColorIt (String sNoob) 
    { 
     String sPro = "[COLOR=\""; 
     int iNum = 0; 
     do 
     { 
      switch (this.iColor) 
      { 
       case 1: sPro+= "Red"; 
           iNum = 0; 
           break; 
       case 2: sPro+= "Orange"; 
           iNum = 0; 
           break; 
       case 3: sPro+= "Green"; 
           iNum = 0; 
           break; 
       case 4: sPro+= "DeepSkyBlue"; 
           iNum = 0; 
           break; 
       case 5: sPro+= "Blue"; 
           iNum = 0; 
           break; 
       default: System.out.println ("Error, Please insert again the number of the color (!) ."); 
            this.iColor = reader.nextInt(); 
            iNum = 1; 
            break; 
      } 
     } 
     while (iNum == 1); 
     sPro+= "\"]" + sNoob + "[/COLOR]"; 
     return sPro; 
    } 
    String WhiteColorIt (int FirstOrSecond) 
    { 
     String sPro = "[COLOR=\"White\"]"; 
     if (FirstOrSecond == 1) 
     { 
      int iNum = 0; 
      do 
      { 
       if (this.iStar >= 3 || this.iStar < 0) 
       { 
        System.out.println ("Error, Please insert again the number of the stars (!) ."); 
        this.iStar = reader.nextInt(); 
        iNum = 1; 
       } 
       else 
       { 
        iNum = 0; 
       } 
      } 
      while (iNum == 1); 
      iNum = 2 - this.iStar; 
      for (; iNum != 0; iNum--) 
       sPro+= "*"; 
      sPro+= "[/COLOR]"; 
     } 
     else 
      sPro+= "**[/COLOR]"; 
     return sPro; 
    } 
    /*הפעולה הראשית*/ 
    String ExtractMe() 
    { 
     String sPro = ""; 
     String sTemp = ""; 
     switch (this.iStar) 
     { 
      case 0: sPro+= this.WhiteColorIt (2) + " "; 
          sTemp = this.ImageIt() + " " + this.UnderscoreIt(); 
          sTemp = this.ColorIt (sTemp); 
          sPro+= sTemp; 
          break; 
      case 1: sPro+= this.WhiteColorIt (1); 
          sTemp = "* " + this.ImageIt() + " " + this.UnderscoreIt(); 
          sTemp = this.ColorIt (sTemp); 
          sPro+= sTemp; 
          break; 
      case 2: sPro+= "** " + this.ImageIt() + " " + this.UnderscoreIt(); 
          sPro = this.ColorIt (sPro); 
          break; 
      default: 
           break; 
     } 
     sPro+= " - " + this.sText + "\n"; 
     sPro+= this.WhiteColorIt (2) + " " + this.BoldIt() + "."; 
     return sPro; 
    } 
} 

主營:

import java.util.*; 
public class Main 
{ 
    static Scanner reader = new Scanner(System.in); 
    public static void main(String[] args) 
    { 
     String sPro = ""; 
     Skill Winner01 = new Skill(); 
     System.out.println ("האם אתה רוצה לתקן את אחד הנתונים?" + "\n" + "1-Yes, 2-No"); 
     if (reader.nextInt() == 1) 
       Winner01.FixMe(); 
     sPro+= Winner01.ExtractMe() + "\n"; 
     Skill Winner02 = new Skill(); 
     System.out.println ("האם אתה רוצה לתקן את אחד הנתונים?" + "\n" + "1-Yes, 2-No"); 
     if (reader.nextInt() == 1) 
       Winner02.FixMe(); 
     sPro+= Winner02.ExtractMe() + "\n"; 
     System.out.println (sPro); 
    } 
} 

我使用BlueJ的,雖然我試圖測試程序我犯了2個技能。所以我使用了兩次「Skill()」,但是第二次,reader.nextLine()不起作用。

+0

'nextLine()發生什麼事?你期望發生什麼? – McDowell 2011-05-22 10:43:57

+0

一個原因可能是Scanner在您的Skill類的兩個實例之間是靜態的和共享的。 – 2011-05-22 11:04:04

回答

2

您正在創建掃描儀類的兩個實例,均附於System.in。一個坐在你的課Main班,另一個坐在Skill班。當他們嘗試從相同的輸入流中讀取時,這兩者會發生干擾。

一個可能的解決方案是僅實例化一個掃描器並將其作爲參數傳遞給您的Skill類的構造函數。 'nextLine()`不能正常工作的方式?

+0

你能解釋一下如何改變它嗎? 我還沒有學會如何用掃描儀做更多的事情,以及如何與他合作。 – Shaked 2011-05-22 11:34:56

+0

A)擺脫''Scanner reader = new Scanner(System.in)'行''Skill'類的內容。 B)將您的構造函數從'Skill(){...}'改爲'Skill(掃描器讀取器){...}'。 C)將'new Skill()'改爲主類中的新'Skill(讀者)'。 D)爲'FixMe'做同樣的事情。 – Howard 2011-05-22 11:37:43