Home > Back-end >  While why to jump out of the loop
While why to jump out of the loop

Time:10-28

Package asd.

import java.util.Scanner;

Public class pay {

Public static void main (String [] args) {
Scanner kk=new Scanner (System. In);
String a="y";
While (a! {
="n")System. The out. Println (" * * * * * * * * * * * * * * * * * * more credit * * * * * * * * * * * * * * * * * * ");
System. The out. Println (" please enter the phone number: ");
String number=kk. Next ();
System. The out. Println (" 1.10 yuan \ \ t3.30 t2.20 yuan yuan \ \ t5.100 n4.50 yuan yuan \ t6 other amount ");
System. The out. Println (" serial number: please select a payment amount ");
Int. No=kk nextInt ();
The switch (no) {
Case 1: System. Out.println (" mobile phone number: "+ number +", 10 yuan more credit!" ); break;
Case 2: System. Out.println (" mobile phone number: "+ number +", more credit 20 yuan!" ); break;
Case 3: System. Out.println (" mobile phone number: "+ number +", more credit 30 yuan!" ); break;
Case 4: System. Out.println (" mobile phone number: "+ number +", more credit $50!" ); break;
Case 5: System. Out.println (" mobile phone number: "+ number +", 100 yuan more credit!" ); break;
Case 6: System. Out. Println (" input amount of money: "); Double money=kk. NextDouble ();
System. Out.println (" mobile phone number: + number + ", "more credit" + money + "yuan!" ); break; }
System. Out.println (" whether to continue top-up (top-up, please enter y; No prepaid phone, please input n) : ");
A=kk. Next ();


}
System. The out. Println (" * * * * * * * * * * * * * * * * * * * * * * * * * * thank you for your help * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ");
}
}

CodePudding user response:

You didn't write out processing

A=kk. Next ();
No processing code later

CodePudding user response:

Because y never equal to n

CodePudding user response:

reference 1st floor tianfang response:
you didn't write out processing

A=kk. Next ();
No processing code behind


reference 1st floor tianfang response:
you didn't write out processing

A=kk. Next ();
No processing code behind
,,, exit deal with how to write

CodePudding user response:

refer to the second floor pinoco response:
because y never equal to n
later I enter a value is not returned to the while there, and thus n!=n jump out, I think that I don't know why but there is no true

CodePudding user response:

The
Kobe reference 4 floor? Response:
Quote: refer to the second floor pinoco response:
because y never equal to n
later I enter a value is not returned to the while there, and thus n!=n jump out, so I think but didn't realize I don't know why
first you n!=n the judgment is entered circulation to true, you are true, then you don't have put an end to conditions, you is not equal to n y into circulation, or y is not equal to n, next time you this is an infinite loop, should not be cycle to what value or after a few times to String a assignment, don't know if you say that logic

CodePudding user response:

The
refer to the original poster Kobe? Response:
front several answer to, but I feel you don't understand to
 import Java. Util. Scanner; 
Public class pay {

Public static void main (String [] args) {
Scanner kk=new Scanner (System. In);
String a="y";
While (a! {
="n")System. The out. Println (" * * * * * * * * * * * * * * * * * * more credit * * * * * * * * * * * * * * * * * * ");
System. The out. Println (" please enter the phone number: ");
String number=kk. Next ();
System. The out. Println (" 1.10 yuan \ \ t3.30 t2.20 yuan yuan \ \ t5.100 n4.50 yuan yuan \ t6 other amount ");
System. The out. Println (" serial number: please select a payment amount ");
Int. No=kk nextInt ();
The switch (no) {
Case 1: System. Out.println (" mobile phone number: "+ number +", 10 yuan more credit!" ); break;
Case 2: System. Out.println (" mobile phone number: "+ number +", more credit 20 yuan!" ); break;
Case 3: System. Out.println (" mobile phone number: "+ number +", more credit 30 yuan!" ); break;
Case 4: System. Out.println (" mobile phone number: "+ number +", more credit $50!" ); break;
Case 5: System. Out.println (" mobile phone number: "+ number +", 100 yuan more credit!" ); break;
Case 6: System. Out. Println (" input amount of money: "); Double money=kk. NextDouble ();
System. Out.println (" mobile phone number: + number + ", "more credit" + money + "yuan!" ); break; }
System. Out.println (" whether to continue top-up (top-up, please enter y; No prepaid phone, please input n) : ");
A=kk. Next ();
//here for a to the new value need to do an exit circulation processing
If (a=="n" | | a.e quals (" n ")) {
break;//exit loop
}
}
System. The out. Println (" * * * * * * * * * * * * * * * * * * * * * * * * * * thank you for your help * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ");
}
}

CodePudding user response:

Put a!="n"!" N "equals (a)
Have a ratio equals to priority==, code habit to develop

CodePudding user response:

String is object type, compared with equals

CodePudding user response:

You this question is very good, a lot of basis points more than cover the
First, the problem of the String, the first String a="y" to create a String, and you inside the while a=kk. The next (); To create a string kk. Next (), then assign it to a, so a reference to the address at this time has changed,
Second, "==", "==" comparison of two object memory address is the same, so the combination of the above said, a address has changed, and so are never equal

Third, the equals method, String to rewrite the method, compare two objects are the same content, so you modify the while (!" N "equals (a)) with respect to ok

Finally, coding standards, string comparison with equals method as far as possible

CodePudding user response:

Thank you all for bosses to help grateful!!!!!!!!!!!
  • Related