我正在尝试制作一个程序,让用户输入未知的名称值,然后输出输入的最长名称。到目前为止,这是我的代码。当我编译时,我有几个错误,它们都是相同的“找不到符号”。如果需要,我是否需要在哪里初始化这些变量?
import java.util.Scanner;
public class Name
{
public static void main(String[] args)
{
Scanner kb = new Scanner(System.in);
longestName(kb);
}
public static void longestName(Scanner sc)
{
String name=kb.nextLine();
biggestName=name;
System.out.println("Type -1 if you want to quit");
int number=kb.nextInt();
While (number !=-1);
{
String name1=kb.nextLine();
if (name1.length() > biggestName)
{
biggestName=name1;
}
System.out.println("Do you want to continue? Type -1 to quit.");
int number1=kb.nextInt();
}
System.out.println("Longest name is "+biggestName);
}
}
感谢帮助人员修复了错误,以及其他一些更改,程序给出了正确的输出。
import java.util.Scanner;
public class Name
{
public static void main(String[] args)
{
Scanner kb = new Scanner(System.in);
longestName(kb);
}
public static void longestName(Scanner kb)
{
String biggestName;
System.out.println("Enter the first name");
String name=kb.nextLine();
biggestName=name;
System.out.println("Type -1 if you want to quit");
int number=kb.nextInt();
while (number !=-1)
{
System.out.println("Enter another name");
Scanner kb1 = new Scanner(System.in);
String name1=kb1.nextLine();
int length1=biggestName.length();
int length2=name1.length();
if (length2 > length1)
{
biggestName=name1;
}
System.out.println("Do you want to continue? Type -1 to quit.");
number=kb.nextInt();
}
System.out.println("Longest name is "+biggestName);
}
}