打印字符串在循环中出现的次数?

时间:2013-12-06 11:06:14

标签: java string loops

我是Java新手......

输出:

Input Number= 0919-FOREKEY

Your telecommunication company is SMART.
Your phone number is 0919-2255633.
Continue Y/N: Y

Input Number=0922-KORELEY
Your telecommunication company is SUN
Your phone number is 0922-2255633.

Continue Y/N: N

--------------------------------------------------------
Thank you for using the program !

SMART : 1
GLOBE : 2
SUN CELL: 0

这是我的代码

public static void main(String[] args)
{
    boolean deci=false;

    PhoneKeyPad pad=new PhoneKeyPad();

    do
    {

        String telco=pad.getTelcoProvider("Enter 09XX-NUMBERS: ");
        boolean condition=false;

        if (telco.length()>12)
        {
            System.out.println("Input must be 12 characters only");
        }

        String telcom=telco.substring(0,4);
        String smart1="0919";
        String smart2="0920";
        String globe1="0927";
        String globe2="0917";
        String sun1="0922";
        String sun2="0933";

        if ((smart1.equals(telcom)) || (smart2.equals(telcom)))
        {
            System.out.println("Your telcom company is Smart");
        }
        if ((globe1.equals(telcom)) || (globe2.equals(telcom)) )
        {
            System.out.println("Your telcom company is Globe");
        }
        else if((sun1.equals(telcom)) || (sun2.endsWith(telcom)) )
        {
            System.out.println("Your telcom company is Sun");
        }

        String first=telco.substring(5,6);
        String firstOutput=pad.convertMobile(first);
        System.out.print(" "+first);

        String second=telco.substring(6,7);
        String secondOutput=pad.convertMobile(second);
        System.out.print(" "+second);

        String third=telco.substring(7,8);
        String thirdOutput=pad.convertMobile(third);
        System.out.print(" "+third);

        String fourth=telco.substring(8,9);
        String fourthOutput=pad.convertMobile(fourth);
        System.out.print(" "+fourth);

        String fifth=telco.substring(9,10);
        String fifthOutput=pad.convertMobile(fifth);
        System.out.print(" "+fifth);

        String sixth=telco.substring(10,11);
        String sixthOutput=pad.convertMobile(sixth);
        System.out.print(" "+sixth);

        String seventh=telco.substring(11,12);
        String seventhOutput=pad.convertMobile(seventh);
        System.out.print(" "+seventh);

        System.out.println("\nYour number is "+telcom+"-"+firstOutput
                     +secondOutput+thirdOutput+fourthOutput+fifthOutput+sixthOutput+
                     seventhOutput);
        System.out.println("Do you want to continue Y/N");
        String decisionFinal=pad.decisionFinal("");
        if ((decisionFinal.equals("N")))
        {
            deci=false;
            break;  
        }

        deci=true;

    }

    while (deci=true);
        System.out.println("Smart: "+smart);

        System.out.println("Thank you for using the program");

}

1 个答案:

答案 0 :(得分:0)

您应该使用boolean deci初始化true。这样你就不需要在循环结束时把它变为真。在底部的if语句中,它负责使用deci = false退出循环。你甚至不需要break。由于while条件,循环将自动退出。你的while条件错了。 =应该是双===是赋值运算符,而==是比较运算符。更好的是,您可以使用while(deci),转换为while(deci == true)

试试这个

boolean deci = true;

do {
    ...
    if ((decisionFinal.equals("N")))
    {
        deci = false;  
    } 

    // delete deci=true; 

} while (deci);