这是我正在使用的程序,需要4个int并找到它们的区域。我需要设置一个Exception来检测用户是否没有输入int和字符串。我希望它告诉用户(“仅限数字”)
stdate <= enddate
我有点想法就像这里一样..
package areacircle;
import java.util.Scanner;
public class AreaCircleTwoPointO {
public static void main(String[] args) {
Scanner reader = new Scanner (System.in);
System.out.print ("Type The x1 point please: ");
int x1 = reader.nextInt();
System.out.print ("Type The x2 point please: ");
int x2 = reader.nextInt();
System.out.print ("Type The y1 point please: ");
int y1 = reader.nextInt();
System.out.print ("Type the y2 point please: ");
int y2 = reader.nextInt();
double area = areaCircle(x1, x2, y1, y2);
System.out.println ("The area of your circle is: " + area);
}
public static double distance
(double x1, double y1, double x2, double y2)
{
double dx = x2 - x1;
double dy = y2 - y1;
double dsquared = dx*dx + dy*dy;
double result = Math.sqrt (dsquared);
return result;
}
public static double areaCircle(double x1, double y1, double x2, double y2)
{
double secretSauce = distance(x1, y1, x2, y2);
return areaCircleOG(secretSauce);
}
public static double areaCircleOG(double secretSauce)
{
double area = Math.PI * Math.pow(secretSauce, 2);
return area;
}
}
但关于这一点的是它正在使用JOptionPane而我不想使用JOptionPane,只是扫描仪。
答案 0 :(得分:0)
您可以使用以下代码:
public static void main(String[] args)
{
Scanner reader = new Scanner (System.in);
boolean finished = false;
while(!finished)
{
try
{
System.out.print ("Type The x1 point please: ");
int x1 = reader.nextInt();
System.out.print ("Type The x2 point please: ");
int x2 = reader.nextInt();
System.out.print ("Type The y1 point please: ");
int y1 = reader.nextInt();
System.out.print ("Type the y2 point please: ");
int y2 = reader.nextInt();
double area = areaCircle(x1, x2, y1, y2);
System.out.println ("The area of your circle is: " + area);
double area = areaCircle(x1, x2, y1, y2);
System.out.println ("The area of your circle is: " + area);
finished = true;
}
catch(NumberFormatException e)
{
System.out.println("Please type in a number! Try again.");
}
}
}