返回导致程序要求输入两次

时间:2015-05-17 03:28:19

标签: java eclipse input

我已经编写了以下程序,除了它要求输入两次这样的事情之外,它做了它应该做的事情。我很肯定它要求输入两次的原因是因为 return ,因为当我在 void 方法中尝试相同的代码时,它只要求输入一次

import java.util.Scanner;

public class Foothill
{
   public static void main(String[] args)
   {
      String theString, maskString, removeString;
      char keyCharacter;
      int keyCount;
      input = new Scanner(System.in);

      getKeyCharacter();
      keyCharacter = getKeyCharacter();
      getString();
      theString = getString();
      maskCharacter(theString , keyCharacter);
      maskString = maskCharacter(theString , keyCharacter);
      removeCharacter(theString , keyCharacter);
      removeString = removeCharacter(theString , keyCharacter);
      countKey(theString , keyCharacter);
      keyCount = countKey(theString , keyCharacter);
      input.close();

      System.out.println("String with '" + keyCharacter + "' masked");
      System.out.println("" + maskString + SEPERATOR);
      System.out.println("String with '" + keyCharacter + "' removed");
      System.out.println("" + removeString + SEPERATOR);
      System.out.println("# of " + keyCharacter + ": " + keyCount);               
   }

   public static char getKeyCharacter()
   {
      String userInput;
      char keyCharacter;
      int characterLength;

      do
      {
         System.out.println("Please enter a SINGLE letter to act as key: ");

         userInput = input.next();           
         characterLength = userInput.length();         

      }
      while (characterLength != 1); 

      keyCharacter = userInput.charAt(0);     
      return keyCharacter;      
   }

   public static String getString()
   {      
      String theString;
      int stringLength;

      do
      {
         System.out.println("Please enter a phrase or sentence >= 4 "
            + "characters: ");

         theString = input.next();
         stringLength = theString.length();

      }
      while (stringLength < MIN_CHAR || stringLength > MAX_CHAR);

      return theString;    
   }
}

1 个答案:

答案 0 :(得分:0)

替换:

import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.TextView;

public class CustomText extends TextView {

public CustomText(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    init(attrs);
}

public CustomText(Context context, AttributeSet attrs) {
    super(context, attrs);
    init(attrs);

}

public CustomText(Context context) {
    super(context);
    init(null);
}

private void init(AttributeSet attrs) {
    if (attrs!=null) {

        {
             Typeface myTypeface = Typeface.createFromAsset(getContext().getAssets(),"fonts/custom_font.ttf");
             setTypeface(myTypeface);
         }

    }
}

} 

使用:

getKeyCharacter();
keyCharacter = getKeyCharacter();
getString();
theString = getString();