如何解决字符串消息错误中的错误?请帮我 [英] How can I solve error in string message error? Please help me
问题描述
错误消息:
it says that "Create method minPrime in isprime.isprime.listener1"
Methods minPrime and maxPrime can not be applied given types
我知道这个错误的原因是这样的:
$在minPrime和maxPrime函数中的b $ b我给出类类型的参数,所以当我想从 - >调用这些方法时字符串消息,会有错误,但是我怎么能解决这个问题?谁能帮助我?
I know that reason of this error is this:
in minPrime and maxPrime functions i give parameters like class type,so when i want to call these methods from-> String message,there will be error like there is.but how can i solve this?can anyone help me?
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package isprime;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class isprime extends JFrame {
private int n;
public isprime(int n) {
this.n = n;
}
public int getN() {
return n;
}
public void setN(int n) {
this.n = n;
}
private JLabel label1;
private JLabel label2;
private JButton min;
private JButton max;
private JTextField Box1;
private JTextField Box2;
public isprime() {
setSize(300, 200);
setTitle("Findingmax and min prime number in the range");
setLayout(new FlowLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
mine();//function prototype
}
public void mine() {
label1 = new JLabel("Enter first integer");
label2 = new JLabel("Enter second integer");
Box1 = new JTextField(5);
Box2 = new JTextField(5);
min = new JButton("Min");
max = new JButton("Max");
//add-de istediyimiz ardicilliqda daxil edireik
add(label1);
add(Box1);
add(label2);
add(Box2);
add(min);
add(max);
//action fired
min.addActionListener(new listener1()
);
max.addActionListener(new listener2()
);
}
private class listener1 implements ActionListener {
private int n;
public boolean isPrime(int n) {//checking is this number prime or not
boolean answer = true;
if (n == 2) {//because 2 is a prime number
return answer;
}
for (int i = 2; i <= n / 2; i++) {//we begin from two,becuase 1 is not prime or composite
// n/2-ye kimi yoxlamaq da kifayetdir
if (n % i == 0) {//eger eded yalniz ozune ve bire bolunerse ,prime-dir,qalan hallarda composite
answer = false;//demeli prime deyil
break;
}
}
return answer;
}
int nextPrime(int n) {
n++;
while (!isPrime(n)) {
n++;
}
return n;
}
public int maxPrime(isprime p) {//for finding max prime number in the given range
for (int i = this.n; i>= p.n; i--) {
if(this.isPrime(i))
return i;
}
return 1;
}
public void actionPerformed(ActionEvent e) {
int a;
int b;
try {
a = Integer.parseInt(Box1.getText());
b = Integer.parseInt(Box2.getText());
} catch (Exception exc) {
a = -1;
b = -1;
}
String message = "Max prime is:" + this.maxPrime(a, b);
JOptionPane.showMessageDialog(null, message);
}
}
private class listener2 implements ActionListener {
private int n;
public int minPrime(isprime p) {//for finding min prime number in the given range
for (int i = this.n; i <= p.n; i++) {
if(this.isPrime(i))
return i;
}
return 1;
}
public boolean isPrime(int n) {//checking is this number prime or not
boolean answer = true;
if (n == 2) {//because 2 is a prime number
return answer;
}
for (int i = 2; i <= n / 2; i++) {//we begin from two,becuase 1 is not prime or composite
// n/2-ye kimi yoxlamaq da kifayetdir
if (n % i == 0) {//eger eded yalniz ozune ve bire bolunerse ,prime-dir,qalan hallarda composite
answer = false;//demeli prime deyil
break;
}
}
return answer;
}
int nextPrime(int n) {
n++;
while (!isPrime(n)) {
n++;
}
return n;
}
public void actionPerformed(ActionEvent e) {
int a;
int b;
try {
a = Integer.parseInt(Box1.getText());
b = Integer.parseInt(Box2.getText());
} catch (Exception exc) {
a = -1;
b = -1;
}
String message = "Min prime is:" + this.minPrime();
JOptionPane.showMessageDialog(null, message);
}
}
public static void main(String[] args) {
new isprime();
}
}
我尝试过:
我试图用这个代码和GUI来找到范围内的最大和最小素数。我接受this.n是范围的起始数字,而pn是range.i的结束编号无法解决String消息中的错误。
What I have tried:
I tried to find max and min prime in range with this code and with GUI.which i accept that this.n is the beginning number of the range and p.n is the ending number of the range.i can't solve that error which is in String message.
推荐答案
考虑maxPrime
签名:
ConsidermaxPrime
signature:
public int maxPrime(isprime p)
它接受 isprime
引用作为唯一参数。
但是你写了
It accepts a isprime
reference as unique argument.
However you wrote
int a;
int b;
// ...
String message = "Max prime is:" + this.maxPrime(a, b);
方法调用与其签名不匹配,因此出错。
[update]
要使用 maxPrime
方法,你必须创建两个 isprime
对象,然后在其中一个上调用方法,将另一个作为参数传递,例如
The method invocation doesn't match its signature, hence the error.
[update]
To use maxPrime
method you have to create two isprime
objects and then invoke the method on one of them passing the other one as argument, e.g.
int a, b;
//...
isprime ipa = new isprime(a);
isprime ipb = new isprime(b);
int maxp = ipa.maxPrime(ipb);
[/ update]
这篇关于如何解决字符串消息错误中的错误?请帮我的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!