2017-05-08 54 views
0

我正在写一个主要方法,要求用户以圆锥体的半径和高度的长度的形式输入,然后调用3个其他数学方法来确定圆锥体底部的面积作为它的表面积和体积。为什么这个循环不支持前两个输入?

这个想法是,你应该能够输入多组输入,并通过输入“q”表示你已完成输入。示例输入可以例如是“10 5 6 8 7 5 q”。然后程序应该计算三次,用两组半径和高度计算,然后打破循环。相反,它忽略了前两个输入,并且完美地完成了其余四个输入。它基本上计算n-1组高度和半径,其中n是提供的组数。我非常感谢这方面的帮助。

while(true)  //Infinite loop 
{ 
if (scan.hasNextInt())  //If next input is an integer, read it 
    { 
    radie = scan.nextInt(); 
    height = scan.nextInt(); 
    } 
else if (scan.next().equals('q')) 
    {       //If it instead if "q", break the loop 

    break; 
    } 


    do 
    { 
     if (scan.hasNextInt()) 
     { 
     radie = scan.nextInt(); 
     height = scan.nextInt(); 
     } 


    System.out.print("r = "+radius); 
     System.out.println("h = "+height); 
     System.out.println("Bottom area:  "+ area(radius)); 
     System.out.println("Surface area: "+area(radie, height)); 
     System.out.println("Volume:    "+ volume(radius, height)); 

    }while (scan.next() != "\n"); 
} 

回答

1

您在两个整数首先阅读。但是,这两个整数都不会在您的程序中使用。因此,他们被扔掉了。

要么消除第二个循环并向上移动输出。或者,在第二个循环之前复制输出。

+0

谢谢,这解决了问题!我不知道我怎么没有看到。我在下面发布了更新的代码,因为我现在又遇到了另一个问题。当我输入例如“10 5 3 4 q”时,程序应计算两组输入,然后继续。但是,它现在计算三组,最后两组相同。任何猜测为什么这可能是? –

+0

因为你的else if(scan.next()。equals('q'))break;'语句检查'char''q',但扫描器读取它作为一个整体'string',因为你正在调用'scan .next()'输入令牌,因此不会中断while循环。用双引号括住你的'q'。检查我的答案的第一个代码片段。 –

+0

此外,请将您的编辑发布在原始问题本身的下方,而不是将其作为答案提交。考虑在问题中移动它并删除您的答案。 –

1

问题与您的代码是,你永远不会使用前两个intwhile循环扫描。而是执行此操作:

while (true)  //Infinite loop 
    { 
     if (scan.hasNextInt())  //If next input is an integer, read it 
     { 
      radius = scan.nextInt(); 
      height = scan.nextInt(); 
     } else if (scan.next().equals("q")) break; 
     System.out.print(radius); 
     System.out.println(height); 
    }` 

或者,只是检查int

int radius; 
int height; 

//provided user would be entering radius and height in pairs 
while (scan.hasNextInt()) { 
    radius = scan.nextInt(); 
    height = scan.nextInt(); 
    //Call your methods 
} 

或者,因为你不知道的参数,用户将被输入数字,它会更好,如果你创建半径以及高度的ArrayList,并将输入添加到列表中,然后从列表中处理它们。这将帮助你保持甚至输入退出while循环后:

ArrayList<Integer> radiusList = new ArrayList<>(); 
    ArrayList<Integer> heightList = new ArrayList<>(); 


    while (scan.hasNextInt()) { 
     radiusList.add(scan.nextInt(); 
     heightList.add(scan.nextInt()); 
    } 

    int radius; 
    int height; 
    for (int i = 0; i < radiusList.size(); i++) { 
     radius = radiusList.get(i); 
     height = heightList.get(i); 
     //Call your methods. 
    } 
+0

这是一个聪明的做法!我会研究它 –

0
while(true)  //Infinite loop 
{ 
if (scan.hasNextInt())  //If next input is an integer, read it 
{ 
radie = scan.nextInt(); 
height = scan.nextInt(); 
} 
else if (scan.next().equals('q')) 
{       //If it instead if "q", break the loop 
    break; 
} 
    System.out.print("r = "+radius); 
    System.out.println("h = "+height); 
    System.out.println("Bottom area:  "+ area(radius)); 
    System.out.println("Surface area: "+area(radie, height)); 
    System.out.println("Volume:    "+ volume(radius, height)); 

} 

}