我在哪里放这个代码? [英] Where do I put this code?
问题描述
import java.util.Scanner;
import java.lang.Math;
public class Circle {
public static void main(String [] args){
int radius;
double area,circumference;
Scanner in = new扫描仪(System.in);
System.out.print(你的圆的半径:);
if(in.hasNextInt())
{
radius = in.nextInt();
area = Math.PI * Math.pow(radius,2);
周长= 2 * Math.PI *半径;
System.out.println(区域:+区域);
System.out.println (周长:+周长);
}
其他
{
System.out.print( 输入无效);
}
}
}
< b>我尝试了什么:
我希望输入只是po敏感数字,意思是> = 1。
我认为如果(in.nextInt()> = 1)可行,但我不知道应该放在哪里。
import java.util.Scanner;
import java.lang.Math;
public class Circle {
public static void main (String []args){
int radius;
double area,circumference;
Scanner in = new Scanner (System.in);
System.out.print ("What is the radius of your circle: ");
if (in.hasNextInt())
{
radius = in.nextInt();
area = Math.PI * Math.pow(radius,2);
circumference = 2 * Math.PI * radius;
System.out.println("Area: " + area);
System.out.println("Circumference: " + circumference);
}
else
{
System.out.print ("Invalid input ");
}
}
}
What I have tried:
I want the input to be only positive numbers, meaning >=1 .
I think if (in.nextInt() >=1) would work but I'm not sure where it should be placed.
推荐答案
if (in.nextInt() >=1)
我会把它放在这里,这一行检查输入是否为正,但输入在测试中丢失,这可能是不需要的。
I would put this nowhere, this line check that the input is positive, but the input is lost in the test, which is probably unwanted.
参见 Java™教程 [ ^ ]。
这篇关于我在哪里放这个代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!