无法完全运行if语句

时间:2014-07-31 10:07:24

标签: java

当我使用if语句时,我的java程序有问题。事实上,在我写作的时候,这些节目并不能很好地完成。说实话我真的很困惑,因为我不知道问题出在哪里......

例如,我已经编写了这些代码,但是当我运行这些代码时,我无法获取城市代码:

import java.util.Scanner;
public class Code {

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

        System.out.println("Give the name, Get the code!\n* * * * * * * * * * * * * * *\n");
        System.out.print("Please enter the name of the country : ");
        String countryInput = sc.next().toLowerCase();
        System.out.print("Please enter the name of the city : ");
        String cityInput = sc.next().toLowerCase(); 
        System.out.println("\n-----------------------------");

        switch(countryInput) {
            case "iran" :
                System.out.print("+98 ");
                break;
            case "us" :
                System.out.print("+1 ");
                break; 
                default: System.out.print("ERROR: the entered country doesn't exist in program database!"); 
        }

        if(countryInput=="iran") {
            switch(cityInput) {
                case "tabriz" :
                    System.out.print("411");
                    break;
                case "tehran" :
                    System.out.print("21");
                    break;
                case "mashhad" :
                    System.out.print("511");
                    break;
                    default : System.out.print("\nERROR: the entered city doesn't exist in program database!"); 
            }
        }

        if(countryInput=="us") {
            System.out.print("(Sorry, the US cities aren't avalable!)");
        }
        System.out.print("\n-----------------------------\nProgrammer: user3891619");

    }
}

还有这一个。该程序从未说过#34;密码错误"即使有必要:

  import java.util.Scanner;
public class Pass {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int pass = 1234 ;
        System.out.print("Please enter your password: ");
        if(sc.hasNextInt() && sc.nextInt() == pass) {
            System.out.println("The password was successfully entered."); 
        }
        else { 
            if(sc.hasNextInt()) {
                System.out.println("The password is wrong.");
            }
            else {
                System.out.println("Password format is incorrect.");
            }
        }
    }
}

3 个答案:

答案 0 :(得分:1)

您的帖子有2个问题。

对于第二个,这很容易。当你调用sc.hasNextInt()时,它会验证下一节中是否有整数。调用sc.nextInt()将读取此整数并将读取光标放在其后面。如果再次调用hasNextInt(),它将验证是否存在第二个整数值,在您的情况下不是这种情况。

你的if结构应该是:

if (sc.hasNextInt())
{
    if (sc.nextInt()==pass)
        System.out.println("The password was successfully entered."); 
    else
        System.out.println("The password is wrong.");
} else {
    System.out.println("Password format is incorrect.");
}

答案 1 :(得分:0)

尝试:

while (sc.hasNextInt(){

 if(sc.nextInt().equals(pass)) {
        System.out.println("The password was successfully entered."); }
    else{ 
        if(sc.hasNextInt()){System.out.println("The password is wrong.");}
        else{System.out.println("Password format is incorrect.");}
    }}

你必须使用 .equals 代替 ==

答案 2 :(得分:0)

是的Ben Green是对的。比较字符串时请使用.equals。

countryInput.equals("something")

请阅读此内容以找到答案。

What is the difference between == vs equals() in Java?