java找不到符号的问题
答案:5 悬赏:50
解决时间 2021-02-17 23:32
- 提问者网友:niaiwoma
- 2021-02-17 00:54
java找不到符号的问题
最佳答案
- 二级知识专家网友:底特律间谍
- 2021-02-17 02:23
你把zeroException和A俩个类定义到Shiyan8_7 类外边,没你这样的写法,内部类也不是这样写的,
public class Shiyan8_7 {
public static void main(String args[]) {
A a = new A();
int b;
Scanner reader = new Scanner(System.in);
b = reader.nextInt();
try {
a.f(b);
} catch (zeroException x) {
System.out.println(x.getMessage());
}
}
}
class zeroException extends Exception {
String message;
zeroException(int n) {
message = n + "不是正数";
}
public String getMessage() {
return message;
}
}
class A {
public void f(int m) throws zeroException {
if (m < 0) {
zeroException ex = new zeroException(m);
throw (ex);
}
double number = Math.sqrt(m);
System.out.println(m + "的平方根是" + number);
}
}
public class Shiyan8_7 {
public static void main(String args[]) {
A a = new A();
int b;
Scanner reader = new Scanner(System.in);
b = reader.nextInt();
try {
a.f(b);
} catch (zeroException x) {
System.out.println(x.getMessage());
}
}
}
class zeroException extends Exception {
String message;
zeroException(int n) {
message = n + "不是正数";
}
public String getMessage() {
return message;
}
}
class A {
public void f(int m) throws zeroException {
if (m < 0) {
zeroException ex = new zeroException(m);
throw (ex);
}
double number = Math.sqrt(m);
System.out.println(m + "的平方根是" + number);
}
}
全部回答
- 1楼网友:不想翻身的咸鱼
- 2021-02-17 06:12
修改了一下:import java.util.Scanner;
public class Shiyan8_7 {
public static void main(String args[]) {
A a = new A();
int b;
Scanner reader = new Scanner(System.in);
b = reader.nextInt();
try {
a.f(b);
} catch(zeroException x) {
System.out.println(x.getMessage());
}
}
}
//放到外面来
class zeroException extends Exception {
String message;
zeroException(int n) {
message = n + "不是正数";
}
public String getMessage() {
return message;
}
}
class A {
public void f(int m) throws zeroException {
if(m<0) {
zeroException ex = new zeroException(m);
throw(ex);
}
double number = Math.sqrt(m);
System.out.println(m+"的平方根是" + number);
}
}
- 2楼网友:爱难随人意
- 2021-02-17 04:45
你把zeroException类放在了Shiyan8_7类里面了。
这主要是你写代码不规范造成的。
下面是正确的:public class Shiyan8_7 {
public static void main(String[] args) {
A a=new A();
int b;
Scanner reader= new Scanner(System.in);
b=reader.nextInt();
try{
a.f(b);
}catch(zeroException x){
System.out.println(x.getMessage());
}
}
}
class zeroException extends Exception{
String message;
zeroException(int n){
message=n+"不是正数";
}
public String getMessage(){
return message;
}
}
class A{
public void f(int m) throws zeroException{
if(m<0){
zeroException ex=new zeroException(m);
throw(ex);
}
double number=Math.sqrt(m);
System.out.println(m+"的平方根是"+number);
}
}
- 3楼网友:舊物识亽
- 2021-02-17 03:54
大括号不匹配,改成:
import java.util.Scanner;
public class Shiyan8_7{
public static void main(String args[]){
A a=new A();
int b;
Scanner reader= new Scanner(System.in);
b=reader.nextInt();
try{
a.f(b);
}
catch(zeroException x){
System.out.println(x.getMessage());
}
}
}
class zeroException extends Exception{
String message;
zeroException(int n){
message=n+"不是正数";
}
public String getMessage(){
return message;
}
}
class A{
public void f(int m) throws zeroException{
if(m<0){
zeroException ex=new zeroException(m);
throw(ex);
}
double number=Math.sqrt(m);
System.out.println(m+"的平方根是"+number);
}
}
import java.util.Scanner;
public class Shiyan8_7{
public static void main(String args[]){
A a=new A();
int b;
Scanner reader= new Scanner(System.in);
b=reader.nextInt();
try{
a.f(b);
}
catch(zeroException x){
System.out.println(x.getMessage());
}
}
}
class zeroException extends Exception{
String message;
zeroException(int n){
message=n+"不是正数";
}
public String getMessage(){
return message;
}
}
class A{
public void f(int m) throws zeroException{
if(m<0){
zeroException ex=new zeroException(m);
throw(ex);
}
double number=Math.sqrt(m);
System.out.println(m+"的平方根是"+number);
}
}
- 4楼网友:鸠书
- 2021-02-17 03:46
import java.util.Scanner;
public class Shiyan8_7{
public static void main(String args[]){
A a=new A();
int b;
Scanner reader= new Scanner(System.in);
b=reader.nextInt();
try{
a.f(b);
}
catch(zeroException x){
System.out.println(x.getMessage());
}
}
}
class zeroException extends Exception{
String message;
zeroException(int n){
message=n+"不是正数";
}
public String getMessage(){
return message;
}
}
class A{
public void f(int m) throws zeroException{
if(m<0){
zeroException ex=new zeroException(m);
throw(ex);
}
double number=Math.sqrt(m);
System.out.println(m+"的平方根是"+number);
}
}
把最下面的两个括号移到两个类的上面
public class Shiyan8_7{
public static void main(String args[]){
A a=new A();
int b;
Scanner reader= new Scanner(System.in);
b=reader.nextInt();
try{
a.f(b);
}
catch(zeroException x){
System.out.println(x.getMessage());
}
}
}
class zeroException extends Exception{
String message;
zeroException(int n){
message=n+"不是正数";
}
public String getMessage(){
return message;
}
}
class A{
public void f(int m) throws zeroException{
if(m<0){
zeroException ex=new zeroException(m);
throw(ex);
}
double number=Math.sqrt(m);
System.out.println(m+"的平方根是"+number);
}
}
把最下面的两个括号移到两个类的上面
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯