我正在尝试将string_1与string_2进行比较,以确定它是否彼此相等,并且我试图使单个字符串完全大写或不足。
import java.util.Scanner;
public class StringMethods
{
public static void main (String [] args)
{
Scanner s=new Scanner (System.in);
String string_1= s.next();
String string_2= s.next();
System.out.println ("a) Determine the length of string_1: " +string_1.length()+ "/t b) Determine the length of string_2: " +string_2.length()+"/tc) Concatenate both strings: " +string_1.concat(string_2)+"/td) Check if the two strings have same set of characters with regard to case: ");
if (string_1.equaltoIgnoreCase(string_2))
{
System.out.print ("equal.");
}
if ((string_1.comparetoIgnoreCase(string_2)>0)||(string_1.comparetoIgnoreCase(string_2)<0))
{
System.out.print ("They are not equal.");
}
System.out.println ("e) Convert string_1 to upper case: " +string_1.toUpperCase()+"/tf) Convert string_2 to lower case: " +string_2.toUnderCase()+"/tg) Extract a valid sub-string of multiple characters from string_1: " +string_1.substring(0,string_1.length));
}}
&#13;
答案 0 :(得分:0)
您的代码中存在大量拼写错误和标点符号错误。我强烈建议使用像IntelliJ这样的IDE来防止这种情况发生。我继续修复了错字,但看起来你需要做一些工作,直到程序当前运行的方式。例如,程序在用户甚至知道要输入什么之前要求输入。
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
String string_1 = s.next();
String string_2 = s.next();
System.out.println("a) Determine the length of string_1: " +
string_1.length() + "/t b) Determine the length of " +
"string_2: " + string_2.length() + "/tc) Concatenate both
strings: " + string_1.concat(string_2) + "/td) " +
"Check if the two strings have same set of characters
with regard to case: ");
if (string_1.equalsIgnoreCase(string_2)) {
System.out.print("equal.");`enter code here`
}
if ((string_1.compareToIgnoreCase(string_2) > 0) ||
(string_1.compareToIgnoreCase(string_2) < 0)) {
System.out.print("They are not equal.");
}
System.out.println("e) Convert string_1 to upper case: " +
string_1.toUpperCase() + "/tf) Convert string_2 to " +
"lower case: " + string_2.toLowerCase() + "/tg) Extract a
valid sub-string of multiple characters from string_1: " +
string_1.substring(0, string_1.length()));
}
}