初学者 Java 学生
Begining Java student
我正在编写一个程序,我必须使用方法来计算我的字符串,.upper 和 lower 使用 switch 语句。我的代码没有显示任何错误,谁能帮忙。
import java.util.*;
public class Strings
{
public static void main(String[] args)
{
String selection;
Scanner keyboard = new Scanner(System.in);
System.out.println("*********** EXAM 3 ENTER A STRING *************");
System.out.println("Enter 1 to display the number of words in the string");
System.out.println("Enter 2 to display the string in all capital letters");
System.out.println("Enter 3 to display the string in all lower case letters");
System.out.println("Enter 4 to display the string in reverse order");
System.out.println("Enter -1 to exit");
selection = keyboard.nextLine();
switch(selection.charAt(0))
{
case 1:
numberOfWords(selection);
break;
case 2:
allCapitals(selection);
break;
case 3:
allLowers(selection);
break;
case 4:
reverseOrder(selection);
break;
}//ends switch
}
/*public static void menuMethod(String [] args)
{
Scanner input = new Scanner(System.in);
System.out.println("*********** EXAM 3 ENTER A STRING *************");
System.out.println("Enter 1 to display the number of words in the string");
System.out.println("Enter 2 to display the string in all capital letters");
System.out.println("Enter 3 to display the string in all lower case letters");
System.out.println("Enter 4 to display the string in reverse order");
System.out.println("Enter -1 to exit");
}
*/
public static void numberOfWords(String selection)
{
String input; // To hold input
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter a string: ");
input = keyboard.nextLine();
// Display the number of words.
System.out.println("That string has " + wordCount(input) + " words in it.");
}
public static int wordCount(String str)
{
StringTokenizer strTok = new StringTokenizer(str);
return strTok.countTokens();
}
public static void allCapitals (String str)
{
String input;
String capInput;
Scanner keyboard = new Scanner(System.in);
System.out.println("Please enter a string. ");
input = keyboard.nextLine();//You must use nextLine here next will not work.
capInput = input.toUpperCase();
System.out.println("Your capital case string = \n" + capInput);
}
public static void allLowers (String str)
{
String input;
String lowerInput;
Scanner keyboard = new Scanner(System.in);
System.out.println("Please enter a string. ");
input = keyboard.nextLine();//You must use nextLine here next will not work.
lowerInput = input.toUpperCase();
System.out.println("Your lower case string = \n" + lowerInput);
}
public static void reverseOrder (String str)
{
String input; // To hold input
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter something: ");
input = keyboard.nextLine();
// Display it backwards.
backward(input);
}
public static void backward(String str)
{
for (int i = str.length() - 1; i >= 0; i--)
System.out.print(str.charAt(i));
System.out.println();
}
}
你还没说程序输出有什么问题。尽可能详尽地解释您的问题将帮助您快速得到一个好的答案。
至于可能出现的问题:
您的 allLowers()
方法正在使用 toUpperCase()
。作为一个好的设计实践,尽量少复制粘贴代码,因为 copy/pasting 代码暗示可能有一个函数做同样的事情。
您实际上从未检查过 -1
的输入。您在当前方法中要做的所有事情(如果您要检查退出条件)是检查 -
符号,因为它是第一个字符,而不是整个 -1
字符串,这很容易如果扩展程序会导致错误。
将您的切换条件替换为
switch(Integer.parseInt(selection))
{
//your code
}
而不是
switch(selection.charAt(0))
{
//your code
}
如果您使用 charAt(0)
,您的检查条件必须是那样,因为该方法 returns 一个字符值
case '1':
numberOfWords(selection);
break;
case '2':
allCapitals(selection);
break;
case '3':
allLowers(selection);
break;
case '4':
reverseOrder(selection);
break;
请使用此代码进行切换
switch(selection.charAt(0))
{
case '1':
numberOfWords(selection);
break;
case '2':
allCapitals(selection);
break;
case '3':
allLowers(selection);
break;
case '4':
reverseOrder(selection);
break;
}//ends switch
我正在编写一个程序,我必须使用方法来计算我的字符串,.upper 和 lower 使用 switch 语句。我的代码没有显示任何错误,谁能帮忙。
import java.util.*;
public class Strings
{
public static void main(String[] args)
{
String selection;
Scanner keyboard = new Scanner(System.in);
System.out.println("*********** EXAM 3 ENTER A STRING *************");
System.out.println("Enter 1 to display the number of words in the string");
System.out.println("Enter 2 to display the string in all capital letters");
System.out.println("Enter 3 to display the string in all lower case letters");
System.out.println("Enter 4 to display the string in reverse order");
System.out.println("Enter -1 to exit");
selection = keyboard.nextLine();
switch(selection.charAt(0))
{
case 1:
numberOfWords(selection);
break;
case 2:
allCapitals(selection);
break;
case 3:
allLowers(selection);
break;
case 4:
reverseOrder(selection);
break;
}//ends switch
}
/*public static void menuMethod(String [] args)
{
Scanner input = new Scanner(System.in);
System.out.println("*********** EXAM 3 ENTER A STRING *************");
System.out.println("Enter 1 to display the number of words in the string");
System.out.println("Enter 2 to display the string in all capital letters");
System.out.println("Enter 3 to display the string in all lower case letters");
System.out.println("Enter 4 to display the string in reverse order");
System.out.println("Enter -1 to exit");
}
*/
public static void numberOfWords(String selection)
{
String input; // To hold input
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter a string: ");
input = keyboard.nextLine();
// Display the number of words.
System.out.println("That string has " + wordCount(input) + " words in it.");
}
public static int wordCount(String str)
{
StringTokenizer strTok = new StringTokenizer(str);
return strTok.countTokens();
}
public static void allCapitals (String str)
{
String input;
String capInput;
Scanner keyboard = new Scanner(System.in);
System.out.println("Please enter a string. ");
input = keyboard.nextLine();//You must use nextLine here next will not work.
capInput = input.toUpperCase();
System.out.println("Your capital case string = \n" + capInput);
}
public static void allLowers (String str)
{
String input;
String lowerInput;
Scanner keyboard = new Scanner(System.in);
System.out.println("Please enter a string. ");
input = keyboard.nextLine();//You must use nextLine here next will not work.
lowerInput = input.toUpperCase();
System.out.println("Your lower case string = \n" + lowerInput);
}
public static void reverseOrder (String str)
{
String input; // To hold input
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter something: ");
input = keyboard.nextLine();
// Display it backwards.
backward(input);
}
public static void backward(String str)
{
for (int i = str.length() - 1; i >= 0; i--)
System.out.print(str.charAt(i));
System.out.println();
}
}
你还没说程序输出有什么问题。尽可能详尽地解释您的问题将帮助您快速得到一个好的答案。
至于可能出现的问题:
您的
allLowers()
方法正在使用toUpperCase()
。作为一个好的设计实践,尽量少复制粘贴代码,因为 copy/pasting 代码暗示可能有一个函数做同样的事情。您实际上从未检查过
-1
的输入。您在当前方法中要做的所有事情(如果您要检查退出条件)是检查-
符号,因为它是第一个字符,而不是整个-1
字符串,这很容易如果扩展程序会导致错误。
将您的切换条件替换为
switch(Integer.parseInt(selection))
{
//your code
}
而不是
switch(selection.charAt(0))
{
//your code
}
如果您使用 charAt(0)
,您的检查条件必须是那样,因为该方法 returns 一个字符值
case '1':
numberOfWords(selection);
break;
case '2':
allCapitals(selection);
break;
case '3':
allLowers(selection);
break;
case '4':
reverseOrder(selection);
break;
请使用此代码进行切换
switch(selection.charAt(0))
{
case '1':
numberOfWords(selection);
break;
case '2':
allCapitals(selection);
break;
case '3':
allLowers(selection);
break;
case '4':
reverseOrder(selection);
break;
}//ends switch