如何解决java while循环跳转命令
package assignment1;
/**
*
* @author ricar
*/
public class Change {
public String personName;
public double coinChange;
public Change(){
this.personName="Unknown";
this.coinChange=0.0;
}
public String getpersonName(){
return this.personName;
}
public double getcoinChange(){
return this.coinChange;
}
public void setpersonName(String name){
this.personName=name;
}
public void setCoinChange(int coin){
this.coinChange=coin;
}
public boolean inRange(int range){
boolean rang=true;
if(range>=5 && range<=95 && range%5==0){
rang=true;
}else{
rang=false;
}
return rang;
}
public static boolean keepEntering (char resp){
boolean result=false;
if (resp=='Y'){
result = true;
}
else if (resp=='N'){
result = false;
System.out.println("You quit");}
else
{
System.out.println("You have chosen an invalid option,the program will quit it self.");
}
return result;
}
}
package assignment1;
import java.util.Scanner;
/**
*
* @author ricar
*/
public class Client {
public static Scanner keyboard = new Scanner (System.in);
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Change c1 = new Change();
boolean keep=true;
while(keep==true){
System.out.println("Please enter the PERSON NAME: ");
c1.setpersonName(keyboard.nextLine());
System.out.println("Please enter the COIN VALUE: ");
c1.setCoinChange(keyboard.nextInt());
if(c1.inRange((int)c1.getcoinChange())){
System.out.println("The person name is: "+c1.getpersonName());
System.out.println("The coin change is: "+ c1.getcoinChange());
}
System.out.println("Do you wish to continue ? [y/n]");
keep = c1.keepEntering(keyboard.next().toUpperCase().charAt(0));
}
}
}
当我选择'Y'时,循环跳 c1.setpersonName(keyboard.nextLine());命令,结果是这样的:
运行:请输入人名:r请输入币值:5 人名是:r硬币找零是:5.0您想继续吗? ? [y / n] y请输入人名:请输入币值:
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。