java的一个小程序,判断输入数字的位数,要用到异常处理
发布网友
发布时间:2022-04-26 21:44
我来回答
共3个回答
热心网友
时间:2023-11-06 20:39
你的getnum方法 定义的时候需要返回int ,但你的方法体里面没有return,报错了
其实这个地方不用返回
public void getnum(int i) throws AAException {
if (i < 0 || i > 9999) {
throw new AAException();
}
if (i >= 0 && i <= 9)
System.out.print("一位数");
else if (i >= 10 && i <= 99)
System.out.print("两位数");
else if (i >= 100 && i <= 999)
System.out.print("三位数");
else if (i >= 1000 && i <= 9999)
System.out.print("四位数");
}
在调用的时候,这样写
try {
String s = stdin.nextLine();
double ii = Double.parseDouble(s);
int i = (int) ii;
a.getnum(i);
} catch (AAException e) {
System.out.println(e.toString());
}
追问能给一个完整的程序么,最好运行过的,不懂啊!
追答
AAException.还是用你自己的,我给你重写下A
class A {
public static void main(String args[]) {
Scanner stdin = new Scanner(System.in);
System.out.println("请输入任意一个0到9999的数字");
try {
String s = stdin.nextLine();
double num = Double.parseDouble(s);
getnum(num);
} catch (AAException e) {
System.out.println(e.toString());
}
}
private static void getnum(double i) throws AAException {
if (i < 0 || i > 9999) {
throw new AAException();
}
if (i >= 0 && i <= 9) {
System.out.print("一位数");
} else if (i >= 10 && i <= 99) {
System.out.print("两位数");
} else if (i >= 100 && i <= 999) {
System.out.print("三位数");
} else {
System.out.print("四位数");
}
}
}
热心网友
时间:2023-11-06 20:39
import java.util.*;
public class A {
class AAException extends Exception
{
String a;
AAException(){
a="输入数据出错";
}
public String toString(){
return a;
}
}
class BB{
public int getnum(double i) throws AAException
{
if(i<0||i>9999)
{
AAException exception=new AAException();
throw exception;
}
if(i>=0&&i<=9)
System.out.print("一位数");
else if(i>=10&&i<=99)
System.out.print("两位数");
else if(i>=100&&i<=999)
System.out.print("三位数");
else if(i>=1000&&i<=9999)
System.out.print("四位数");
}
}
public static void main(String args[]){
Scanner stdin=new Scanner(System.in);
BB a=new BB();
int result=0;
System.out.println("请输入任意一个0到9999的数字");
try{
String s=stdin.nextLine();
double ii=Double.parseDouble(s);
result=a.getnum(ii);
}catch(AAException e){
System.out.println(e.toString());
}
}
}
热心网友
时间:2023-11-06 20:40
public static void main(String[] args) throws ParseException {
while (true) {
Scanner stdin = new Scanner(System.in);
int result = 0;
System.out.println("请输入任意一个0到9999的数字");
try {
String s = stdin.nextLine();
double ii = Double.parseDouble(s);
int i = (int) ii;
result = getnum(i);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
public static int getnum(int i) {
int num = 0;
if (i < 0 || i > 9999) {
throw new RuntimeException("请重新输入");
}
System.out.println(String.valueOf(i).length() + "位数字");
return i;
}
这样简单多了
热心网友
时间:2023-11-06 20:39
你的getnum方法 定义的时候需要返回int ,但你的方法体里面没有return,报错了
其实这个地方不用返回
public void getnum(int i) throws AAException {
if (i < 0 || i > 9999) {
throw new AAException();
}
if (i >= 0 && i <= 9)
System.out.print("一位数");
else if (i >= 10 && i <= 99)
System.out.print("两位数");
else if (i >= 100 && i <= 999)
System.out.print("三位数");
else if (i >= 1000 && i <= 9999)
System.out.print("四位数");
}
在调用的时候,这样写
try {
String s = stdin.nextLine();
double ii = Double.parseDouble(s);
int i = (int) ii;
a.getnum(i);
} catch (AAException e) {
System.out.println(e.toString());
}
追问能给一个完整的程序么,最好运行过的,不懂啊!
追答
AAException.还是用你自己的,我给你重写下A
class A {
public static void main(String args[]) {
Scanner stdin = new Scanner(System.in);
System.out.println("请输入任意一个0到9999的数字");
try {
String s = stdin.nextLine();
double num = Double.parseDouble(s);
getnum(num);
} catch (AAException e) {
System.out.println(e.toString());
}
}
private static void getnum(double i) throws AAException {
if (i < 0 || i > 9999) {
throw new AAException();
}
if (i >= 0 && i <= 9) {
System.out.print("一位数");
} else if (i >= 10 && i <= 99) {
System.out.print("两位数");
} else if (i >= 100 && i <= 999) {
System.out.print("三位数");
} else {
System.out.print("四位数");
}
}
}
热心网友
时间:2023-11-06 20:39
import java.util.*;
public class A {
class AAException extends Exception
{
String a;
AAException(){
a="输入数据出错";
}
public String toString(){
return a;
}
}
class BB{
public int getnum(double i) throws AAException
{
if(i<0||i>9999)
{
AAException exception=new AAException();
throw exception;
}
if(i>=0&&i<=9)
System.out.print("一位数");
else if(i>=10&&i<=99)
System.out.print("两位数");
else if(i>=100&&i<=999)
System.out.print("三位数");
else if(i>=1000&&i<=9999)
System.out.print("四位数");
}
}
public static void main(String args[]){
Scanner stdin=new Scanner(System.in);
BB a=new BB();
int result=0;
System.out.println("请输入任意一个0到9999的数字");
try{
String s=stdin.nextLine();
double ii=Double.parseDouble(s);
result=a.getnum(ii);
}catch(AAException e){
System.out.println(e.toString());
}
}
}
热心网友
时间:2023-11-06 20:40
public static void main(String[] args) throws ParseException {
while (true) {
Scanner stdin = new Scanner(System.in);
int result = 0;
System.out.println("请输入任意一个0到9999的数字");
try {
String s = stdin.nextLine();
double ii = Double.parseDouble(s);
int i = (int) ii;
result = getnum(i);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
public static int getnum(int i) {
int num = 0;
if (i < 0 || i > 9999) {
throw new RuntimeException("请重新输入");
}
System.out.println(String.valueOf(i).length() + "位数字");
return i;
}
这样简单多了