[英]looping an if statement in Java
嘿,我有一个if和else语句,如果发生else则想循环,因为这意味着用户尚未输入所需的金额,因此程序重新启动,这里的代码有什么想法吗?
System.out.println("The area of the glass is " + area );
if (thick == 3)
{
System.out.println("The price to replace the glass will be £" + price1);
System.out.println("With VAT the price is £" + Final1 );
}
else if (thick == 5)
{
System.out.println("The price to replace the glass will be £" + price2);
System.out.println("With VAT the price is £" + Final2);
}
else if (thick == 7)
{
System.out.println("The price to replace the glass will be £" + price3);
System.out.println("With VAT the price is £" + Final3);
}
else
{
System.out.println("Sorry for the inconvenience but we do not do this size thinkness."); //If the we don't have the thickness this is displayed
}
提前致谢
使用while循环,直到变量可接受为止:
thick = 0;
while (thick != 3 && thick != 5 && thick != 7) {
// read new thick value from user
// your current code
}
您可以更简洁地表达以上内容:
for (thick = 0; !Arrays.asList(3, 5, 7).contains(thick);)
// read new thick value from user
// your current code
}
您可以使用循环。 如果输入了有效的响应,请跳出循环。 否则,请继续循环播放。
例:
while(true){
<get input here>
if(thick==3){
<do stuff>
break;
}
else if(...){
<do stuff>
break;
}
...
else{
<do stuff>
}
}
do {
your code
}while(the_else_occurs);
即:
boolean flag=false;
do{
if (thick == 3)
{
System.out.println("The price to replace the glass will be £" + price1);
System.out.println("With VAT the price is £" + Final1 );
}
else if (thick == 5)
{
System.out.println("The price to replace the glass will be £" + price2);
System.out.println("With VAT the price is £" + Final2);
}
else if (thick == 7)
{
System.out.println("The price to replace the glass will be £" + price3);
System.out.println("With VAT the price is £" + Final3);
}
else {
flag=true;
System.out.println("Sorry for the inconvenience but we do not do this size thinkness."); //If the we don't have the thickness this is displayed
}
}while(flag);
我认为您应该进行一些输入验证:
while(thick != 3 && thick != 5 && thick != 7 && thick != null){
System.out.println("Sorry for the inconvenience but we do not do this size thinkness.");
//If the we don't have the thickness this is displayed
}
如果输入的方法不正确,您也可以尝试创建一个方法并调用它(递归):
public void MyMethod() {
System.out.println("The area of the glass is " + area );
if (thick == 3)
{
System.out.println("The price to replace the glass will be £" + price1);
System.out.println("With VAT the price is £" + Final1 );
}
else if (thick == 5)
{
System.out.println("The price to replace the glass will be £" + price2);
System.out.println("With VAT the price is £" + Final2);
}
else if (thick == 7)
{
System.out.println("The price to replace the glass will be £" + price3);
System.out.println("With VAT the price is £" + Final3);
}
else
{
System.out.println("Sorry for the inconvenience but we do not do this size thinkness."); //If the we don't have the thickness this is displayed
MyMethod();
}
}
唯一的问题是您必须小心溢出堆栈。 但是,用switch
语句代替也是一个好主意。 只是一些建议。
您可以改用哈希图:
HashMap<int, PointF> map = new HashMap<int, PointF>()
{
{ 3, new PointF(4.5f, 6) },
{ 5, new PointF(8, 6.3f) },
{ 6, new PointF(9, 8) },
};
if (map.containsKey(thick))
{
System.out.println("The price to replace the glass will be £" + map.get(thick).x);
System.out.println("With VAT the price is £" + map.get(thick).y);
}
如果您可以使用自定义类切换PointF,那就更好了
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.