Java CompareTo和If语句

Java CompareTo和If语句,java,Java,我是Java初学者。现在,我尝试修复代码中的错误。据说有3个错误:没有if的else。我一直在修改我的代码,但似乎我会有更多的其他错误 我制作了一个程序,询问用户的姓氏,询问他们在点名过程中需要等待多长时间,我需要使用else if,以便只打印一条语句 import java.util.Scanner; public class CompareTo {public static void main( String[] args ) { Scanner Keyboard = new S

我是Java初学者。现在,我尝试修复代码中的错误。据说有3个错误:
没有if的else
。我一直在修改我的代码,但似乎我会有更多的其他错误

我制作了一个程序,询问用户的姓氏,询问他们在点名过程中需要等待多长时间,我需要使用else if,以便只打印一条语句

import java.util.Scanner;
public class CompareTo
{public static void main( String[] args )
    {   Scanner Keyboard = new Scanner (System.in);

        String Name, Carswell, Jones, Smith, Young;
        System.print.out( "What's your last name? ");
        Name = Keyboard.next();   
        "Name".compareTo("Carswell");

        if ( "Name" < "Carswell" )
        {System.out.print( "You dont' have to wait long, " + Name);
        }

        "Name".compareTo("Jones"); 
         else if ( "Name" > "Carswell"  && Name < "Jones" )
        {System.out.print( "that's not bad, "  + Name );
        }

        "Name".compareTo("Smith");
        else if ( "Name" > "Jones" && Name < "Smith")
        {System.out.print( "looks like a bit of a wait, "  + Name );
        }

        "Young".compareTo("Young");
         else if ( "Name" > "Smith" && Name < "Young" )
        {System.out.print( "it's gonna be a while, "  + Name );
        }

        else 
        {System.out.print( "not going anywhere for a while, "  + Name );
        }
    }
}
import java.util.Scanner;
公共类比较
{公共静态void main(字符串[]args)
{扫描仪键盘=新扫描仪(System.in);
字符串名称,Carswell,Jones,Smith,Young;
System.print.out(“你姓什么?”);
Name=键盘.next();
“名称”。与“卡斯韦尔”相比;
如果(“名称”<“Carswell”)
{System.out.print(“您不必等待太久,”+Name);
}
“名称”,与“琼斯”相比;
否则,如果(“名称”>“卡斯韦尔”&名称<“琼斯”)
{System.out.print(“不错,”+Name);
}
“姓名”,与“史密斯”相比;
如果出现其他情况(“姓名”>“琼斯”&姓名<“史密斯”)
{System.out.print(“看起来有点等待,”+Name);
}
“年轻”。与“年轻”相比;
else if(“姓名”>“史密斯”&&Name<“杨”)
{System.out.print(“需要一段时间,”+Name);
}
其他的
{System.out.print(“暂时不去任何地方,”+Name);
}
}
}
问题在于:

if ( "Name" < "Carswell" )
{System.out.print( "You dont' have to wait long, " + Name);
}

"Name".compareTo("Jones"); //This is the issue
 else if ( "Name" > "Carswell"  && Name < "Jones" )
{System.out.print( "that's not bad, "  + Name );
}
if(“Name”<“Carswell”)
{System.out.print(“您不必等待太久,”+Name);
}
“名称”,与“琼斯”相比//这就是问题所在
否则,如果(“名称”>“卡斯韦尔”&名称<“琼斯”)
{System.out.print(“不错,”+Name);
}

您有
“Name”。与(“Jones”)在if和else if之间。if和else if之间不应有任何代码。将上面的代码移到if块或如果是块以使代码编译。

您的代码有很多错误,但是正如您所说的,如果没有,则得到其他的,然后考虑这个

import java.util.Scanner;
类比较{

public static void main(String[] args){
    Scanner keyboard = new Scanner(System.in);

    String Name, Carswell, Jones, Smith, Young;

    System.out.println( "What's your last name? ");

    Name = keyboard.next();   

    if ( Name.compareTo("Carswell")<0 )
    {
        System.out.print( "You dont' have to wait long, " + Name);
    }

    /*
        if you have an else if after if; then between them
        you cannot write any other code 
        because you cant write an else if without an if 
        thats why you getting error
    */

    else if(Name.compareTo("Carswell") > 0 && Name.compareTo("Jones")<0){
        System.out.print( "that's not bad, "  + Name );
    }
}
publicstaticvoidmain(字符串[]args){
扫描仪键盘=新扫描仪(System.in);
字符串名称,Carswell,Jones,Smith,Young;
System.out.println(“你姓什么?”;
Name=键盘.next();

if(Name.compareTo(“Carswell”)0&&Name.compareTo(“Jones”)//永远不要使用java预定义方法作为类名

//变量必须以小写字母开头

public class TestClass{
public static void main( String[] args ) 
{ 
    Scanner Keyboard = new Scanner (System.in);
    String Name, Carswell, Jones, Smith, Young;
    System.out.print( "What's your last name? ");
    Name = Keyboard.next();
    if (Name.compareTo("Carswell")<0){
        System.out.print( "You dont' have to wait long, " + Name);
    }
    //You need to use || not &&
    else if (Name.compareTo("Carswell")>0  || Name.compareTo("Jones")<0 ) {
        System.out.print( "that's not bad, "  + Name );
    }
    else if (Name.compareTo("Jones")>0 || Name.compareTo("Smith")<0)
    {
        System.out.print( "looks like a bit of a wait, "  + Name );
    }

    else if ( Name.compareTo("Smith")>0 || Name.compareTo("Young")<0){
        System.out.print( "it's gonna be a while, "  + Name );
    }

    else {
        System.out.print( "not going anywhere for a while, "  + Name );
    }
}
}
公共类TestClass{
公共静态void main(字符串[]args)
{ 
扫描仪键盘=新扫描仪(System.in);
字符串名称,Carswell,Jones,Smith,Young;
System.out.print(“你姓什么?”);
Name=键盘.next();
if(Name.compareTo(“Carswell”)0 | | Name.compareTo(“Jones”)0 | | Name.compareTo(“Smith”)0 | | | Name.compareTo(“Young”)
按字母顺序包装示例;
导入java.util.Scanner;
公共类字母顺序示例{
公共静态void main(字符串[]args){
//请在下面写下你的代码。
扫描仪键盘=新扫描仪(System.in);
字符串名;
System.out.print(“您姓什么?>>”;
Name=键盘.next();
如果(名称比较(“Carswell”)<0){
System.out.print(“您不必等待”+Name);
}
如果(Name.compareTo(“Carswell”)>0&&Name.compareTo(“Jones”)<0){
System.out.print(“不错”+名称);
}
如果(Name.compareTo(“琼斯”)>0和Name.compareTo(“史密斯”)<0){
System.out.print(“看起来有点等待”+Name);
}
如果(Name.compareTo(“Smith”)>0&&Name.compareTo(“Young”)<0){
}
否则{
System.out.print(“您暂时不去任何地方”+姓名);
}
}
}

您希望这一行做什么?“Name”。compareTo(“Jones”);您正在比较两个常量字符串并放弃结果。请与我们分享您的想法并解释您的代码。
package alphabetical_order_example;

import java.util.Scanner;

public class alphabetical_order_example {

public static void main(String[] args) {
        // please write your code below. 

     Scanner keyboard = new Scanner(System.in);

     String Name;


       System.out.print("What is your last name? >>> ");
       Name = keyboard.next();


    if (Name.compareTo("Carswell ") < 0 ) {
        System.out.print("You don't have to wait " + Name);
    }
    else if (Name.compareTo("Carswell") > 0  && Name.compareTo("Jones") < 0) {
        System.out.print("That's not Bad " + Name);
    }
    else if (Name.compareTo("Jones") > 0 && Name.compareTo("Smith") < 0) {
        System.out.print("Looks like a bit wait " + Name);

        }
    else if (Name.compareTo("Smith") > 0 && Name.compareTo("Young") < 0 ) {

       }
    else {
        System.out.print("Your not going anywhere for a while " + Name);
       }

    }
}