Java 您在该代码/程序中发现了什么错误?除非你先输入一个数字,否则它不会运行
所以我的问题是: 这个程序用来计算圆的面积 用户输入一个数字,然后给出答案 此外,任何无效的输入是不允许的,我使用的尝试捕捉,但它不会工作 非常感谢大家抽出时间:)) 代码如下:Java 您在该代码/程序中发现了什么错误?除非你先输入一个数字,否则它不会运行,java,class,terminal,Java,Class,Terminal,所以我的问题是: 这个程序用来计算圆的面积 用户输入一个数字,然后给出答案 此外,任何无效的输入是不允许的,我使用的尝试捕捉,但它不会工作 非常感谢大家抽出时间:)) 代码如下: import java.util.Scanner; import java.io; /** * * @author Osugule */ public class AreaCircle { /** * @param args the command line arguments
import java.util.Scanner;
import java.io;
/**
*
* @author Osugule
*/
public class AreaCircle {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Scanner sc = new Scanner(System.in); // read the keyboard
System.out.println("This program will calculate the area of a circle");
System.out.println("Enter radius:");//Print to screen
double r = sc.nextDouble(); // Read in the double from the keyboard
double area = (3.14 *r * r);
try {
}
catch( NumberFormatException e ) {
System.out.println("Invalid Input, please enter a number");
//put a message or anything you want to tell the user that their input was weird.
}
String output = "Radius: " + r + "\n";
output = output + "Area: " + area + "\n";
System.out.println("The area of the circle is " + area);
}
}
你应该把这两行
double r = sc.nextDouble(); // Read in the double from the keyboard
double area = (3.14 *r * r);
在try
块内-
import java.util.Scanner;
import java.io;
/**
*
* @author Osugule
*/
public class AreaCircle
{
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
// TODO code application logic here
Scanner sc = new Scanner(System.in); // read the keyboard
System.out.println("This program will calculate the area of a circle");
boolean correct_input = false;
while(correct_input == false)
{
System.out.println("Enter radius:");//Print to screen
try
{
double r = sc.nextDouble(); // Read in the double from the keyboard
double area = (3.14 *r * r);
String output = "Radius: " + r + "\n";
output = output + "Area: " + area + "\n";
System.out.println("The area of the circle is " + area);
correct_input = true;
}
catch( NumberFormatException e )
{
System.out.println("Invalid Input, please enter a number");
//put a message or anything you want to tell the user that their input was weird.
}
catch( InputMismatchException e )
{
System.out.println("Input Mismatch, please enter a number");
//put a message or anything you want to tell the user that there is an
//input mismatch.
}
}
}
}
catch( NumberFormatException e )
{
System.out.println("Invalid Input, please enter a number");
//put a message or anything you want to tell the user that their input was weird.
}
// New catch block
catch( InputMismatchException e )
{
System.out.println("Input Mismatch, please enter a number");
//put a message or anything you want to tell the user that there is an
//input mismatch.
}
从—
public double nextDouble()
将输入的下一个标记扫描为双精度标记。如果无法将下一个标记转换为有效的双精度值,则此方法将抛出InputMismatchException。如果翻译成功,扫描仪将通过匹配的输入
抛出:
InputMismatchException-如果下一个标记与浮点正则表达式不匹配,
或者超出范围
NoTouchElementException-如果输入已耗尽
IllegalStateException-如果此扫描仪已关闭
因此,如果输入了无效字符,sc.nextDouble()
可能会抛出一个InputMismatchException
,该异常不会被catch
块捕获,因为它只包含catcherNumberFormatException
。因此,您的程序将在抛出异常后终止,但您不会收到任何消息
要处理此情况,请在当前catch
块下方添加以下块-
import java.util.Scanner;
import java.io;
/**
*
* @author Osugule
*/
public class AreaCircle
{
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
// TODO code application logic here
Scanner sc = new Scanner(System.in); // read the keyboard
System.out.println("This program will calculate the area of a circle");
boolean correct_input = false;
while(correct_input == false)
{
System.out.println("Enter radius:");//Print to screen
try
{
double r = sc.nextDouble(); // Read in the double from the keyboard
double area = (3.14 *r * r);
String output = "Radius: " + r + "\n";
output = output + "Area: " + area + "\n";
System.out.println("The area of the circle is " + area);
correct_input = true;
}
catch( NumberFormatException e )
{
System.out.println("Invalid Input, please enter a number");
//put a message or anything you want to tell the user that their input was weird.
}
catch( InputMismatchException e )
{
System.out.println("Input Mismatch, please enter a number");
//put a message or anything you want to tell the user that there is an
//input mismatch.
}
}
}
}
catch( NumberFormatException e )
{
System.out.println("Invalid Input, please enter a number");
//put a message or anything you want to tell the user that their input was weird.
}
// New catch block
catch( InputMismatchException e )
{
System.out.println("Input Mismatch, please enter a number");
//put a message or anything you want to tell the user that there is an
//input mismatch.
}
你应该把这两行
double r = sc.nextDouble(); // Read in the double from the keyboard
double area = (3.14 *r * r);
在try
块内-
import java.util.Scanner;
import java.io;
/**
*
* @author Osugule
*/
public class AreaCircle
{
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
// TODO code application logic here
Scanner sc = new Scanner(System.in); // read the keyboard
System.out.println("This program will calculate the area of a circle");
boolean correct_input = false;
while(correct_input == false)
{
System.out.println("Enter radius:");//Print to screen
try
{
double r = sc.nextDouble(); // Read in the double from the keyboard
double area = (3.14 *r * r);
String output = "Radius: " + r + "\n";
output = output + "Area: " + area + "\n";
System.out.println("The area of the circle is " + area);
correct_input = true;
}
catch( NumberFormatException e )
{
System.out.println("Invalid Input, please enter a number");
//put a message or anything you want to tell the user that their input was weird.
}
catch( InputMismatchException e )
{
System.out.println("Input Mismatch, please enter a number");
//put a message or anything you want to tell the user that there is an
//input mismatch.
}
}
}
}
catch( NumberFormatException e )
{
System.out.println("Invalid Input, please enter a number");
//put a message or anything you want to tell the user that their input was weird.
}
// New catch block
catch( InputMismatchException e )
{
System.out.println("Input Mismatch, please enter a number");
//put a message or anything you want to tell the user that there is an
//input mismatch.
}
从—
public double nextDouble()
将输入的下一个标记扫描为双精度标记。如果无法将下一个标记转换为有效的双精度值,则此方法将抛出InputMismatchException。如果翻译成功,扫描仪将通过匹配的输入
抛出:
InputMismatchException-如果下一个标记与浮点正则表达式不匹配,
或者超出范围
NoTouchElementException-如果输入已耗尽
IllegalStateException-如果此扫描仪已关闭
因此,如果输入了无效字符,sc.nextDouble()
可能会抛出一个InputMismatchException
,该异常不会被catch
块捕获,因为它只包含catcherNumberFormatException
。因此,您的程序将在抛出异常后终止,但您不会收到任何消息
要处理此情况,请在当前catch
块下方添加以下块-
import java.util.Scanner;
import java.io;
/**
*
* @author Osugule
*/
public class AreaCircle
{
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
// TODO code application logic here
Scanner sc = new Scanner(System.in); // read the keyboard
System.out.println("This program will calculate the area of a circle");
boolean correct_input = false;
while(correct_input == false)
{
System.out.println("Enter radius:");//Print to screen
try
{
double r = sc.nextDouble(); // Read in the double from the keyboard
double area = (3.14 *r * r);
String output = "Radius: " + r + "\n";
output = output + "Area: " + area + "\n";
System.out.println("The area of the circle is " + area);
correct_input = true;
}
catch( NumberFormatException e )
{
System.out.println("Invalid Input, please enter a number");
//put a message or anything you want to tell the user that their input was weird.
}
catch( InputMismatchException e )
{
System.out.println("Input Mismatch, please enter a number");
//put a message or anything you want to tell the user that there is an
//input mismatch.
}
}
}
}
catch( NumberFormatException e )
{
System.out.println("Invalid Input, please enter a number");
//put a message or anything you want to tell the user that their input was weird.
}
// New catch block
catch( InputMismatchException e )
{
System.out.println("Input Mismatch, please enter a number");
//put a message or anything you want to tell the user that there is an
//input mismatch.
}
我编辑了答案,这样它会再次询问用户输入是否错误 您需要在try子句中添加nextDouble:
boolean inputProcessed = false;
while (!inputProcessed) {
try {
double r = sc.nextDouble(); // Read in the double from the keyboard
double area = (3.14 *r * r);
String output = "Radius: " + r + "\n";
output = output + "Area: " + area + "\n";
System.out.println("The area of the circle is " + area);
// this indicates that we were able to process the input, and we should stop now.
inputProcessed = true;
}
catch( NumberFormatException e ) {
System.out.println("Invalid Input, please enter a number");
//put a message or anything you want to tell the user that their input was weird.
}
}
请注意,我在try子句中添加了try catch之后的行,好像您无法解析数字一样,它们不应该被打印出来。我编辑了答案,这样它会再次询问用户输入是否错误 您需要在try子句中添加nextDouble:
boolean inputProcessed = false;
while (!inputProcessed) {
try {
double r = sc.nextDouble(); // Read in the double from the keyboard
double area = (3.14 *r * r);
String output = "Radius: " + r + "\n";
output = output + "Area: " + area + "\n";
System.out.println("The area of the circle is " + area);
// this indicates that we were able to process the input, and we should stop now.
inputProcessed = true;
}
catch( NumberFormatException e ) {
System.out.println("Invalid Input, please enter a number");
//put a message or anything you want to tell the user that their input was weird.
}
}
请注意,我在try子句中添加了try catch之后的行,就好像您无法解析数字一样,它们不应该被打印。您必须捕获
InputMismatchException
异常,如下所示:
import java.util.InputMismatchException;
import java.util.Scanner;
public class AreaCircle {
public static void main(String[] args) {
// TODO code application logic here
Scanner sc = new Scanner(System.in); // read the keyboard
System.out.println("This program will calculate the area of a circle");
System.out.println("Enter radius:");//Print to screen
try {
double r = sc.nextDouble(); // Read in the double from the keyboard
double area = (3.14 *r * r);
String output = "Radius: " + r + "\n";
output = output + "Area: " + area + "\n";
System.out.println("The area of the circle is " + area);
}
catch( InputMismatchException e ) {
System.out.println("Invalid Input, please enter a number");
//put a message or anything you want to tell the user that their input was weird.
}
catch( NumberFormatException e ) {
System.out.println("Invalid Input, please enter a number");
//put a message or anything you want to tell the user that their input was weird.
}
}
}
您必须捕获如下异常:
import java.util.InputMismatchException;
import java.util.Scanner;
public class AreaCircle {
public static void main(String[] args) {
// TODO code application logic here
Scanner sc = new Scanner(System.in); // read the keyboard
System.out.println("This program will calculate the area of a circle");
System.out.println("Enter radius:");//Print to screen
try {
double r = sc.nextDouble(); // Read in the double from the keyboard
double area = (3.14 *r * r);
String output = "Radius: " + r + "\n";
output = output + "Area: " + area + "\n";
System.out.println("The area of the circle is " + area);
}
catch( InputMismatchException e ) {
System.out.println("Invalid Input, please enter a number");
//put a message or anything you want to tell the user that their input was weird.
}
catch( NumberFormatException e ) {
System.out.println("Invalid Input, please enter a number");
//put a message or anything you want to tell the user that their input was weird.
}
}
}
我建议养成使用
Math.PI
而不是3.14
的习惯,你是第一次编码吗??首先理解代码的含义!是的,不是第一次,但我刚开始我建议养成使用Math.PI
而不是3.14
的习惯你是第一次编码吗??首先理解代码的含义!是的,不是第一次,但我刚刚开始OK,很好,非常感谢…但是当我运行程序并输入无效的字母时,程序就结束了?程序结束了,因为异常捕获程序正在捕获坏异常并继续执行到最后。如果您希望程序再次提示用户,则需要将整个try/catch
块放入while循环中。好的,非常感谢……但当我运行程序并输入无效字符时,程序刚刚结束?程序结束,因为异常捕获器刚刚捕获坏异常并继续执行到结束。如果您希望程序再次提示用户,则需要将整个try/catch
块放入while循环中。如果我在其中放入无效字符,为什么程序刚好结束?@user1190386:可能是抛出输入不匹配异常
。查看编辑。@user1190386:请让我知道它是否有效:-)。非常感谢,我真的很感激。我现在对学习java非常陌生。对不起,我还有一个问题。。。。当我输入一个无效的输入时,它会显示消息,程序会停止..我需要使用while循环吗?如果我输入了一个无效的字符,为什么程序会结束?@user1190386:可能它抛出了InputMismatchException
。查看编辑。@user1190386:请让我知道它是否有效:-)。非常感谢,我真的很感激。我现在对学习java非常陌生。对不起,我还有一个问题。。。。当我输入无效输入时,它会显示消息,程序停止..我需要使用while循环吗?当用户输入错误输入时,程序会停止吗?当用户输入错误输入时,程序会停止吗??