替换构造函数正在使用的String语句

时间:2018-02-03 00:56:09

标签: java string constructor switch-statement

所以我正在研究一个项目来计算所需字符串的元音和辅音数量。我几乎所有的东西都在工作,但我似乎无法选择用新的字符串替换字符串才能正常工作。我认为将新选项应用于Driver构造是一个问题,但我似乎无法在不破坏程序的情况下找到一种方法。

import java.util.*;

public class Driver{

 private String entry;

int vowels = 0;
int cons = 0;

 public Driver(String input){
  entry = input;
  this.count();
 }

 public boolean isVowel(char x){
  return (x == 'A' || x == 'E' || x == 'I' || x == 'O' || x == 'U' ||
   x == 'a' || x == 'e' || x == 'i' || x == 'o' || x == 'u');
 }

 public boolean isConsonant(char x){
  return (((x >= 'a' && x <= 'z') || (x >= 'A' && x <= 'Z')) && !isVowel(x));}

 public int getVowels(){
  return vowels;
 }

 public int getConsonants(){
  return cons;
 }

 public void count(){

  int l = entry.length();
  for(int i = 0; i < l; i++){
   if(this.isVowel(entry.charAt(i))){
    vowels++;
   }else{
    if(this.isConsonant(entry.charAt(i))){
     cons++;
    }
   }

  } 

 }
 static Scanner kb = new Scanner(System.in);

 public static void main(String[] args){

  int choice;   //Choice from menu
  int vowels = 0;  //# of vowels in entry
 int cons = 0;  //# of consonants in entry
  String entry;  //User's input

  System.out.print("Enter a string:");
  entry = kb.nextLine();
  do{
   Driver sentence = new Driver(entry);

   System.out.println("Enter a number");
   System.out.println("1. Count the number of vowels in the string");
   System.out.println("2. Count the number of consonants in the string");
   System.out.println("3. Count both the vowels and consonants in the string");
   System.out.println("4. Enter another string");
   System.out.println("5. Exit the program:");
   choice = kb.nextInt();

   switch(choice){

case 1:
 System.out.println("Vowels: " + sentence.getVowels());
 break;
case 2:
 System.out.println("Consonants: " + sentence.getConsonants());
 break;
case 3:; 
 System.out.println("Vowels: " + sentence.getVowels() + "\nConsonants: " + sentence.getConsonants());
 break;
case 4:
 System.out.print("Enter a string:");
 entry = kb.nextLine();
 //Driver sentence = new Driver(entry);
 break;
default:
 System.out.println("Please enter a value input: ");
   }

  }while(choice != 5);


 }

}

1 个答案:

答案 0 :(得分:0)

你应该拥有驱动程序类(没有 main()方法)。

然后创建另一个将成为启动类的类,它将包含 main()方法。为了论证,我们称之为 VowelsAndConsonants

引起所有悲痛的问题是控制台菜单的键盘输入为1到5。您正在使用 Scanner.nextInt()方法,只要您知道输入的内容并非全部消耗并保留在扫描仪缓冲区内,这是因为扫描仪。 nextInt()方法不会读取并使用在按Enter键时应用的 行分隔符 。因此,当 Scanner.nextLine()用作下一个扫描程序方法时,它首先要抓住的是 行分隔符 扫描仪缓冲区又类似于按Enter键,因此它会跳过键入的内容。另一方面, Scanner.nextLine()方法会读入并使用 行分隔符 。解决此问题的一种方法是在kb.nextLine();代码行之后直接添加(在您的情况下)choice = kb.nextInt();。这会强制使用 行分隔符 并清空缓冲区。从本质上讲,菜单看起来像这样:

Driver sentence = new Driver(entry);
do {
    System.out.println("Enter a Menu choice (1 to 5): ");
    System.out.println("1. Count the number of vowels in the string");
    System.out.println("2. Count the number of consonants in the string");
    System.out.println("3. Count both the vowels and consonants in the string");
    System.out.println("4. Enter another string");
    System.out.println("5. Exit the program:");
    choice = kb.nextInt();
    kb.nextLine();  // Consume

    ..... the rest of your do/while code .....
}
  

编辑:根据您在评论中的问题:“我很好奇为什么   行处理器的处理方式有所不同。是   这只是Java中的东西,或者是有原因的   是不是从nextInt()中消耗了?“

扫描仪类非常复杂。根据设计, nextInt()可以做到这一点,因为Scanner可以基于令牌工作。您可以使用 nextInt()在单个输入上接受多个数字,使用(例如)空格分隔符,然后使用 Scanner.hasNextInt()或Scanner.hasNext( )与Scanner.nextInt()结合使用的方法,您可以单独获取每个值,例如:

Scanner kb = new Scanner(System.in).useDelimiter(" *");
System.out.println("Enter numbers delimited with a white-space:  ");
while (kb.hasNextInt()) {
    int x = kb.nextInt();
    System.out.println(x);
}

如果您要运行此代码并在提示符下输入一行:

2 4 6 8 10

(注意数字之间的空格)您将在控制台窗口中看到显示:

<强> 2
4
6
8
10

通过 while 循环的每次迭代, x 将等于输入的数字行中的下一个整数标记。然而,这种东西最适合从扫描仪也可以读取的文本文件中读取数字。阅读 Scanner Class