search

Home  >  Q&A  >  body text

java - 怎么输入回车跳出循环


这是我上java课要做的题,我在这个直接按回车键结束循环这里困住了。

请问要怎么修改呢?谢谢!

System.out.println(" ==== 构建园对象并计算其面积周长 ==== ");
System.out.println("请输入半径(直接按回车键结束程序)");
while(true)
{
    circleANDrectangle a1 = new circleANDrectangle();
    if()
        break;
    else
        a1.radius = input.nextDouble();
    number++;
    System.out.println("构建了半径为"+a1.radius+"的园,圆面积"+
    a1.yuans()+"、周长"+a1.yuanc());
    System.out.println("目前园个数为"+number);
    System.out.println("请输入半径(直接按回车键结束程序)");
}
System.out.println("——程序结束。");`
巴扎黑巴扎黑2804 days ago1173

reply all(1)I'll reply

  • 天蓬老师

    天蓬老师2017-04-17 17:28:04

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int count=0;
        while(true)
        {
            
            System.out.println("请输入半径:(直接按回车键程序结束)");
            String s = in.nextLine();
            if (s.equals("")){
                System.out.println("——————程序结束");
                break;
                }
            else{
                
                double r =Double.parseDouble(s);
                circle c = new circle(r);
                c.outValue();
                count++;
                System.out.println("目前圆个数:"+count);
            }
            
            
        }
    }
    

    This is a solution provided by a classmate, and it can be implemented.

    reply
    0
  • Cancelreply