Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从细绳中挑出_Java_If Statement_Switch Statement - Fatal编程技术网

Java 从细绳中挑出

Java 从细绳中挑出,java,if-statement,switch-statement,Java,If Statement,Switch Statement,我如何编写一个程序,如果用户输入一个网站名称,程序将如何确定它是否是一个com、gov、org、edu实体?这是我读过的一章中的一个问题。本章介绍If/else语句和switch语句。所以我相信它必须围绕这些。但这些例子中的每一个都使用INT。我想我会利用字符串来解决这个问题,但这本书在这方面并没有真正帮助我 这是我现在的代码/伪代码 package ch5_inClass; import java.util.Scanner; public class FindingWebAddressEn

我如何编写一个程序,如果用户输入一个网站名称,程序将如何确定它是否是一个com、gov、org、edu实体?这是我读过的一章中的一个问题。本章介绍If/else语句和switch语句。所以我相信它必须围绕这些。但这些例子中的每一个都使用INT。我想我会利用字符串来解决这个问题,但这本书在这方面并没有真正帮助我

这是我现在的代码/伪代码

package ch5_inClass;

import java.util.Scanner;

public class FindingWebAddressEntity
{

    public static void main(String[] args)
    {
        // TODO Auto-generated method stub

        //enter web site
        String webAddress;

        System.out.println("Please enter web site name: ");
        Scanner scan = new Scanner(System.in);
        webAddress = scan.next();

        //determine last three characters of the web address
        if(webAddress)
        {
            System.out.println("this is a gov website");
        }
        else
        {
            System.out.println("this is not a gov website");
        }
        //display government entity if gov
        //display university entity if edu
        //display commercial entity if com
        //display organization entity if org


    }

}
您可以使用该函数。比如:

if(webAddress.endsWith(".gov") {
   System.out.println("This is a gov address");
}
else if(webAddress.endsWith(".edu") {
   System.out.println("This is an edu address");
}

您要查找的是
String
中的方法。将其与下面的
if
else
语句一起使用

String s = "oliver.com";
if(s.endsWith(".com")){
    System.out.println("its a dot come site");
} else if (s.endsWith(".gov")) {
    System.out.println("its a government site");
}
然后将打印出
这是一个点播站点

希望这有帮助